This commit is contained in:
Alex Yatskov 2016-10-15 19:51:41 -07:00
parent 49f98a8c53
commit 5057d6b6cf
3 changed files with 27 additions and 23 deletions

View File

@ -2,7 +2,6 @@
<html lang="en"> <html lang="en">
<body> <body>
<script src="../lib/handlebars.min.js"></script> <script src="../lib/handlebars.min.js"></script>
<script src="../lib/jquery-2.2.2.min.js"></script>
<script src="../lib/dexie.min.js"></script> <script src="../lib/dexie.min.js"></script>
<script src="js/ankiweb.js"></script> <script src="js/ankiweb.js"></script>
<script src="js/ankiconnect.js"></script> <script src="js/ankiconnect.js"></script>

View File

@ -75,12 +75,9 @@ class AnkiWeb {
static scrape() { static scrape() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.get('https://ankiweb.net/edit/', (data, status) => { const xhr = new XMLHttpRequest();
if (status !== 'success') { xhr.addEventListener('error', () => reject('failed to execute scrape request'));
reject('failed to execute scrape request'); xhr.addEventListener('load', () => {
return;
}
const modelsJson = JSON.parse(/editor\.models = (.*}]);/.exec(data)[1]); const modelsJson = JSON.parse(/editor\.models = (.*}]);/.exec(data)[1]);
if (!modelsJson) { if (!modelsJson) {
reject('failed to scrape model data'); reject('failed to scrape model data');
@ -105,34 +102,42 @@ class AnkiWeb {
resolve({deckNames, models}); resolve({deckNames, models});
}); });
xhr.open('GET', 'https://ankiweb.net/edit/');
xhr.send();
}); });
} }
static login(username, password) { static login(username, password) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.post('https://ankiweb.net/account/login', {username, password, submitted: 1}, (data, status) => { const xhr = new XMLHttpRequest();
if (status === 'success') { xhr.addEventListener('error', () => reject('failed to execute login request'));
if (data.includes('class="mitem"')) { xhr.addEventListener('success', () => {
resolve(); if (xhr.responseText.includes('class="mitem"')) {
} else { resolve();
reject('failed to authenticate');
}
} else { } else {
reject('failed to execute login request'); 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);
}); });
} }
static logout() { static logout() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
$.get('https://ankiweb.net/account/logout', (data, status) => { const xhr = new XMLHttpRequest();
if (status === 'success') { xhr.addEventListener('error', () => reject('failed to execute logout request'));
resolve(); xhr.addEventListener('load', () => resolve());
} else {
reject('failed to execute logout request'); xhr.open('GET', 'https://ankiweb.net/account/logout');
} xhr.send();
});
}); });
} }
} }

View File

@ -34,7 +34,7 @@ function loadJson(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText))); xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
xhr.open('GET', chrome.extension.getURL(url), true); xhr.open('GET', chrome.extension.getURL(url));
xhr.send(); xhr.send();
}); });
} }