Improved login behavior
This commit is contained in:
parent
f2c8d7d04e
commit
bc8ef56cc2
@ -21,7 +21,6 @@ class AnkiWeb {
|
|||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.noteInfo = null;
|
this.noteInfo = null;
|
||||||
this.logged = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addNote(note) {
|
addNote(note) {
|
||||||
@ -52,96 +51,75 @@ class AnkiWeb {
|
|||||||
return Promise.resolve(this.noteInfo);
|
return Promise.resolve(this.noteInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.authenticate().then(() => {
|
return AnkiWeb.scrape(this.username, this.password).then(({deckNames, models}) => {
|
||||||
return AnkiWeb.scrape();
|
|
||||||
}).then(({deckNames, models}) => {
|
|
||||||
this.noteInfo = {deckNames, models};
|
this.noteInfo = {deckNames, models};
|
||||||
return this.noteInfo;
|
return this.noteInfo;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate() {
|
static scrape(username, password) {
|
||||||
if (this.username.length === 0 || this.password.length === 0) {
|
return AnkiWeb.loadAccountPage('https://ankiweb.net/edit/', 'GET', null, username, password).then(response => {
|
||||||
return Promise.reject('missing login credentials');
|
const modelsMatch = /editor\.models = (.*}]);/.exec(response);
|
||||||
}
|
if (modelsMatch === null) {
|
||||||
|
return Promise.reject('failed to scrape model data');
|
||||||
|
}
|
||||||
|
|
||||||
if (this.logged) {
|
const decksMatch = /editor\.decks = (.*}});/.exec(response);
|
||||||
return Promise.resolve(true);
|
if (decksMatch === null) {
|
||||||
}
|
return Promise.reject('failed to scrape deck data');
|
||||||
|
}
|
||||||
|
|
||||||
return AnkiWeb.logout().then(() => {
|
const modelsJson = JSON.parse(modelsMatch[1]);
|
||||||
return AnkiWeb.login(this.username, this.password);
|
const decksJson = JSON.parse(decksMatch[1]);
|
||||||
}).then(() => {
|
|
||||||
this.logged = true;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
static scrape() {
|
const deckNames = Object.keys(decksJson).map(d => decksJson[d].name);
|
||||||
return new Promise((resolve, reject) => {
|
const models = [];
|
||||||
const xhr = new XMLHttpRequest();
|
for (const modelJson of modelsJson) {
|
||||||
xhr.addEventListener('error', () => reject('failed to execute scrape request'));
|
models.push({
|
||||||
xhr.addEventListener('load', () => {
|
name: modelJson.name,
|
||||||
const modelsJson = JSON.parse(/editor\.models = (.*}]);/.exec(xhr.responseText)[1]);
|
id: modelJson.id,
|
||||||
if (!modelsJson) {
|
fields: modelJson.flds.map(f => f.name)
|
||||||
reject('failed to scrape model data');
|
});
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const decksJson = JSON.parse(/editor\.decks = (.*}});/.exec(xhr.responseText)[1]);
|
return {deckNames, models};
|
||||||
if (!decksJson) {
|
|
||||||
reject('failed to scrape deck data');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve({deckNames, models});
|
|
||||||
});
|
|
||||||
|
|
||||||
xhr.open('GET', 'https://ankiweb.net/edit/');
|
|
||||||
xhr.send();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static login(username, password) {
|
static login(username, password) {
|
||||||
|
if (username.length === 0 || password.length === 0) {
|
||||||
|
return Promise.reject('unspecified login credentials');
|
||||||
|
}
|
||||||
|
|
||||||
|
const form = new FormData();
|
||||||
|
form.append('username', username);
|
||||||
|
form.append('password', password);
|
||||||
|
form.append('submitted', 1);
|
||||||
|
|
||||||
|
return AnkiWeb.loadPage('https://ankiweb.net/account/login', 'POST', form).then(response => {
|
||||||
|
if (!response.includes('class="mitem"')) {
|
||||||
|
return Promise.reject('failed to authenticate');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static loadAccountPage(url, method, form, username, password) {
|
||||||
|
return AnkiWeb.loadPage(url, method, form).then(response => {
|
||||||
|
if (response.includes('name="password"')) {
|
||||||
|
return AnkiWeb.login(username, password).then(() => AnkiWeb.loadPage(url, method, form));
|
||||||
|
} else {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static loadPage(url, method, form) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.addEventListener('error', () => reject('failed to execute login request'));
|
xhr.addEventListener('error', () => reject('failed to execute request'));
|
||||||
xhr.addEventListener('load', () => {
|
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
||||||
if (xhr.responseText.includes('class="mitem"')) {
|
xhr.open(method, url);
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject('failed to authenticate');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const form = new FormData();
|
|
||||||
form.append('username', username);
|
|
||||||
form.append('password', password);
|
|
||||||
form.append('submitted', 1);
|
|
||||||
|
|
||||||
xhr.open('POST', 'https://ankiweb.net/account/login');
|
|
||||||
xhr.send(form);
|
xhr.send(form);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static logout() {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
xhr.addEventListener('error', () => reject('failed to execute logout request'));
|
|
||||||
xhr.addEventListener('load', () => resolve());
|
|
||||||
|
|
||||||
xhr.open('GET', 'https://ankiweb.net/account/logout');
|
|
||||||
xhr.send();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user