aboutsummaryrefslogtreecommitdiff
path: root/ext/fg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg')
-rw-r--r--ext/fg/js/api.js24
-rw-r--r--ext/fg/js/client.js95
-rw-r--r--ext/fg/js/source-range.js2
3 files changed, 64 insertions, 57 deletions
diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js
index 7316c604..643d0360 100644
--- a/ext/fg/js/api.js
+++ b/ext/fg/js/api.js
@@ -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});
}
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js
index 34a1a605..202d6dbd 100644
--- a/ext/fg/js/client.js
+++ b/ext/fg/js/client.js
@@ -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);
-
- bgCanAddDefinitions(definitions, ['vocab_kanji', 'vocab_kana'], (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}, '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;
- });
-
- 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);
-
- bgCanAddDefinitions(definitions, ['kanji'], (states) => {
- if (states !== null) {
- states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence}));
- }
- });
+ let defs = [];
+ let seq = -1;
+
+ bgFindKanji(kanji)
+ .then((definitions) => {
+ definitions.forEach((definition) => definition.url = window.location.href);
+
+ 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) {
diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js
index 879b949c..a25b3fdc 100644
--- a/ext/fg/js/source-range.js
+++ b/ext/fg/js/source-range.js
@@ -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) {