diff options
| author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-10-02 20:31:42 -0400 | 
|---|---|---|
| committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-10-02 20:31:42 -0400 | 
| commit | 7d15213916355a806ba687803ca94209e29f142c (patch) | |
| tree | 01452a4ee0338a6e4e59f161996f32b39964e239 | |
| parent | a628610cbd9ea235987cef399021b0685c50f0e4 (diff) | |
Use static object for frontend message handlers
| -rw-r--r-- | ext/fg/js/frontend.js | 64 | 
1 files changed, 34 insertions, 30 deletions
| diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index d5bb00c0..deec1ffd 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);          } @@ -135,20 +135,12 @@ class Frontend {          this.popupTimerClear();      } -    onFrameMessage(e) { -        const handlers = { -            popupClose: () => { -                this.searchClear(true); -            }, - -            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);          }      } @@ -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();          }      } @@ -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(); |