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 => {
|
2017-01-15 20:03:27 +00:00
|
|
|
/* eslint-disable */
|
2016-11-13 03:29:30 +00:00
|
|
|
console.log(error);
|
2017-01-15 20:03:27 +00:00
|
|
|
/* eslint-enable */
|
2016-10-22 03:08:16 +00:00
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Options
|
|
|
|
*/
|
|
|
|
|
|
|
|
function optionsSetDefaults(options) {
|
|
|
|
const defaults = {
|
|
|
|
general: {
|
|
|
|
enable: true,
|
|
|
|
audioPlayback: true,
|
|
|
|
groupResults: true,
|
|
|
|
softKatakana: true,
|
|
|
|
maxResults: 32,
|
2017-03-12 01:48:30 +00:00
|
|
|
showAdvanced: false,
|
|
|
|
popupWidth: 400,
|
|
|
|
popupHeight: 250,
|
|
|
|
popupOffset: 10
|
2017-03-03 05:01:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
scanning: {
|
|
|
|
requireShift: true,
|
2017-03-06 00:03:14 +00:00
|
|
|
middleMouse: true,
|
2017-03-03 05:01:49 +00:00
|
|
|
selectText: true,
|
|
|
|
imposter: true,
|
|
|
|
delay: 15,
|
|
|
|
length: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
dictionaries: {},
|
|
|
|
|
|
|
|
anki: {
|
|
|
|
enable: false,
|
|
|
|
server: 'http://127.0.0.1:8765',
|
|
|
|
tags: ['yomichan'],
|
|
|
|
htmlCards: true,
|
|
|
|
sentenceExt: 200,
|
|
|
|
terms: {deck: '', model: '', fields: {}},
|
|
|
|
kanji: {deck: '', model: '', fields: {}}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const combine = (target, source) => {
|
|
|
|
for (const key in source) {
|
|
|
|
if (!target.hasOwnProperty(key)) {
|
|
|
|
target[key] = source[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
combine(options, defaults);
|
|
|
|
combine(options.general, defaults.general);
|
|
|
|
combine(options.scanning, defaults.scanning);
|
|
|
|
combine(options.anki, defaults.anki);
|
|
|
|
combine(options.anki.terms, defaults.anki.terms);
|
|
|
|
combine(options.anki.kanji, defaults.anki.kanji);
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionsVersion(options) {
|
|
|
|
const fixups = [
|
|
|
|
() => {
|
|
|
|
const copy = (targetDict, targetKey, sourceDict, sourceKey) => {
|
|
|
|
targetDict[targetKey] = sourceDict.hasOwnProperty(sourceKey) ? sourceDict[sourceKey] : targetDict[targetKey];
|
|
|
|
};
|
|
|
|
|
|
|
|
copy(options.general, 'autoStart', options, 'activateOnStartup');
|
|
|
|
copy(options.general, 'audioPlayback', options, 'enableAudioPlayback');
|
|
|
|
copy(options.general, 'softKatakana', options, 'enableSoftKatakanaSearch');
|
|
|
|
copy(options.general, 'groupResults', options, 'groupTermResults');
|
|
|
|
copy(options.general, 'showAdvanced', options, 'showAdvancedOptions');
|
|
|
|
|
|
|
|
copy(options.scanning, 'requireShift', options, 'holdShiftToScan');
|
|
|
|
copy(options.scanning, 'selectText', options, 'selectMatchedText');
|
|
|
|
copy(options.scanning, 'delay', options, 'scanDelay');
|
|
|
|
copy(options.scanning, 'length', options, 'scanLength');
|
|
|
|
|
|
|
|
options.anki.enable = options.ankiMethod === 'ankiconnect';
|
|
|
|
|
|
|
|
copy(options.anki, 'tags', options, 'ankiCardTags');
|
|
|
|
copy(options.anki, 'sentenceExt', options, 'sentenceExtent');
|
|
|
|
copy(options.anki.terms, 'deck', options, 'ankiTermDeck');
|
|
|
|
copy(options.anki.terms, 'model', options, 'ankiTermModel');
|
|
|
|
copy(options.anki.terms, 'fields', options, 'ankiTermFields');
|
|
|
|
copy(options.anki.kanji, 'deck', options, 'ankiKanjiDeck');
|
|
|
|
copy(options.anki.kanji, 'model', options, 'ankiKanjiModel');
|
|
|
|
copy(options.anki.kanji, 'fields', options, 'ankiKanjiFields');
|
|
|
|
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
const dictionary = options.dictionaries[title];
|
|
|
|
dictionary.enabled = dictionary.enableTerms || dictionary.enableKanji;
|
|
|
|
dictionary.priority = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
const fixupFields = fields => {
|
|
|
|
const fixups = {
|
|
|
|
'{expression-furigana}': '{furigana}',
|
|
|
|
'{glossary-list}': '{glossary}'
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const name in fields) {
|
|
|
|
for (const fixup in fixups) {
|
|
|
|
fields[name] = fields[name].replace(fixup, fixups[fixup]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fixupFields(options.anki.terms.fields);
|
|
|
|
fixupFields(options.anki.kanji.fields);
|
2017-03-05 19:39:07 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
let hasEnabledDict = false;
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
if (options.dictionaries[title].enabled) {
|
|
|
|
hasEnabledDict = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:18:40 +00:00
|
|
|
if (!hasEnabledDict) {
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
options.dictionaries[title].enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
let hasEnabledDict = false;
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
if (options.dictionaries[title].enabled) {
|
|
|
|
hasEnabledDict = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 19:39:07 +00:00
|
|
|
if (!hasEnabledDict) {
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
options.dictionaries[title].enabled = true;
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
optionsSetDefaults(options);
|
|
|
|
if (!options.hasOwnProperty('version')) {
|
|
|
|
options.version = fixups.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (options.version < fixups.length) {
|
|
|
|
fixups[options.version++]();
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionsLoad() {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-03-05 19:29:28 +00:00
|
|
|
chrome.storage.local.get(null, store => resolve(store.options));
|
|
|
|
}).then(optionsStr => {
|
2017-03-12 02:39:01 +00:00
|
|
|
return optionsStr ? JSON.parse(optionsStr) : {};
|
2017-03-05 19:29:28 +00:00
|
|
|
}).catch(error => {
|
|
|
|
return {};
|
2017-03-05 04:56:54 +00:00
|
|
|
}).then(options => {
|
2017-03-05 19:29:28 +00:00
|
|
|
return optionsVersion(options);
|
2017-03-03 05:01:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function optionsSave(options) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-03-05 19:12:48 +00:00
|
|
|
chrome.storage.local.set({options: JSON.stringify(options)}, resolve);
|
2017-03-05 03:16:19 +00:00
|
|
|
}).then(() => {
|
|
|
|
instYomi().optionsSet(options);
|
|
|
|
fgOptionsSet(options);
|
2017-03-03 05:01:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dictionary
|
|
|
|
*/
|
|
|
|
|
|
|
|
function dictEnabled(options) {
|
|
|
|
const dictionaries = {};
|
|
|
|
for (const title in options.dictionaries) {
|
|
|
|
const dictionary = options.dictionaries[title];
|
|
|
|
if (dictionary.enabled) {
|
|
|
|
dictionaries[title] = dictionary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dictionaries;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:54:03 +00:00
|
|
|
function dictRowsSort(rows, options) {
|
|
|
|
return rows.sort((ra, rb) => {
|
2017-03-06 00:27:29 +00:00
|
|
|
const pa = (options.dictionaries[ra.title] || {}).priority || 0;
|
|
|
|
const pb = (options.dictionaries[rb.title] || {}).priority || 0;
|
2017-03-05 23:54:03 +00:00
|
|
|
if (pa > pb) {
|
|
|
|
return -1;
|
|
|
|
} else if (pa < pb) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTermsSort(definitions, dictionaries=null) {
|
2016-09-20 03:17:58 +00:00
|
|
|
return definitions.sort((v1, v2) => {
|
|
|
|
const sl1 = v1.source.length;
|
|
|
|
const sl2 = v2.source.length;
|
|
|
|
if (sl1 > sl2) {
|
|
|
|
return -1;
|
|
|
|
} else if (sl1 < sl2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-17 04:54:01 +00:00
|
|
|
if (dictionaries !== null) {
|
|
|
|
const p1 = (dictionaries[v1.dictionary] || {}).priority || 0;
|
|
|
|
const p2 = (dictionaries[v2.dictionary] || {}).priority || 0;
|
|
|
|
if (p1 > p2) {
|
|
|
|
return -1;
|
|
|
|
} else if (p1 < p2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 03:17:58 +00:00
|
|
|
const s1 = v1.score;
|
|
|
|
const s2 = v2.score;
|
|
|
|
if (s1 > s2) {
|
|
|
|
return -1;
|
|
|
|
} else if (s1 < s2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-12-18 02:45:19 +00:00
|
|
|
const rl1 = v1.reasons.length;
|
|
|
|
const rl2 = v2.reasons.length;
|
2016-09-20 03:17:58 +00:00
|
|
|
if (rl1 < rl2) {
|
|
|
|
return -1;
|
|
|
|
} else if (rl1 > rl2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v2.expression.localeCompare(v1.expression);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTermsUndupe(definitions) {
|
2016-12-23 23:44:16 +00:00
|
|
|
const definitionGroups = {};
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const definition of definitions) {
|
2016-12-23 23:44:16 +00:00
|
|
|
const definitionExisting = definitionGroups[definition.id];
|
|
|
|
if (!definitionGroups.hasOwnProperty(definition.id) || definition.expression.length > definitionExisting.expression.length) {
|
|
|
|
definitionGroups[definition.id] = definition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const definitionsUnique = [];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const key in definitionGroups) {
|
2016-12-23 23:44:16 +00:00
|
|
|
definitionsUnique.push(definitionGroups[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return definitionsUnique;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTermsGroup(definitions, dictionaries) {
|
2017-01-08 23:08:18 +00:00
|
|
|
const groups = {};
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const definition of definitions) {
|
2017-01-08 23:08:18 +00:00
|
|
|
const key = [definition.source, definition.expression].concat(definition.reasons);
|
|
|
|
if (definition.reading) {
|
|
|
|
key.push(definition.reading);
|
|
|
|
}
|
|
|
|
|
|
|
|
const group = groups[key];
|
|
|
|
if (group) {
|
|
|
|
group.push(definition);
|
|
|
|
} else {
|
|
|
|
groups[key] = [definition];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = [];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const key in groups) {
|
2017-01-09 02:10:38 +00:00
|
|
|
const groupDefs = groups[key];
|
2017-01-08 23:08:18 +00:00
|
|
|
const firstDef = groupDefs[0];
|
2017-03-03 05:01:49 +00:00
|
|
|
dictTermsSort(groupDefs, dictionaries);
|
2017-01-08 23:08:18 +00:00
|
|
|
results.push({
|
|
|
|
definitions: groupDefs,
|
|
|
|
expression: firstDef.expression,
|
|
|
|
reading: firstDef.reading,
|
|
|
|
reasons: firstDef.reasons,
|
2017-01-17 04:54:01 +00:00
|
|
|
score: groupDefs.reduce((x, y) => x.score > y.score ? x.score : y.score, Number.MIN_SAFE_INTEGER),
|
2017-01-15 20:42:44 +00:00
|
|
|
source: firstDef.source
|
2017-01-08 23:08:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
return dictTermsSort(results);
|
2017-01-08 23:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagBuildSource(name) {
|
|
|
|
return dictTagSanitize({name, category: 'dictionary', order: 100});
|
2017-01-08 19:18:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagBuild(name, meta) {
|
2016-12-18 05:26:46 +00:00
|
|
|
const tag = {name};
|
|
|
|
const symbol = name.split(':')[0];
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const prop in meta[symbol] || {}) {
|
2016-09-20 03:17:58 +00:00
|
|
|
tag[prop] = meta[symbol][prop];
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
return dictTagSanitize(tag);
|
2016-12-18 19:52:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagSanitize(tag) {
|
2016-12-18 19:52:11 +00:00
|
|
|
tag.name = tag.name || 'untitled';
|
|
|
|
tag.category = tag.category || 'default';
|
|
|
|
tag.notes = tag.notes || '';
|
|
|
|
tag.order = tag.order || 0;
|
2016-09-20 03:17:58 +00:00
|
|
|
return tag;
|
|
|
|
}
|
2016-10-22 04:33:54 +00:00
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictTagsSort(tags) {
|
2017-01-08 22:08:36 +00:00
|
|
|
return tags.sort((v1, v2) => {
|
|
|
|
const order1 = v1.order;
|
|
|
|
const order2 = v2.order;
|
|
|
|
if (order1 < order2) {
|
|
|
|
return -1;
|
|
|
|
} else if (order1 > order2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name1 = v1.name;
|
|
|
|
const name2 = v2.name;
|
|
|
|
if (name1 < name2) {
|
|
|
|
return -1;
|
|
|
|
} else if (name1 > name2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function dictFieldSplit(field) {
|
|
|
|
return field.length === 0 ? [] : field.split(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function dictFieldFormat(field, definition, mode, options) {
|
2017-01-08 22:08:36 +00:00
|
|
|
const markers = [
|
|
|
|
'audio',
|
|
|
|
'character',
|
|
|
|
'dictionary',
|
|
|
|
'expression',
|
2017-01-22 03:30:01 +00:00
|
|
|
'furigana',
|
2017-01-08 22:08:36 +00:00
|
|
|
'glossary',
|
|
|
|
'kunyomi',
|
|
|
|
'onyomi',
|
|
|
|
'reading',
|
|
|
|
'sentence',
|
|
|
|
'tags',
|
2017-01-15 20:42:44 +00:00
|
|
|
'url'
|
2017-01-08 22:08:36 +00:00
|
|
|
];
|
|
|
|
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const marker of markers) {
|
2017-01-22 03:30:01 +00:00
|
|
|
const data = {
|
|
|
|
marker,
|
|
|
|
definition,
|
2017-01-27 04:22:52 +00:00
|
|
|
group: options.general.groupResults,
|
|
|
|
html: options.anki.htmlCards,
|
2017-03-05 03:53:28 +00:00
|
|
|
modeTermKanji: mode === 'term-kanji',
|
|
|
|
modeTermKana: mode === 'term-kana',
|
2017-01-22 03:30:01 +00:00
|
|
|
modeKanji: mode === 'kanji'
|
|
|
|
};
|
|
|
|
|
|
|
|
field = field.replace(
|
|
|
|
`{${marker}}`,
|
|
|
|
Handlebars.templates['fields.html'](data).trim()
|
|
|
|
);
|
2017-01-08 22:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Json
|
|
|
|
*/
|
|
|
|
|
|
|
|
function jsonLoad(url) {
|
2016-11-06 19:01:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
2017-01-29 04:36:58 +00:00
|
|
|
xhr.overrideMimeType('application/json');
|
2016-11-08 06:31:57 +00:00
|
|
|
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
2016-11-07 17:16:38 +00:00
|
|
|
xhr.addEventListener('error', () => reject('failed to execute network request'));
|
2016-11-07 17:22:57 +00:00
|
|
|
xhr.open('GET', url);
|
2016-11-06 19:01:35 +00:00
|
|
|
xhr.send();
|
2016-11-08 06:31:57 +00:00
|
|
|
}).then(responseText => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(responseText);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return Promise.reject('invalid JSON response');
|
|
|
|
}
|
2016-11-06 19:01:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function jsonLoadInt(url) {
|
|
|
|
return jsonLoad(chrome.extension.getURL(url));
|
2016-11-07 17:22:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 05:01:49 +00:00
|
|
|
function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
|
2016-11-05 23:44:29 +00:00
|
|
|
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
|
2017-03-03 05:01:49 +00:00
|
|
|
return jsonLoad(indexUrl).then(index => {
|
2016-12-24 05:59:19 +00:00
|
|
|
if (!index.title || !index.version || !index.revision) {
|
2016-11-08 06:31:57 +00:00
|
|
|
return Promise.reject('unrecognized dictionary format');
|
|
|
|
}
|
|
|
|
|
2016-11-06 00:30:00 +00:00
|
|
|
if (indexLoaded !== null) {
|
2016-11-07 01:10:31 +00:00
|
|
|
return indexLoaded(
|
|
|
|
index.title,
|
|
|
|
index.version,
|
2016-12-24 05:59:19 +00:00
|
|
|
index.revision,
|
|
|
|
index.tagMeta || {},
|
2016-11-07 01:10:31 +00:00
|
|
|
index.termBanks > 0,
|
|
|
|
index.kanjiBanks > 0
|
|
|
|
).then(() => index);
|
2016-11-05 23:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}).then(index => {
|
|
|
|
const loaders = [];
|
2016-11-06 19:01:35 +00:00
|
|
|
const banksTotal = index.termBanks + index.kanjiBanks;
|
|
|
|
let banksLoaded = 0;
|
|
|
|
|
|
|
|
for (let i = 1; i <= index.termBanks; ++i) {
|
|
|
|
const bankUrl = `${indexDir}/term_bank_${i}.json`;
|
2017-03-03 05:01:49 +00:00
|
|
|
loaders.push(() => jsonLoad(bankUrl).then(entries => termsLoaded(
|
2016-11-06 19:01:35 +00:00
|
|
|
index.title,
|
|
|
|
entries,
|
|
|
|
banksTotal,
|
|
|
|
banksLoaded++
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 1; i <= index.kanjiBanks; ++i) {
|
|
|
|
const bankUrl = `${indexDir}/kanji_bank_${i}.json`;
|
2017-03-03 05:01:49 +00:00
|
|
|
loaders.push(() => jsonLoad(bankUrl).then(entries => kanjiLoaded(
|
2016-11-06 19:01:35 +00:00
|
|
|
index.title,
|
|
|
|
entries,
|
|
|
|
banksTotal,
|
|
|
|
banksLoaded++
|
|
|
|
)));
|
2016-11-05 23:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let chain = Promise.resolve();
|
2017-02-26 19:12:54 +00:00
|
|
|
for (const loader of loaders) {
|
2016-11-05 23:44:29 +00:00
|
|
|
chain = chain.then(loader);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain;
|
|
|
|
});
|
|
|
|
}
|
2017-03-03 05:01:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2017-03-05 03:16:19 +00:00
|
|
|
function handlebarsKanjiLinks(options) {
|
2017-03-03 05:01:49 +00:00
|
|
|
const isKanji = c => {
|
|
|
|
const code = c.charCodeAt(0);
|
|
|
|
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = '';
|
|
|
|
for (const c of options.fn(this)) {
|
|
|
|
if (isKanji(c)) {
|
|
|
|
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;
|
|
|
|
Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks);
|
|
|
|
Handlebars.registerHelper('multiLine', handlebarsMultiLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlebarsRender(template, data) {
|
|
|
|
return Handlebars.templates[template](data);
|
|
|
|
}
|