aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-07 10:41:31 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-03-07 10:41:31 -0500
commit7822230b7f969b74d3a307fe383a62be9e31c713 (patch)
tree2da2b21067964ffb55f49e2e35a93b4945736c2c /ext/bg
parent58d734b8f79a9d859063ef70afe1ff367afe7a29 (diff)
Use events for ClipboardMonitor
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/backend.js4
-rw-r--r--ext/bg/js/clipboard-monitor.js9
-rw-r--r--ext/bg/js/search.js5
3 files changed, 7 insertions, 11 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 5b7ab084..2e46088c 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -117,7 +117,7 @@ class Backend {
chrome.tabs.create({url: chrome.runtime.getURL('/bg/guide.html')});
}
- this.clipboardMonitor.onClipboardText = this._onClipboardText.bind(this);
+ this.clipboardMonitor.on('change', this._onClipboardText.bind(this));
this._sendMessageAllTabs('backendPrepared');
chrome.runtime.sendMessage({action: 'backendPrepared'});
@@ -154,7 +154,7 @@ class Backend {
}
}
- _onClipboardText(text) {
+ _onClipboardText({text}) {
this._onCommandSearch({mode: 'popup', query: text});
}
diff --git a/ext/bg/js/clipboard-monitor.js b/ext/bg/js/clipboard-monitor.js
index c2f41385..c102572f 100644
--- a/ext/bg/js/clipboard-monitor.js
+++ b/ext/bg/js/clipboard-monitor.js
@@ -18,18 +18,15 @@
/*global apiClipboardGet, jpIsStringPartiallyJapanese*/
-class ClipboardMonitor {
+class ClipboardMonitor extends EventDispatcher {
constructor() {
+ super();
this.timerId = null;
this.timerToken = null;
this.interval = 250;
this.previousText = null;
}
- onClipboardText(_text) {
- throw new Error('Override me');
- }
-
start() {
this.stop();
@@ -55,7 +52,7 @@ class ClipboardMonitor {
) {
this.previousText = text;
if (jpIsStringPartiallyJapanese(text)) {
- this.onClipboardText(text);
+ this.trigger('change', {text});
}
}
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js
index f3cba7ae..9acccb90 100644
--- a/ext/bg/js/search.js
+++ b/ext/bg/js/search.js
@@ -102,8 +102,7 @@ class DisplaySearch extends Display {
this.wanakanaEnable.addEventListener('change', this.onWanakanaEnableChange.bind(this));
window.addEventListener('popstate', this.onPopState.bind(this));
window.addEventListener('copy', this.onCopy.bind(this));
-
- this.clipboardMonitor.onClipboardText = this.onExternalSearchUpdate.bind(this);
+ this.clipboardMonitor.on('change', this.onExternalSearchUpdate.bind(this));
this.updateSearchButton();
} catch (e) {
@@ -198,7 +197,7 @@ class DisplaySearch extends Display {
this.clipboardMonitor.setPreviousText(document.getSelection().toString().trim());
}
- onExternalSearchUpdate(text) {
+ onExternalSearchUpdate({text}) {
this.setQuery(text);
const url = new URL(window.location.href);
url.searchParams.set('query', text);