From 48776145d6bdb8aff82e82546583c790353e75b6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sun, 27 Oct 2019 15:46:27 +0200 Subject: [PATCH] add workaround to Chrome clipboard.readText For some reason this doesn't work on Firefox, so keep using the new API for Firefox --- ext/bg/background.html | 2 ++ ext/bg/js/api.js | 8 ++++++++ ext/bg/js/backend.js | 5 ++++- ext/bg/js/search.js | 14 +++++++++++++- ext/fg/js/api.js | 4 ++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ext/bg/background.html b/ext/bg/background.html index 194d4a45..30b3db48 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -5,6 +5,8 @@ +
+ diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 999ea337..88eef431 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -401,3 +401,11 @@ async function apiFocusTab(tab) { // Edge throws exception for no reason here. } } + +async function apiClipboardGet() { + const clipboardPasteTarget = utilBackend().clipboardPasteTarget; + clipboardPasteTarget.innerText = ''; + clipboardPasteTarget.focus(); + document.execCommand('paste'); + return clipboardPasteTarget.innerText; +} diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 23d876f6..7192d026 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -30,6 +30,8 @@ class Backend { this.isPreparedResolve = null; this.isPreparedPromise = new Promise((resolve) => (this.isPreparedResolve = resolve)); + this.clipboardPasteTarget = document.querySelector('#clipboard-paste-target'); + this.apiForwarder = new BackendApiForwarder(); } @@ -187,7 +189,8 @@ Backend.messageHandlers = { forward: ({action, params}, sender) => apiForward(action, params, sender), frameInformationGet: (params, sender) => apiFrameInformationGet(sender), injectStylesheet: ({css}, sender) => apiInjectStylesheet(css, sender), - getEnvironmentInfo: () => apiGetEnvironmentInfo() + getEnvironmentInfo: () => apiGetEnvironmentInfo(), + clipboardGet: () => apiClipboardGet() }; window.yomichan_backend = new Backend(); diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index a09ca822..dca4e8fa 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -17,6 +17,12 @@ */ +let IS_FIREFOX = null; +(async () => { + const {browser} = await apiGetEnvironmentInfo(); + IS_FIREFOX = ['firefox', 'firefox-mobile'].includes(browser); +})(); + class DisplaySearch extends Display { constructor() { super(document.querySelector('#spinner'), document.querySelector('#content')); @@ -235,7 +241,13 @@ class DisplaySearch extends Display { startClipboardMonitor() { this.clipboardMonitorIntervalId = setInterval(async () => { - const curText = (await navigator.clipboard.readText()).trim(); + let curText = null; + // TODO get rid of this and figure out why apiClipboardGet doesn't work on Firefox + if (IS_FIREFOX) { + curText = (await navigator.clipboard.readText()).trim(); + } else if (IS_FIREFOX === false) { + curText = (await apiClipboardGet()).trim(); + } if (curText && (curText !== this.clipboardPrevText)) { if (this.isWanakanaEnabled()) { this.query.value = window.wanakana.toKana(curText); diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js index b0746b85..bbc9b5fc 100644 --- a/ext/fg/js/api.js +++ b/ext/fg/js/api.js @@ -72,3 +72,7 @@ function apiInjectStylesheet(css) { function apiGetEnvironmentInfo() { return utilInvoke('getEnvironmentInfo'); } + +function apiClipboardGet() { + return utilInvoke('clipboardGet'); +}