Use events for ClipboardMonitor

This commit is contained in:
toasted-nutbread 2020-03-07 10:41:31 -05:00
parent 58d734b8f7
commit 7822230b7f
3 changed files with 7 additions and 11 deletions

View File

@ -117,7 +117,7 @@ class Backend {
chrome.tabs.create({url: chrome.runtime.getURL('/bg/guide.html')}); 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'); this._sendMessageAllTabs('backendPrepared');
chrome.runtime.sendMessage({action: 'backendPrepared'}); chrome.runtime.sendMessage({action: 'backendPrepared'});
@ -154,7 +154,7 @@ class Backend {
} }
} }
_onClipboardText(text) { _onClipboardText({text}) {
this._onCommandSearch({mode: 'popup', query: text}); this._onCommandSearch({mode: 'popup', query: text});
} }

View File

@ -18,18 +18,15 @@
/*global apiClipboardGet, jpIsStringPartiallyJapanese*/ /*global apiClipboardGet, jpIsStringPartiallyJapanese*/
class ClipboardMonitor { class ClipboardMonitor extends EventDispatcher {
constructor() { constructor() {
super();
this.timerId = null; this.timerId = null;
this.timerToken = null; this.timerToken = null;
this.interval = 250; this.interval = 250;
this.previousText = null; this.previousText = null;
} }
onClipboardText(_text) {
throw new Error('Override me');
}
start() { start() {
this.stop(); this.stop();
@ -55,7 +52,7 @@ class ClipboardMonitor {
) { ) {
this.previousText = text; this.previousText = text;
if (jpIsStringPartiallyJapanese(text)) { if (jpIsStringPartiallyJapanese(text)) {
this.onClipboardText(text); this.trigger('change', {text});
} }
} }

View File

@ -102,8 +102,7 @@ class DisplaySearch extends Display {
this.wanakanaEnable.addEventListener('change', this.onWanakanaEnableChange.bind(this)); this.wanakanaEnable.addEventListener('change', this.onWanakanaEnableChange.bind(this));
window.addEventListener('popstate', this.onPopState.bind(this)); window.addEventListener('popstate', this.onPopState.bind(this));
window.addEventListener('copy', this.onCopy.bind(this)); window.addEventListener('copy', this.onCopy.bind(this));
this.clipboardMonitor.on('change', this.onExternalSearchUpdate.bind(this));
this.clipboardMonitor.onClipboardText = this.onExternalSearchUpdate.bind(this);
this.updateSearchButton(); this.updateSearchButton();
} catch (e) { } catch (e) {
@ -198,7 +197,7 @@ class DisplaySearch extends Display {
this.clipboardMonitor.setPreviousText(document.getSelection().toString().trim()); this.clipboardMonitor.setPreviousText(document.getSelection().toString().trim());
} }
onExternalSearchUpdate(text) { onExternalSearchUpdate({text}) {
this.setQuery(text); this.setQuery(text);
const url = new URL(window.location.href); const url = new URL(window.location.href);
url.searchParams.set('query', text); url.searchParams.set('query', text);