From 863e36e829e4e01a8c1e7b15384820ad7766d6a8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 8 Dec 2019 22:20:07 -0500 Subject: Update frontend message handlers --- ext/fg/js/frontend.js | 52 +++++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) (limited to 'ext/fg') diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js index 54e78bf5..6c1dafe5 100644 --- a/ext/fg/js/frontend.js +++ b/ext/fg/js/frontend.js @@ -77,20 +77,19 @@ class Frontend extends TextScanner { onWindowMessage(e) { const action = e.data; - const handlers = Frontend.windowMessageHandlers; - if (hasOwn(handlers, action)) { - const handler = handlers[action]; - handler(this); - } + const handler = Frontend._windowMessageHandlers.get(action); + if (typeof handler !== 'function') { return false; } + + handler(this); } onRuntimeMessage({action, params}, sender, callback) { - const handlers = Frontend.runtimeMessageHandlers; - if (hasOwn(handlers, action)) { - const handler = handlers[action]; - const result = handler(this, params); - callback(result); - } + const handler = Frontend._runtimeMessageHandlers.get(action); + if (typeof handler !== 'function') { return false; } + + const result = handler(this, params, sender); + callback(result); + return false; } getMouseEventListeners() { @@ -195,26 +194,13 @@ class Frontend extends TextScanner { } } -Frontend.windowMessageHandlers = { - popupClose: (self) => { - self.onSearchClear(true); - }, +Frontend._windowMessageHandlers = new Map([ + ['popupClose', (self) => self.onSearchClear(true)], + ['selectionCopy', () => document.execCommand('copy')] +]); - selectionCopy: () => { - document.execCommand('copy'); - } -}; - -Frontend.runtimeMessageHandlers = { - optionsUpdate: (self) => { - self.updateOptions(); - }, - - popupSetVisibleOverride: (self, {visible}) => { - self.popup.setVisibleOverride(visible); - }, - - getUrl: () => { - return {url: window.location.href}; - } -}; +Frontend._runtimeMessageHandlers = new Map([ + ['optionsUpdate', (self) => self.updateOptions()], + ['popupSetVisibleOverride', (self, {visible}) => self.popup.setVisibleOverride(visible)], + ['getUrl', () => ({url: window.location.href})] +]); -- cgit v1.2.3