diff options
Diffstat (limited to 'ext/fg/js/driver.js')
| -rw-r--r-- | ext/fg/js/driver.js | 228 | 
1 files changed, 58 insertions, 170 deletions
diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js index 9aab7950..ef7db481 100644 --- a/ext/fg/js/driver.js +++ b/ext/fg/js/driver.js @@ -21,27 +21,22 @@ 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));          window.addEventListener('keydown', this.onKeyDown.bind(this)); -        window.addEventListener('resize', e => this.hidePopup()); +        window.addEventListener('resize', e => this.searchClear());          getOptions().then(opts => {              this.options = opts; -            return getEnabled(); +            return isEnabled();          }).then(enabled => {              this.enabled = enabled;          }); @@ -65,7 +60,7 @@ class Driver {          if (this.enabled && this.lastMousePos !== null && e.keyCode === 16 /* shift */) {              this.searchAt(this.lastMousePos, true);          } else if (e.keyCode === 27 /* esc */) { -            this.hidePopup(); +            this.searchClear();          }      } @@ -92,7 +87,7 @@ class Driver {          }          const searcher = () => this.searchAt(this.lastMousePos, false); -        if (!this.popup.visible() || e.shiftKey || e.which === 2 /* mmb */) { +        if (!this.popup.isVisible() || e.shiftKey || e.which === 2 /* mmb */) {              searcher();          } else {              this.popupTimerSet(searcher); @@ -102,7 +97,7 @@ class Driver {      onMouseDown(e) {          this.lastMousePos = {x: e.clientX, y: e.clientY};          this.popupTimerClear(); -        this.hidePopup(); +        this.searchClear();      }      onBgMessage({action, params}, sender, callback) { @@ -114,20 +109,46 @@ 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.searchClear(); +            } + +            return; +        } + +        if (this.lastTextSource !== null && this.lastTextSource.equals(textSource)) { +            return;          } + +        this.pendingLookup = true; +        this.searchTerms(textSource).then(found => { +            if (!found) { +                this.searchKanji(textSource).then(found => { +                    if (!found && hideNotFound) { +                        this.searchClear(); +                    } +                }); +            } +        }).catch(error => { +            window.alert('Error: ' + error); +        }).then(() => { +            this.pendingLookup = false; +        });      }      searchTerms(textSource) {          textSource.setEndOffset(this.options.scanLength); -        this.pendingLookup = true; -        return findTerm(textSource.text()).then(({definitions, length}) => { +        const findFunc = this.options.groupTermResults ? findTermGrouped : findTerm; +        return findFunc(textSource.text()).then(({definitions, length}) => {              if (definitions.length === 0) { -                this.pendingLookup = false;                  return false;              } else {                  textSource.setEndOffset(length); @@ -138,119 +159,46 @@ 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.showTermDefs(definitions, this.options); +                this.lastTextSource = textSource; +                if (this.options.selectMatchedText) { +                    textSource.select(); +                } -                    return true; -                }); +                return true;              }          }).catch(error => { -            alert('Error: ' + error); +            window.alert('Error: ' + error); +            return false;          });      }      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} -                        )); -                    } - -                    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.popup.showNextTo(textSource.getRect()); +                this.popup.showKanjiDefs(definitions, this.options); +                this.lastTextSource = textSource; +                if (this.options.selectMatchedText) { +                    textSource.select(); +                } -        this.searchTerms(textSource).then(found => { -            if (!found) { -                this.searchKanji(textSource).then(found => { -                    if (!found && hideNotFound) { -                        this.hidePopup(); -                    } -                }); +                return true;              }          }).catch(error => { -            alert('Error: ' + error); +            window.alert('Error: ' + error); +            return false;          });      } -    showPopup(textSource, content) { -        this.popup.showNextTo(textSource.getRect(), content); - -        if (this.options.selectMatchedText) { -            textSource.select(); -        } - -        this.lastTextSource = textSource; -    } - -    hidePopup() { +    searchClear() {          this.popup.hide();          if (this.options.selectMatchedText && this.lastTextSource !== null) { @@ -258,7 +206,6 @@ class Driver {          }          this.lastTextSource = null; -        this.definitions = null;      }      api_setOptions(opts) { @@ -267,68 +214,9 @@ class Driver {      api_setEnabled(enabled) {          if (!(this.enabled = enabled)) { -            this.hidePopup(); +            this.searchClear();          }      } - -    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'); -            } -        }).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();  |