summaryrefslogtreecommitdiff
path: root/ext/fg/js/frontend.js
diff options
context:
space:
mode:
authorAlex Yatskov <FooSoft@users.noreply.github.com>2019-10-05 09:20:45 -0700
committerGitHub <noreply@github.com>2019-10-05 09:20:45 -0700
commit46ab36180f486a19332c538401b4db12ffe1bda1 (patch)
tree81a90e3967a44715b7d5c7115b87f048fc40e291 /ext/fg/js/frontend.js
parent440d6a91fd9c5122aa804b72171e4c15a496e58a (diff)
parentfa7ee468c0b8b877bb6d94a031464525b1a87c6b (diff)
Merge pull request #233 from toasted-nutbread/static-handlers
Static handlers
Diffstat (limited to 'ext/fg/js/frontend.js')
-rw-r--r--ext/fg/js/frontend.js64
1 files changed, 34 insertions, 30 deletions
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 3292cac4..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);
}
@@ -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();