use ClipboardMonitor in Backend

This commit is contained in:
siikamiika 2020-01-26 03:31:31 +02:00
parent c685fd0e5f
commit 90a5d79570
3 changed files with 16 additions and 14 deletions

View File

@ -26,6 +26,7 @@
<script src="/bg/js/mecab.js"></script> <script src="/bg/js/mecab.js"></script>
<script src="/bg/js/audio.js"></script> <script src="/bg/js/audio.js"></script>
<script src="/bg/js/backend-api-forwarder.js"></script> <script src="/bg/js/backend-api-forwarder.js"></script>
<script src="/bg/js/clipboard-monitor.js"></script>
<script src="/bg/js/conditions.js"></script> <script src="/bg/js/conditions.js"></script>
<script src="/bg/js/database.js"></script> <script src="/bg/js/database.js"></script>
<script src="/bg/js/deinflector.js"></script> <script src="/bg/js/deinflector.js"></script>

View File

@ -29,6 +29,10 @@ function apiGetDisplayTemplatesHtml() {
return _apiInvoke('getDisplayTemplatesHtml'); return _apiInvoke('getDisplayTemplatesHtml');
} }
function apiClipboardGet() {
return _apiInvoke('clipboardGet');
}
function _apiInvoke(action, params={}) { function _apiInvoke(action, params={}) {
const data = {action, params}; const data = {action, params};
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -22,6 +22,7 @@ class Backend {
this.translator = new Translator(); this.translator = new Translator();
this.anki = new AnkiNull(); this.anki = new AnkiNull();
this.mecab = new Mecab(); this.mecab = new Mecab();
this.clipboardMonitor = new ClipboardMonitor();
this.options = null; this.options = null;
this.optionsSchema = null; this.optionsSchema = null;
this.optionsContext = { this.optionsContext = {
@ -33,10 +34,8 @@ class Backend {
this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve));
this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target'); this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target');
this.popupWindow = null; this.popupWindow = null;
this.clipboardPopupTimerId = null;
this.clipboardInterval = 250;
this.clipboardPreviousText = null;
this.apiForwarder = new BackendApiForwarder(); this.apiForwarder = new BackendApiForwarder();
} }
@ -71,6 +70,8 @@ class Backend {
this.isPreparedResolve(); this.isPreparedResolve();
this.isPreparedResolve = null; this.isPreparedResolve = null;
this.isPreparedPromise = null; this.isPreparedPromise = null;
this.clipboardMonitor.onClipboardText = (text) => this._onClipboardText(text);
} }
onOptionsUpdated(source) { onOptionsUpdated(source) {
@ -101,6 +102,10 @@ class Backend {
} }
} }
_onClipboardText(text) {
this._onCommandSearch({mode: 'popup', query: text});
}
_onZoomChange({tabId, oldZoomFactor, newZoomFactor}) { _onZoomChange({tabId, oldZoomFactor, newZoomFactor}) {
const callback = () => this.checkLastError(chrome.runtime.lastError); const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.sendMessage(tabId, {action: 'zoomChanged', params: {oldZoomFactor, newZoomFactor}}, callback); chrome.tabs.sendMessage(tabId, {action: 'zoomChanged', params: {oldZoomFactor, newZoomFactor}}, callback);
@ -126,18 +131,10 @@ class Backend {
this.mecab.stopListener(); this.mecab.stopListener();
} }
window.clearInterval(this.clipboardPopupTimerId);
if (options.general.enableClipboardPopups) { if (options.general.enableClipboardPopups) {
this.clipboardPopupTimerId = setInterval(() => { this.clipboardMonitor.start();
this._onApiClipboardGet() } else {
.then((result) => { this.clipboardMonitor.stop();
if (this.clipboardPreviousText === result) {
return;
}
this._onCommandSearch({mode: 'popup', query: result});
this.clipboardPreviousText = result;
});
}, this.clipboardInterval);
} }
} }