2017-08-06 02:23:17 +00:00
|
|
|
/*
|
2017-08-15 04:43:09 +00:00
|
|
|
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
|
2017-08-06 02:23:17 +00:00
|
|
|
* 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-08-15 04:43:09 +00:00
|
|
|
function utilAsync(func) {
|
|
|
|
return function(...args) {
|
|
|
|
func.apply(this, args);
|
|
|
|
};
|
|
|
|
}
|
2017-08-06 02:23:17 +00:00
|
|
|
|
2017-08-16 04:51:48 +00:00
|
|
|
function utilIsolate(data) {
|
|
|
|
return JSON.parse(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
2019-09-14 20:21:41 +00:00
|
|
|
function utilBackgroundIsolate(data) {
|
|
|
|
const backgroundPage = chrome.extension.getBackgroundPage();
|
|
|
|
return backgroundPage.utilIsolate(data);
|
|
|
|
}
|
|
|
|
|
2017-10-03 04:20:02 +00:00
|
|
|
function utilSetEqual(setA, setB) {
|
|
|
|
if (setA.size !== setB.size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const value of setA) {
|
|
|
|
if (!setB.has(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function utilSetIntersection(setA, setB) {
|
|
|
|
return new Set(
|
|
|
|
[...setA].filter(value => setB.has(value))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function utilSetDifference(setA, setB) {
|
|
|
|
return new Set(
|
|
|
|
[...setA].filter(value => !setB.has(value))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:40:56 +00:00
|
|
|
function utilStringHashCode(string) {
|
|
|
|
let hashCode = 0;
|
|
|
|
|
2017-10-18 21:47:08 +00:00
|
|
|
for (let i = 0, charCode = string.charCodeAt(i); i < string.length; charCode = string.charCodeAt(++i)) {
|
2017-10-18 20:40:56 +00:00
|
|
|
hashCode = ((hashCode << 5) - hashCode) + charCode;
|
|
|
|
hashCode |= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
}
|
|
|
|
|
2017-08-06 02:23:17 +00:00
|
|
|
function utilBackend() {
|
|
|
|
return chrome.extension.getBackgroundPage().yomichan_backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
function utilAnkiGetModelNames() {
|
|
|
|
return utilBackend().anki.getModelNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
function utilAnkiGetDeckNames() {
|
|
|
|
return utilBackend().anki.getDeckNames();
|
|
|
|
}
|
|
|
|
|
2017-10-29 17:42:39 +00:00
|
|
|
function utilDatabaseSummarize() {
|
|
|
|
return utilBackend().translator.database.summarize();
|
2017-08-06 02:23:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 17:42:39 +00:00
|
|
|
function utilAnkiGetModelFieldNames(modelName) {
|
|
|
|
return utilBackend().anki.getModelFieldNames(modelName);
|
2017-10-24 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 02:23:17 +00:00
|
|
|
function utilDatabasePurge() {
|
|
|
|
return utilBackend().translator.database.purge();
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:44:31 +00:00
|
|
|
async function utilDatabaseImport(data, progress, exceptions) {
|
|
|
|
// Edge cannot read data on the background page due to the File object
|
|
|
|
// being created from a different window. Read on the same page instead.
|
|
|
|
if (EXTENSION_IS_BROWSER_EDGE) {
|
|
|
|
data = await utilReadFile(data);
|
|
|
|
}
|
2019-02-27 02:01:32 +00:00
|
|
|
return utilBackend().translator.database.importDictionary(data, progress, exceptions);
|
2017-08-06 02:23:17 +00:00
|
|
|
}
|
2019-08-22 23:44:31 +00:00
|
|
|
|
|
|
|
function utilReadFile(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => resolve(reader.result);
|
|
|
|
reader.onerror = () => reject(reader.error);
|
|
|
|
reader.readAsBinaryString(file);
|
|
|
|
});
|
|
|
|
}
|
2019-09-07 21:35:33 +00:00
|
|
|
|
|
|
|
function utilIsObject(value) {
|
|
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
|
|
}
|