diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2020-01-26 03:31:31 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2020-02-09 21:51:33 +0200 |
commit | 90a5d795708898fba940f77358269354e96355a7 (patch) | |
tree | d77eea8b005848d5528c539ace87232cc624f0cd /ext/bg/js | |
parent | c685fd0e5f90c32911a225f0aaa68f5ff9fdfb7b (diff) |
use ClipboardMonitor in Backend
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/api.js | 4 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 25 |
2 files changed, 15 insertions, 14 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 285b8016..f4be7a0c 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -29,6 +29,10 @@ function apiGetDisplayTemplatesHtml() { return _apiInvoke('getDisplayTemplatesHtml'); } +function apiClipboardGet() { + return _apiInvoke('clipboardGet'); +} + function _apiInvoke(action, params={}) { const data = {action, params}; return new Promise((resolve, reject) => { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index d80e7713..fa1660c2 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -22,6 +22,7 @@ class Backend { this.translator = new Translator(); this.anki = new AnkiNull(); this.mecab = new Mecab(); + this.clipboardMonitor = new ClipboardMonitor(); this.options = null; this.optionsSchema = null; this.optionsContext = { @@ -33,10 +34,8 @@ class Backend { this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target'); + this.popupWindow = null; - this.clipboardPopupTimerId = null; - this.clipboardInterval = 250; - this.clipboardPreviousText = null; this.apiForwarder = new BackendApiForwarder(); } @@ -71,6 +70,8 @@ class Backend { this.isPreparedResolve(); this.isPreparedResolve = null; this.isPreparedPromise = null; + + this.clipboardMonitor.onClipboardText = (text) => this._onClipboardText(text); } onOptionsUpdated(source) { @@ -101,6 +102,10 @@ class Backend { } } + _onClipboardText(text) { + this._onCommandSearch({mode: 'popup', query: text}); + } + _onZoomChange({tabId, oldZoomFactor, newZoomFactor}) { const callback = () => this.checkLastError(chrome.runtime.lastError); chrome.tabs.sendMessage(tabId, {action: 'zoomChanged', params: {oldZoomFactor, newZoomFactor}}, callback); @@ -126,18 +131,10 @@ class Backend { this.mecab.stopListener(); } - window.clearInterval(this.clipboardPopupTimerId); if (options.general.enableClipboardPopups) { - this.clipboardPopupTimerId = setInterval(() => { - this._onApiClipboardGet() - .then((result) => { - if (this.clipboardPreviousText === result) { - return; - } - this._onCommandSearch({mode: 'popup', query: result}); - this.clipboardPreviousText = result; - }); - }, this.clipboardInterval); + this.clipboardMonitor.start(); + } else { + this.clipboardMonitor.stop(); } } |