This commit is contained in:
Alex Yatskov 2017-07-10 15:00:38 -07:00
parent f694026827
commit 28bc1449d1
2 changed files with 21 additions and 27 deletions

View File

@ -30,7 +30,8 @@ class Translator {
}
if (!this.deinflector) {
const reasons = await jsonLoadInt('/bg/lang/deinflect.json');
const url = chrome.extension.getURL('/bg/lang/deinflect.json');
const reasons = await Translator.loadRules(url);
this.deinflector = new Deinflector(reasons);
}
}
@ -127,4 +128,23 @@ class Translator {
return definitions;
}
static loadRules(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.addEventListener('error', () => reject('failed to execute network request'));
xhr.open('GET', url);
xhr.send();
}).then(responseText => {
try {
return JSON.parse(responseText);
}
catch (e) {
return Promise.reject('invalid JSON response');
}
});
}
}

View File

@ -429,32 +429,6 @@ function dictFieldFormat(field, definition, mode, options) {
}
/*
* Json
*/
function jsonLoad(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.addEventListener('error', () => reject('failed to execute network request'));
xhr.open('GET', url);
xhr.send();
}).then(responseText => {
try {
return JSON.parse(responseText);
}
catch (e) {
return Promise.reject('invalid JSON response');
}
});
}
function jsonLoadInt(url) {
return jsonLoad(chrome.extension.getURL(url));
}
/*
* Helpers
*/