yomichan/ext/bg/js/util.js

142 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-08-24 05:28:37 +00:00
/*
* Copyright (C) 2016 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/>.
*/
2017-03-03 05:01:49 +00:00
/*
* Promise
*/
2017-01-10 03:42:54 +00:00
2016-10-22 03:08:16 +00:00
function promiseCallback(promise, callback) {
return promise.then(result => {
2017-03-05 03:16:19 +00:00
callback({result});
2016-10-22 03:08:16 +00:00
}).catch(error => {
callback({error});
});
}
2017-03-03 05:01:49 +00:00
2017-03-18 20:00:29 +00:00
/*
* Commands
*/
function commandExec(command) {
instYomi().onCommand(command);
}
2017-03-03 05:01:49 +00:00
/*
* Instance
*/
function instYomi() {
2017-03-03 04:23:56 +00:00
return chrome.extension.getBackgroundPage().yomichan;
}
2017-03-03 05:01:49 +00:00
function instDb() {
return instYomi().translator.database;
2017-03-03 04:23:56 +00:00
}
2017-03-03 05:01:49 +00:00
function instAnki() {
return instYomi().anki;
2017-03-03 04:23:56 +00:00
}
2017-03-03 05:01:49 +00:00
2017-03-05 03:16:19 +00:00
/*
* Foreground
*/
function fgBroadcast(action, params) {
chrome.tabs.query({}, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, {action, params}, () => null);
}
});
}
function fgOptionsSet(options) {
fgBroadcast('optionsSet', options);
}
2017-03-03 05:01:49 +00:00
2017-07-10 23:24:31 +00:00
/*
* 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');
}
});
}
2017-03-03 05:01:49 +00:00
/*
* Helpers
*/
2017-05-25 02:13:56 +00:00
function handlebarsEscape(text) {
return Handlebars.Utils.escapeExpression(text);
}
2017-05-24 04:27:20 +00:00
function handlebarsDumpObject(options) {
const dump = JSON.stringify(options.fn(this), null, 4);
2017-05-25 02:13:56 +00:00
return handlebarsEscape(dump);
2017-05-24 04:27:20 +00:00
}
2017-03-05 03:16:19 +00:00
function handlebarsKanjiLinks(options) {
2017-03-03 05:01:49 +00:00
let result = '';
for (const c of options.fn(this)) {
if (jpIsKanji(c)) {
2017-03-03 05:01:49 +00:00
result += `<a href="#" class="kanji-link">${c}</a>`;
} else {
result += c;
}
}
return result;
}
2017-03-05 03:16:19 +00:00
function handlebarsMultiLine(options) {
2017-03-03 05:01:49 +00:00
return options.fn(this).split('\n').join('<br>');
}
2017-03-05 03:16:19 +00:00
function handlebarsRegister() {
Handlebars.partials = Handlebars.templates;
2017-05-24 04:27:20 +00:00
Handlebars.registerHelper('dumpObject', handlebarsDumpObject);
2017-03-05 03:16:19 +00:00
Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks);
Handlebars.registerHelper('multiLine', handlebarsMultiLine);
}
function handlebarsRender(template, data) {
return Handlebars.templates[template](data);
}