Cleanup using promises
This commit is contained in:
parent
dcce58fc2e
commit
e6d821b731
@ -65,7 +65,7 @@ class Translator {
|
||||
|
||||
const dfs = this.deinflector.deinflect(term, t => {
|
||||
const tags = [];
|
||||
for (let d of this.dictionary.findTerm(t)) {
|
||||
for (const d of this.dictionary.findTerm(t)) {
|
||||
tags.push(d.tags);
|
||||
}
|
||||
|
||||
@ -76,13 +76,13 @@ class Translator {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let df of dfs) {
|
||||
for (const df of dfs) {
|
||||
this.processTerm(groups, df.source, df.tags, df.rules, df.root);
|
||||
}
|
||||
}
|
||||
|
||||
let definitions = [];
|
||||
for (let key in groups) {
|
||||
for (const key in groups) {
|
||||
definitions.push(groups[key]);
|
||||
}
|
||||
|
||||
@ -115,7 +115,7 @@ class Translator {
|
||||
});
|
||||
|
||||
let length = 0;
|
||||
for (let result of definitions) {
|
||||
for (const result of definitions) {
|
||||
length = Math.max(length, result.source.length);
|
||||
}
|
||||
|
||||
@ -126,7 +126,7 @@ class Translator {
|
||||
let definitions = [];
|
||||
const processed = {};
|
||||
|
||||
for (let c of text) {
|
||||
for (const c of text) {
|
||||
if (!processed[c]) {
|
||||
definitions = definitions.concat(this.dictionary.findKanji(c));
|
||||
processed[c] = true;
|
||||
@ -137,13 +137,13 @@ class Translator {
|
||||
}
|
||||
|
||||
processTerm(groups, dfSource, dfTags, dfRules=[], dfRoot='') {
|
||||
for (let entry of this.dictionary.findTerm(dfRoot)) {
|
||||
for (const entry of this.dictionary.findTerm(dfRoot)) {
|
||||
if (entry.id in groups) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let matched = dfTags.length === 0;
|
||||
for (let t of dfTags) {
|
||||
for (const t of dfTags) {
|
||||
if (entry.tags.indexOf(t) !== -1) {
|
||||
matched = true;
|
||||
break;
|
||||
@ -157,7 +157,7 @@ class Translator {
|
||||
let popular = false;
|
||||
|
||||
let tags = [];
|
||||
for (let t of entry.tags) {
|
||||
for (const t of entry.tags) {
|
||||
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: entry.entities[t] || '', name: t};
|
||||
this.applyTagMeta(tag);
|
||||
tags.push(tag);
|
||||
@ -186,9 +186,9 @@ class Translator {
|
||||
processKanji(entries) {
|
||||
const processed = [];
|
||||
|
||||
for (let entry of entries) {
|
||||
for (const entry of entries) {
|
||||
const tags = [];
|
||||
for (let t of entry.tags) {
|
||||
for (const t of entry.tags) {
|
||||
const tag = {class: 'default', order: Number.MAX_SAFE_INTEGER, desc: '', name: t};
|
||||
this.applyTagMeta(tag);
|
||||
tags.push(tag);
|
||||
|
@ -17,26 +17,26 @@
|
||||
*/
|
||||
|
||||
|
||||
function bgSendMessage(action, params, callback) {
|
||||
chrome.runtime.sendMessage({action, params}, callback);
|
||||
function bgSendMessage(action, params) {
|
||||
return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve));
|
||||
}
|
||||
|
||||
function bgFindTerm(text, callback) {
|
||||
bgSendMessage('findTerm', {text}, callback);
|
||||
function bgFindTerm(text) {
|
||||
return bgSendMessage('findTerm', {text});
|
||||
}
|
||||
|
||||
function bgFindKanji(text, callback) {
|
||||
bgSendMessage('findKanji', {text}, callback);
|
||||
function bgFindKanji(text) {
|
||||
return bgSendMessage('findKanji', {text});
|
||||
}
|
||||
|
||||
function bgRenderText(data, template, callback) {
|
||||
bgSendMessage('renderText', {data, template}, callback);
|
||||
function bgRenderText(data, template) {
|
||||
return bgSendMessage('renderText', {data, template});
|
||||
}
|
||||
|
||||
function bgCanAddDefinitions(definitions, modes, callback) {
|
||||
bgSendMessage('canAddDefinitions', {definitions, modes}, callback);
|
||||
function bgCanAddDefinitions(definitions, modes) {
|
||||
return bgSendMessage('canAddDefinitions', {definitions, modes});
|
||||
}
|
||||
|
||||
function bgAddDefinition(definition, mode, callback) {
|
||||
bgSendMessage('addDefinition', {definition, mode}, callback);
|
||||
function bgAddDefinition(definition, mode) {
|
||||
return bgSendMessage('addDefinition', {definition, mode});
|
||||
}
|
||||
|
@ -90,10 +90,16 @@ class Client {
|
||||
}
|
||||
|
||||
textSource.setEndOffset(this.options.scanLength);
|
||||
bgFindTerm(textSource.text(), ({definitions, length}) => {
|
||||
if (length === 0) {
|
||||
this.hidePopup();
|
||||
} else {
|
||||
|
||||
let defs = [];
|
||||
let seq = -1;
|
||||
|
||||
bgFindTerm(textSource.text())
|
||||
.then(({definitions, length}) => {
|
||||
if (length === 0) {
|
||||
return Promise.reject();
|
||||
}
|
||||
|
||||
textSource.setEndOffset(length);
|
||||
|
||||
const sentence = Client.extractSentence(textSource, this.options.sentenceExtent);
|
||||
@ -102,23 +108,22 @@ class Client {
|
||||
definition.sentence = sentence;
|
||||
});
|
||||
|
||||
const sequence = ++this.sequence;
|
||||
bgRenderText(
|
||||
{definitions, root: this.fgRoot, options: this.options, sequence},
|
||||
'term-list.html',
|
||||
(content) => {
|
||||
this.definitions = definitions;
|
||||
this.showPopup(textSource, content);
|
||||
defs = definitions;
|
||||
seq = ++this.sequence;
|
||||
|
||||
bgCanAddDefinitions(definitions, ['vocab_kanji', 'vocab_kana'], (states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'term-list.html');
|
||||
})
|
||||
.then((content) => {
|
||||
this.definitions = defs;
|
||||
this.showPopup(textSource, content);
|
||||
|
||||
return bgCanAddDefinitions(defs, ['vocab_kanji', 'vocab_kana']);
|
||||
})
|
||||
.then((states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
|
||||
}
|
||||
}, () => this.hidePopup());
|
||||
}
|
||||
|
||||
showPopup(textSource, content) {
|
||||
@ -138,7 +143,7 @@ class Client {
|
||||
this.lastTextSource.deselect();
|
||||
}
|
||||
|
||||
this.lastTextSource = null;
|
||||
this.lastTextSource = null;
|
||||
this.definitions = null;
|
||||
}
|
||||
|
||||
@ -156,7 +161,7 @@ class Client {
|
||||
const state = {};
|
||||
state[mode] = false;
|
||||
|
||||
bgAddDefinition(this.definitions[index], mode, (success) => {
|
||||
bgAddDefinition(this.definitions[index], mode).then((success) => {
|
||||
if (success) {
|
||||
this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});
|
||||
} else {
|
||||
@ -173,7 +178,7 @@ class Client {
|
||||
url += `&kana=${encodeURIComponent(definition.reading)}`;
|
||||
}
|
||||
|
||||
for (let key in this.audio) {
|
||||
for (const key in this.audio) {
|
||||
this.audio[key].pause();
|
||||
}
|
||||
|
||||
@ -185,27 +190,29 @@ class Client {
|
||||
}
|
||||
|
||||
api_displayKanji(kanji) {
|
||||
bgFindKanji(kanji, (definitions) => {
|
||||
definitions.forEach((definition) => {
|
||||
definition.url = window.location.href;
|
||||
});
|
||||
let defs = [];
|
||||
let seq = -1;
|
||||
|
||||
const sequence = ++this.sequence;
|
||||
bgRenderText(
|
||||
{definitions, root: this.fgRoot, options: this.options, sequence},
|
||||
'kanji-list.html',
|
||||
(content) => {
|
||||
this.definitions = definitions;
|
||||
this.popup.setContent(content, definitions);
|
||||
bgFindKanji(kanji)
|
||||
.then((definitions) => {
|
||||
definitions.forEach((definition) => definition.url = window.location.href);
|
||||
|
||||
bgCanAddDefinitions(definitions, ['kanji'], (states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
|
||||
}
|
||||
});
|
||||
defs = definitions;
|
||||
seq = ++this.sequence;
|
||||
|
||||
return bgRenderText({definitions, root: this.fgRoot, options: this.options, sequence: seq}, 'kanji-list.html');
|
||||
})
|
||||
.then((content) => {
|
||||
this.definitions = defs;
|
||||
this.popup.setContent(content, defs);
|
||||
|
||||
return bgCanAddDefinitions(defs, ['kanji']);
|
||||
})
|
||||
.then((states) => {
|
||||
if (states !== null) {
|
||||
states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence: seq}));
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
static textSourceFromPoint(point) {
|
||||
|
@ -77,7 +77,7 @@ class TextSourceRange {
|
||||
}
|
||||
|
||||
equals(other) {
|
||||
return other.rng && other.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) == 0;
|
||||
return other.rng && other.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) === 0;
|
||||
}
|
||||
|
||||
static seekForward(node, length) {
|
||||
|
Loading…
Reference in New Issue
Block a user