Merge pull request #397 from toasted-nutbread/clipboard-monitor-refactor2
Clipboard monitor refactor
This commit is contained in:
commit
b8eb5e6016
@ -25,10 +25,6 @@ function apiAudioGetUrl(definition, source, optionsContext) {
|
|||||||
return _apiInvoke('audioGetUrl', {definition, source, optionsContext});
|
return _apiInvoke('audioGetUrl', {definition, source, optionsContext});
|
||||||
}
|
}
|
||||||
|
|
||||||
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) => {
|
||||||
|
@ -30,7 +30,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.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)});
|
||||||
this.options = null;
|
this.options = null;
|
||||||
this.optionsSchema = null;
|
this.optionsSchema = null;
|
||||||
this.defaultAnkiFieldTemplates = null;
|
this.defaultAnkiFieldTemplates = null;
|
||||||
@ -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');
|
||||||
const callback = () => this.checkLastError(chrome.runtime.lastError);
|
const callback = () => this.checkLastError(chrome.runtime.lastError);
|
||||||
@ -155,7 +155,7 @@ class Backend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_onClipboardText(text) {
|
_onClipboardText({text}) {
|
||||||
this._onCommandSearch({mode: 'popup', query: text});
|
this._onCommandSearch({mode: 'popup', query: text});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,66 +16,64 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*global apiClipboardGet, jpIsStringPartiallyJapanese*/
|
/*global jpIsStringPartiallyJapanese*/
|
||||||
|
|
||||||
class ClipboardMonitor {
|
class ClipboardMonitor extends EventDispatcher {
|
||||||
constructor() {
|
constructor({getClipboard}) {
|
||||||
this.timerId = null;
|
super();
|
||||||
this.timerToken = null;
|
this._timerId = null;
|
||||||
this.interval = 250;
|
this._timerToken = null;
|
||||||
this.previousText = null;
|
this._interval = 250;
|
||||||
}
|
this._previousText = null;
|
||||||
|
this._getClipboard = getClipboard;
|
||||||
onClipboardText(_text) {
|
|
||||||
throw new Error('Override me');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
this.stop();
|
this.stop();
|
||||||
|
|
||||||
// The token below is used as a unique identifier to ensure that a new clipboard monitor
|
// The token below is used as a unique identifier to ensure that a new clipboard monitor
|
||||||
// hasn't been started during the await call. The check below the await apiClipboardGet()
|
// hasn't been started during the await call. The check below the await this._getClipboard()
|
||||||
// call will exit early if the reference has changed.
|
// call will exit early if the reference has changed.
|
||||||
const token = {};
|
const token = {};
|
||||||
const intervalCallback = async () => {
|
const intervalCallback = async () => {
|
||||||
this.timerId = null;
|
this._timerId = null;
|
||||||
|
|
||||||
let text = null;
|
let text = null;
|
||||||
try {
|
try {
|
||||||
text = await apiClipboardGet();
|
text = await this._getClipboard();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// NOP
|
// NOP
|
||||||
}
|
}
|
||||||
if (this.timerToken !== token) { return; }
|
if (this._timerToken !== token) { return; }
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof text === 'string' &&
|
typeof text === 'string' &&
|
||||||
(text = text.trim()).length > 0 &&
|
(text = text.trim()).length > 0 &&
|
||||||
text !== this.previousText
|
text !== this._previousText
|
||||||
) {
|
) {
|
||||||
this.previousText = text;
|
this._previousText = text;
|
||||||
if (jpIsStringPartiallyJapanese(text)) {
|
if (jpIsStringPartiallyJapanese(text)) {
|
||||||
this.onClipboardText(text);
|
this.trigger('change', {text});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.timerId = setTimeout(intervalCallback, this.interval);
|
this._timerId = setTimeout(intervalCallback, this._interval);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.timerToken = token;
|
this._timerToken = token;
|
||||||
|
|
||||||
intervalCallback();
|
intervalCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
this.timerToken = null;
|
this._timerToken = null;
|
||||||
if (this.timerId !== null) {
|
if (this._timerId !== null) {
|
||||||
clearTimeout(this.timerId);
|
clearTimeout(this._timerId);
|
||||||
this.timerId = null;
|
this._timerId = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setPreviousText(text) {
|
setPreviousText(text) {
|
||||||
this.previousText = text;
|
this._previousText = text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*global apiOptionsSet, apiTermsFind, Display, QueryParser, ClipboardMonitor*/
|
/*global apiOptionsSet, apiTermsFind, apiClipboardGet, Display, QueryParser, ClipboardMonitor*/
|
||||||
|
|
||||||
class DisplaySearch extends Display {
|
class DisplaySearch extends Display {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -38,7 +38,7 @@ class DisplaySearch extends Display {
|
|||||||
this.introVisible = true;
|
this.introVisible = true;
|
||||||
this.introAnimationTimer = null;
|
this.introAnimationTimer = null;
|
||||||
|
|
||||||
this.clipboardMonitor = new ClipboardMonitor();
|
this.clipboardMonitor = new ClipboardMonitor({getClipboard: apiClipboardGet});
|
||||||
|
|
||||||
this._onKeyDownIgnoreKeys = new Map([
|
this._onKeyDownIgnoreKeys = new Map([
|
||||||
['ANY_MOD', new Set([
|
['ANY_MOD', new Set([
|
||||||
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user