cleanup
This commit is contained in:
parent
62db3d74b8
commit
fe137e94c9
@ -14,6 +14,7 @@
|
|||||||
<script src="/bg/js/handlebars.js"></script>
|
<script src="/bg/js/handlebars.js"></script>
|
||||||
<script src="/bg/js/dictionary.js"></script>
|
<script src="/bg/js/dictionary.js"></script>
|
||||||
<script src="/mixed/js/japanese.js"></script>
|
<script src="/mixed/js/japanese.js"></script>
|
||||||
|
<script src="/mixed/js/request.js"></script>
|
||||||
<script src="/bg/js/options.js"></script>
|
<script src="/bg/js/options.js"></script>
|
||||||
<script src="/bg/js/anki-connect.js"></script>
|
<script src="/bg/js/anki-connect.js"></script>
|
||||||
<script src="/bg/js/anki-null.js"></script>
|
<script src="/bg/js/anki-null.js"></script>
|
||||||
|
@ -64,6 +64,6 @@ class AnkiConnect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ankiInvoke(action, params) {
|
ankiInvoke(action, params) {
|
||||||
return jsonRequest(this.server, 'POST', {action, params, version: this.localVersion});
|
return requestJson(this.server, 'POST', {action, params, version: this.localVersion});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ class Translator {
|
|||||||
|
|
||||||
if (!this.deinflector) {
|
if (!this.deinflector) {
|
||||||
const url = chrome.extension.getURL('/bg/lang/deinflect.json');
|
const url = chrome.extension.getURL('/bg/lang/deinflect.json');
|
||||||
const reasons = await jsonRequest(url, 'GET');
|
const reasons = await requestJson(url, 'GET');
|
||||||
this.deinflector = new Deinflector(reasons);
|
this.deinflector = new Deinflector(reasons);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,19 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Promise
|
|
||||||
*/
|
|
||||||
|
|
||||||
function promiseCallback(promise, callback) {
|
|
||||||
return promise.then(result => {
|
|
||||||
callback({result});
|
|
||||||
}).catch(error => {
|
|
||||||
callback({error});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Commands
|
* Commands
|
||||||
*/
|
*/
|
||||||
@ -71,30 +58,3 @@ function fgBroadcast(action, params) {
|
|||||||
function fgOptionsSet(options) {
|
function fgOptionsSet(options) {
|
||||||
fgBroadcast('optionsSet', options);
|
fgBroadcast('optionsSet', options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* JSON
|
|
||||||
*/
|
|
||||||
|
|
||||||
function jsonRequest(url, action, params) {
|
|
||||||
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(action, url);
|
|
||||||
if (params) {
|
|
||||||
xhr.send(JSON.stringify(params));
|
|
||||||
} else {
|
|
||||||
xhr.send();
|
|
||||||
}
|
|
||||||
}).then(responseText => {
|
|
||||||
try {
|
|
||||||
return JSON.parse(responseText);
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
return Promise.reject('invalid JSON response');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
@ -195,6 +195,14 @@ window.yomichan = new class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMessage({action, params}, sender, callback) {
|
onMessage({action, params}, sender, callback) {
|
||||||
|
const promiseCallback = (promise, callback) => {
|
||||||
|
return promise.then(result => {
|
||||||
|
callback({result});
|
||||||
|
}).catch(error => {
|
||||||
|
callback({error});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const handlers = {
|
const handlers = {
|
||||||
optionsGet: ({callback}) => {
|
optionsGet: ({callback}) => {
|
||||||
promiseCallback(optionsLoad(), callback);
|
promiseCallback(optionsLoad(), callback);
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
<script src="/mixed/lib/handlebars.min.js"></script>
|
<script src="/mixed/lib/handlebars.min.js"></script>
|
||||||
<script src="/bg/js/util.js"></script>
|
<script src="/bg/js/util.js"></script>
|
||||||
<script src="/bg/js/dictionary.js"></script>
|
<script src="/bg/js/dictionary.js"></script>
|
||||||
|
<script src="/mixed/js/request.js"></script>
|
||||||
<script src="/mixed/js/japanese.js"></script>
|
<script src="/mixed/js/japanese.js"></script>
|
||||||
<script src="/bg/js/options.js"></script>
|
<script src="/bg/js/options.js"></script>
|
||||||
<script src="/bg/js/popup.js"></script>
|
<script src="/bg/js/popup.js"></script>
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
<script src="/mixed/lib/jquery.min.js"></script>
|
<script src="/mixed/lib/jquery.min.js"></script>
|
||||||
<script src="/bg/js/util.js"></script>
|
<script src="/bg/js/util.js"></script>
|
||||||
<script src="/bg/js/dictionary.js"></script>
|
<script src="/bg/js/dictionary.js"></script>
|
||||||
|
<script src="/mixed/js/request.js"></script>
|
||||||
<script src="/mixed/js/japanese.js"></script>
|
<script src="/mixed/js/japanese.js"></script>
|
||||||
<script src="/mixed/js/audio.js"></script>
|
<script src="/mixed/js/audio.js"></script>
|
||||||
<script src="/mixed/js/display.js"></script>
|
<script src="/mixed/js/display.js"></script>
|
||||||
|
@ -280,6 +280,7 @@
|
|||||||
<script src="/bg/js/handlebars.js"></script>
|
<script src="/bg/js/handlebars.js"></script>
|
||||||
<script src="/bg/js/dictionary.js"></script>
|
<script src="/bg/js/dictionary.js"></script>
|
||||||
<script src="/mixed/js/japanese.js"></script>
|
<script src="/mixed/js/japanese.js"></script>
|
||||||
|
<script src="/mixed/js/request.js"></script>
|
||||||
<script src="/bg/js/options.js"></script>
|
<script src="/bg/js/options.js"></script>
|
||||||
<script src="/bg/js/settings.js"></script>
|
<script src="/bg/js/settings.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
40
ext/mixed/js/request.js
Normal file
40
ext/mixed/js/request.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
function requestJson(url, action, params) {
|
||||||
|
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(action, url);
|
||||||
|
if (params) {
|
||||||
|
xhr.send(JSON.stringify(params));
|
||||||
|
} else {
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
}).then(responseText => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(responseText);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return Promise.reject('invalid JSON response');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user