diff options
Diffstat (limited to 'ext/fg/js/frontend.js')
| -rw-r--r-- | ext/fg/js/frontend.js | 80 | 
1 files changed, 42 insertions, 38 deletions
| diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 167e82c0..58dc0e4a 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -55,7 +55,7 @@ class Frontend {          try {              this.options = await apiOptionsGet(this.getOptionsContext()); -            window.addEventListener('message', this.onFrameMessage.bind(this)); +            window.addEventListener('message', this.onWindowMessage.bind(this));              window.addEventListener('mousedown', this.onMouseDown.bind(this));              window.addEventListener('mousemove', this.onMouseMove.bind(this));              window.addEventListener('mouseover', this.onMouseOver.bind(this)); @@ -71,7 +71,7 @@ class Frontend {                  window.addEventListener('contextmenu', this.onContextMenu.bind(this));              } -            chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this)); +            chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));          } catch (e) {              this.onError(e);          } @@ -128,32 +128,24 @@ class Frontend {          }          this.popupTimerClear(); -        this.searchClear(); +        this.searchClear(true);      }      onMouseOut(e) {          this.popupTimerClear();      } -    onFrameMessage(e) { -        const handlers = { -            popupClose: () => { -                this.searchClear(); -            }, - -            selectionCopy: () => { -                document.execCommand('copy'); -            } -        }; - -        const handler = handlers[e.data]; -        if (handler) { -            handler(); +    onWindowMessage(e) { +        const action = e.data; +        const handlers = Frontend.windowMessageHandlers; +        if (handlers.hasOwnProperty(action)) { +            const handler = handlers[action]; +            handler(this);          }      }      onResize() { -        this.searchClear(); +        this.searchClear(true);      }      onClick(e) { @@ -240,20 +232,11 @@ class Frontend {          this.contextMenuChecking = false;      } -    onBgMessage({action, params}, sender, callback) { -        const handlers = { -            optionsUpdate: () => { -                this.updateOptions(); -            }, - -            popupSetVisible: ({visible}) => { -                this.popup.setVisible(visible); -            } -        }; - -        const handler = handlers[action]; -        if (handler) { -            handler(params); +    onRuntimeMessage({action, params}, sender, callback) { +        const handlers = Frontend.runtimeMessageHandlers; +        if (handlers.hasOwnProperty(action)) { +            const handler = handlers[action]; +            handler(this, params);              callback();          }      } @@ -265,7 +248,7 @@ class Frontend {      async updateOptions() {          this.options = await apiOptionsGet(this.getOptionsContext());          if (!this.options.enable) { -            this.searchClear(); +            this.searchClear(false);          }      } @@ -320,7 +303,7 @@ class Frontend {                  textSource.cleanup();              }              if (hideResults && this.options.scanning.autoHideResults) { -                this.searchClear(); +                this.searchClear(true);              }              this.pendingLookup = false; @@ -333,7 +316,7 @@ class Frontend {          const searchText = textSource.text();          if (searchText.length === 0) { -            return; +            return false;          }          const {definitions, length} = await apiTermsFind(searchText, this.getOptionsContext()); @@ -366,7 +349,7 @@ class Frontend {          const searchText = textSource.text();          if (searchText.length === 0) { -            return; +            return false;          }          const definitions = await apiKanjiFind(searchText, this.getOptionsContext()); @@ -392,8 +375,8 @@ class Frontend {          return true;      } -    searchClear() { -        this.popup.hide(); +    searchClear(changeFocus) { +        this.popup.hide(changeFocus);          this.popup.clearAutoPlayTimer();          if (this.options.scanning.selectText && this.textSourceLast) { @@ -529,4 +512,25 @@ class Frontend {      }  } +Frontend.windowMessageHandlers = { +    popupClose: (self) => { +        self.searchClear(true); +    }, + +    selectionCopy: () => { +        document.execCommand('copy'); +    } +}; + +Frontend.runtimeMessageHandlers = { +    optionsUpdate: (self) => { +        self.updateOptions(); +    }, + +    popupSetVisible: (self, {visible}) => { +        self.popup.setVisible(visible); +    } +}; + +  window.yomichan_frontend = Frontend.create(); |