fixing ankiweb breakage (resolves #26)

This commit is contained in:
Alex Yatskov 2016-12-29 09:36:54 -08:00
parent 1e6feff207
commit c185125e9f
2 changed files with 22 additions and 4 deletions

View File

@ -21,6 +21,15 @@ class AnkiWeb {
this.username = username; this.username = username;
this.password = password; this.password = password;
this.noteInfo = null; this.noteInfo = null;
chrome.webRequest.onBeforeSendHeaders.addListener(
details => {
details.requestHeaders.push({name: 'Origin', value: 'https://ankiweb.net'});
return {requestHeaders: details.requestHeaders};
},
{urls: ['https://ankiweb.net/*']},
['blocking', 'requestHeaders']
);
} }
addNote(note) { addNote(note) {
@ -133,20 +142,22 @@ class AnkiWeb {
static loadPage(url, data) { static loadPage(url, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let dataEnc = null;
if (data) { if (data) {
const params = []; const params = [];
for (const key in data) { for (const key in data) {
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`); params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`);
} }
url += '?' + params.join('&'); dataEnc = params.join('&');
} }
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.addEventListener('error', () => reject('failed to execute network request')); xhr.addEventListener('error', () => reject('failed to execute network request'));
xhr.addEventListener('load', () => resolve(xhr.responseText)); xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.open('GET', url); xhr.open('POST', url);
xhr.send(); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(dataEnc);
}); });
} }
} }

View File

@ -24,7 +24,14 @@
"options_ui": { "options_ui": {
"page": "bg/options.html" "page": "bg/options.html"
}, },
"permissions": ["file://*/*", "http://127.0.0.1/*", "storage"], "permissions": [
"webRequest",
"webRequestBlocking",
"file://*",
"http://127.0.0.1/*",
"https://ankiweb.net/*",
"storage"
],
"web_accessible_resources": [ "web_accessible_resources": [
"fg/css/frame.css", "fg/css/frame.css",
"fg/img/add_kanji.png", "fg/img/add_kanji.png",