Update frontend message handlers

This commit is contained in:
toasted-nutbread 2019-12-08 22:20:07 -05:00
parent e2c5c16da6
commit 863e36e829

View File

@ -77,20 +77,19 @@ class Frontend extends TextScanner {
onWindowMessage(e) { onWindowMessage(e) {
const action = e.data; const action = e.data;
const handlers = Frontend.windowMessageHandlers; const handler = Frontend._windowMessageHandlers.get(action);
if (hasOwn(handlers, action)) { if (typeof handler !== 'function') { return false; }
const handler = handlers[action];
handler(this); handler(this);
} }
}
onRuntimeMessage({action, params}, sender, callback) { onRuntimeMessage({action, params}, sender, callback) {
const handlers = Frontend.runtimeMessageHandlers; const handler = Frontend._runtimeMessageHandlers.get(action);
if (hasOwn(handlers, action)) { if (typeof handler !== 'function') { return false; }
const handler = handlers[action];
const result = handler(this, params); const result = handler(this, params, sender);
callback(result); callback(result);
} return false;
} }
getMouseEventListeners() { getMouseEventListeners() {
@ -195,26 +194,13 @@ class Frontend extends TextScanner {
} }
} }
Frontend.windowMessageHandlers = { Frontend._windowMessageHandlers = new Map([
popupClose: (self) => { ['popupClose', (self) => self.onSearchClear(true)],
self.onSearchClear(true); ['selectionCopy', () => document.execCommand('copy')]
}, ]);
selectionCopy: () => { Frontend._runtimeMessageHandlers = new Map([
document.execCommand('copy'); ['optionsUpdate', (self) => self.updateOptions()],
} ['popupSetVisibleOverride', (self, {visible}) => self.popup.setVisibleOverride(visible)],
}; ['getUrl', () => ({url: window.location.href})]
]);
Frontend.runtimeMessageHandlers = {
optionsUpdate: (self) => {
self.updateOptions();
},
popupSetVisibleOverride: (self, {visible}) => {
self.popup.setVisibleOverride(visible);
},
getUrl: () => {
return {url: window.location.href};
}
};