yomichan/ext/bg/js/ankiweb.js

169 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-10-12 03:54:54 +00:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class AnkiWeb {
2016-10-16 00:30:49 +00:00
constructor(username, password) {
this.username = username;
this.password = password;
this.noteInfo = null;
2016-12-29 17:36:54 +00:00
chrome.webRequest.onBeforeSendHeaders.addListener(
details => {
details.requestHeaders.push({name: 'Origin', value: 'https://ankiweb.net'});
return {requestHeaders: details.requestHeaders};
},
{urls: ['https://ankiweb.net/*']},
['blocking', 'requestHeaders']
);
2016-10-12 03:54:54 +00:00
}
addNote(note) {
2016-10-18 03:59:54 +00:00
return this.retrieve().then(info => {
const model = info.models.find(m => m.name === note.modelName);
if (!model) {
2016-10-20 04:21:00 +00:00
return Promise.reject('cannot add note model provided');
2016-10-18 03:59:54 +00:00
}
const fields = [];
for (const field of model.fields) {
fields.push(note.fields[field]);
}
2016-10-19 04:28:25 +00:00
const data = {
data: JSON.stringify([fields, note.tags.join(' ')]),
mid: model.id,
deck: note.deckName
};
return AnkiWeb.loadAccountPage('https://ankiweb.net/edit/save', data, this.username, this.password);
}).then(response => response !== '0');
2016-10-12 03:54:54 +00:00
}
canAddNotes(notes) {
2016-10-18 03:20:50 +00:00
return Promise.resolve(new Array(notes.length).fill(true));
2016-10-12 03:54:54 +00:00
}
getDeckNames() {
2016-10-16 00:30:49 +00:00
return this.retrieve().then(info => info.deckNames);
2016-10-12 03:54:54 +00:00
}
getModelNames() {
2016-10-16 00:30:49 +00:00
return this.retrieve().then(info => info.models.map(m => m.name));
2016-10-12 03:54:54 +00:00
}
getModelFieldNames(modelName) {
2016-10-16 00:30:49 +00:00
return this.retrieve().then(info => {
const model = info.models.find(m => m.name === modelName);
return model ? model.fields : [];
});
}
retrieve() {
if (this.noteInfo !== null) {
return Promise.resolve(this.noteInfo);
}
2016-10-17 04:45:41 +00:00
return AnkiWeb.scrape(this.username, this.password).then(({deckNames, models}) => {
2016-10-16 00:30:49 +00:00
this.noteInfo = {deckNames, models};
return this.noteInfo;
});
}
logout() {
return AnkiWeb.loadPage('https://ankiweb.net/account/logout', null);
}
2016-10-17 04:45:41 +00:00
static scrape(username, password) {
2016-10-19 04:28:25 +00:00
return AnkiWeb.loadAccountPage('https://ankiweb.net/edit/', null, username, password).then(response => {
2016-10-17 04:45:41 +00:00
const modelsMatch = /editor\.models = (.*}]);/.exec(response);
if (modelsMatch === null) {
return Promise.reject('failed to scrape model data');
}
const decksMatch = /editor\.decks = (.*}});/.exec(response);
if (decksMatch === null) {
return Promise.reject('failed to scrape deck data');
}
const modelsJson = JSON.parse(modelsMatch[1]);
const decksJson = JSON.parse(decksMatch[1]);
const deckNames = Object.keys(decksJson).map(d => decksJson[d].name);
const models = [];
for (const modelJson of modelsJson) {
models.push({
name: modelJson.name,
id: modelJson.id,
fields: modelJson.flds.map(f => f.name)
});
}
return {deckNames, models};
});
}
2016-10-17 00:56:00 +00:00
2016-10-17 04:45:41 +00:00
static login(username, password) {
if (username.length === 0 || password.length === 0) {
2016-10-20 04:21:00 +00:00
return Promise.reject('login credentials not specified');
2016-10-16 00:30:49 +00:00
}
2016-10-19 04:28:25 +00:00
const data = {username, password, submitted: 1};
return AnkiWeb.loadPage('https://ankiweb.net/account/login', data).then(response => {
2016-10-17 04:45:41 +00:00
if (!response.includes('class="mitem"')) {
return Promise.reject('failed to authenticate');
}
2016-10-16 00:30:49 +00:00
});
}
2016-10-19 04:28:25 +00:00
static loadAccountPage(url, data, username, password) {
return AnkiWeb.loadPage(url, data).then(response => {
2016-10-17 04:45:41 +00:00
if (response.includes('name="password"')) {
2016-10-19 04:28:25 +00:00
return AnkiWeb.login(username, password).then(() => AnkiWeb.loadPage(url, data));
2016-10-17 04:45:41 +00:00
} else {
return response;
}
2016-10-16 00:30:49 +00:00
});
}
2016-10-19 04:28:25 +00:00
static loadPage(url, data) {
2016-10-16 00:30:49 +00:00
return new Promise((resolve, reject) => {
2016-12-29 17:36:54 +00:00
let dataEnc = null;
2016-10-19 04:28:25 +00:00
if (data) {
const params = [];
for (const key in data) {
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`);
}
2016-12-29 17:36:54 +00:00
dataEnc = params.join('&');
2016-10-19 04:28:25 +00:00
}
2016-10-16 02:51:41 +00:00
const xhr = new XMLHttpRequest();
2016-10-20 04:21:00 +00:00
xhr.addEventListener('error', () => reject('failed to execute network request'));
2016-10-17 04:45:41 +00:00
xhr.addEventListener('load', () => resolve(xhr.responseText));
2016-12-29 19:10:12 +00:00
if (dataEnc) {
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(dataEnc);
} else {
xhr.open('GET', url);
xhr.send();
}
2016-10-12 03:54:54 +00:00
});
}
}