summaryrefslogtreecommitdiff
path: root/ext/fg/js/driver.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg/js/driver.js')
-rw-r--r--ext/fg/js/driver.js198
1 files changed, 37 insertions, 161 deletions
diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js
index 0c785d31..1c797201 100644
--- a/ext/fg/js/driver.js
+++ b/ext/fg/js/driver.js
@@ -21,18 +21,13 @@ class Driver {
constructor() {
this.popup = new Popup();
this.popupTimer = null;
- this.audio = {};
this.lastMousePos = null;
this.lastTextSource = null;
this.pendingLookup = false;
this.enabled = false;
this.options = null;
- this.definitions = null;
- this.sequence = 0;
- this.fgRoot = chrome.extension.getURL('fg');
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
- window.addEventListener('message', this.onFrameMessage.bind(this));
window.addEventListener('mouseover', this.onMouseOver.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this));
window.addEventListener('mousemove', this.onMouseMove.bind(this));
@@ -114,20 +109,45 @@ class Driver {
callback();
}
- onFrameMessage(e) {
- const {action, params} = e.data, method = this['api_' + action];
- if (typeof(method) === 'function') {
- method.call(this, params);
+ searchAt(point, hideNotFound) {
+ if (this.pendingLookup) {
+ return;
+ }
+
+ const textSource = textSourceFromPoint(point);
+ if (textSource === null || !textSource.containsPoint(point)) {
+ if (hideNotFound) {
+ this.hidePopup();
+ }
+
+ return;
+ }
+
+ if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
+ return true;
}
+
+ this.pendingLookup = true;
+ this.searchTerms(textSource).then(found => {
+ if (!found) {
+ this.searchKanji(textSource).then(found => {
+ if (!found && hideNotFound) {
+ this.hidePopup();
+ }
+ });
+ }
+ }).catch(error => {
+ alert('Error: ' + error);
+ }).then(() => {
+ this.pendingLookup = false;
+ });
}
searchTerms(textSource) {
textSource.setEndOffset(this.options.scanLength);
- this.pendingLookup = true;
return findTerm(textSource.text()).then(({definitions, length}) => {
if (definitions.length === 0) {
- this.pendingLookup = false;
return false;
} else {
textSource.setEndOffset(length);
@@ -138,30 +158,10 @@ class Driver {
definition.sentence = sentence;
});
- const sequence = ++this.sequence;
- const context = {
- definitions,
- sequence,
- addable: this.options.ankiMethod !== 'disabled',
- root: this.fgRoot,
- options: this.options
- };
-
- return renderText(context, 'term-list.html').then(content => {
- this.definitions = definitions;
- this.pendingLookup = false;
- this.showPopup(textSource, content);
- return canAddDefinitions(definitions, ['term_kanji', 'term_kana']);
- }).then(states => {
- if (states !== null) {
- states.forEach((state, index) => this.popup.invokeApi(
- 'setActionState',
- {index, state, sequence}
- ));
- }
+ this.popup.showNextTo(textSource.getRect());
+ this.popup.invoke('showTermDefs', {definitions, options: this.options});
- return true;
- });
+ return true;
}
}).catch(error => {
alert('Error: ' + error);
@@ -171,85 +171,22 @@ class Driver {
searchKanji(textSource) {
textSource.setEndOffset(1);
- this.pendingLookup = true;
return findKanji(textSource.text()).then(definitions => {
if (definitions.length === 0) {
- this.pendingLookup = false;
return false;
} else {
definitions.forEach(definition => definition.url = window.location.href);
- const sequence = ++this.sequence;
- const context = {
- definitions,
- sequence,
- addable: this.options.ankiMethod !== 'disabled',
- root: this.fgRoot,
- options: this.options
- };
-
- return renderText(context, 'kanji-list.html').then(content => {
- this.definitions = definitions;
- this.pendingLookup = false;
- this.showPopup(textSource, content);
- return canAddDefinitions(definitions, ['kanji']);
- }).then(states => {
- if (states !== null) {
- states.forEach((state, index) => this.popup.invokeApi(
- 'setActionState',
- {index, state, sequence}
- ));
- }
+ this.popup.showNextTo(textSource.getRect());
+ this.popup.invoke('showKanjiDefs', {definitions, options: this.options});
- return true;
- });
+ return true;
}
}).catch(error => {
alert('Error: ' + error);
});
}
- searchAt(point, hideNotFound) {
- if (this.pendingLookup) {
- return;
- }
-
- const textSource = textSourceFromPoint(point);
- if (textSource === null || !textSource.containsPoint(point)) {
- if (hideNotFound) {
- this.hidePopup();
- }
-
- return;
- }
-
- if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) {
- return true;
- }
-
- this.searchTerms(textSource).then(found => {
- if (!found) {
- this.searchKanji(textSource).then(found => {
- if (!found && hideNotFound) {
- this.hidePopup();
- }
- });
- }
- }).catch(error => {
- alert('Error: ' + error);
- });
- }
-
- showPopup(textSource, content) {
- this.popup.showNextTo(textSource.getRect(), content);
-
- if (this.options.selectMatchedText) {
- textSource.select();
- }
-
- this.lastTextSource = textSource;
- }
-
hidePopup() {
this.popup.hide();
@@ -270,67 +207,6 @@ class Driver {
this.hidePopup();
}
}
-
- api_addNote({index, mode}) {
- const state = {[mode]: false};
- addDefinition(this.definitions[index], mode).then(success => {
- if (success) {
- this.popup.invokeApi('setActionState', {index, state, sequence: this.sequence});
- } else {
- alert('Note could not be added');
- }
-
- this.popup.invokeApi('addNoteComplete');
- }).catch(error => {
- alert('Error: ' + error);
- });
- }
-
- api_playAudio(index) {
- const definition = this.definitions[index];
-
- let url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=${encodeURIComponent(definition.expression)}`;
- if (definition.reading) {
- url += `&kana=${encodeURIComponent(definition.reading)}`;
- }
-
- for (const key in this.audio) {
- this.audio[key].pause();
- }
-
- const audio = this.audio[url] || new Audio(url);
- audio.currentTime = 0;
- audio.play();
-
- this.audio[url] = audio;
- }
-
- api_displayKanji(kanji) {
- findKanji(kanji).then(definitions => {
- definitions.forEach(definition => definition.url = window.location.href);
-
- const sequence = ++this.sequence;
- const context = {
- definitions,
- sequence,
- addable: this.options.ankiMethod !== 'disabled',
- root: this.fgRoot,
- options: this.options
- };
-
- return renderText(context, 'kanji-list.html').then(content => {
- this.definitions = definitions;
- this.popup.setContent(content, definitions);
- return canAddDefinitions(definitions, ['kanji']);
- }).then(states => {
- if (states !== null) {
- states.forEach((state, index) => this.popup.invokeApi('setActionState', {index, state, sequence}));
- }
- });
- }).catch(error => {
- alert('Error: ' + error);
- });
- }
}
window.driver = new Driver();