From 679e42c21ccc3ab778f4b26406b353769e171878 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 25 Jan 2020 18:11:19 +0200 Subject: move apiClipboardGet Firefox handling to Backend --- ext/bg/js/backend.js | 31 ++++++++++++++++++++++++------- ext/bg/js/search.js | 44 +++++++------------------------------------- 2 files changed, 31 insertions(+), 44 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 23811e9d..2fb7711f 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -522,13 +522,30 @@ class Backend { } async _onApiClipboardGet() { - const clipboardPasteTarget = this.clipboardPasteTarget; - clipboardPasteTarget.value = ''; - clipboardPasteTarget.focus(); - document.execCommand('paste'); - const result = clipboardPasteTarget.value; - clipboardPasteTarget.value = ''; - return result; + /* + Notes: + document.execCommand('paste') doesn't work on Firefox. + This may be a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1603985 + Therefore, navigator.clipboard.readText() is used on Firefox. + + navigator.clipboard.readText() can't be used in Chrome for two reasons: + * Requires page to be focused, else it rejects with an exception. + * When the page is focused, Chrome will request clipboard permission, despite already + being an extension with clipboard permissions. It effectively asks for the + non-extension permission for clipboard access. + */ + const browser = await Backend._getBrowser(); + if (browser === 'firefox' || browser === 'firefox-mobile') { + return await navigator.clipboard.readText(); + } else { + const clipboardPasteTarget = this.clipboardPasteTarget; + clipboardPasteTarget.value = ''; + clipboardPasteTarget.focus(); + document.execCommand('paste'); + const result = clipboardPasteTarget.value; + clipboardPasteTarget.value = ''; + return result; + } } async _onApiGetDisplayTemplatesHtml() { diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js index f5c641a8..9508346e 100644 --- a/ext/bg/js/search.js +++ b/ext/bg/js/search.js @@ -36,8 +36,6 @@ class DisplaySearch extends Display { this.introVisible = true; this.introAnimationTimer = null; - this.isFirefox = false; - this.clipboardMonitorTimerId = null; this.clipboardMonitorTimerToken = null; this.clipboardInterval = 250; @@ -53,7 +51,6 @@ class DisplaySearch extends Display { async prepare() { try { await this.initialize(); - this.isFirefox = await DisplaySearch._isFirefox(); if (this.search !== null) { this.search.addEventListener('click', (e) => this.onSearch(e), false); @@ -250,13 +247,18 @@ class DisplaySearch extends Display { startClipboardMonitor() { // 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 this.getClipboardText() + // hasn't been started during the await call. The check below the await apiClipboardGet() // call will exit early if the reference has changed. const token = {}; const intervalCallback = async () => { this.clipboardMonitorTimerId = null; - let text = await this.getClipboardText(); + let text = null; + try { + text = await apiClipboardGet(); + } catch (e) { + // NOP + } if (this.clipboardMonitorTimerToken !== token) { return; } if ( @@ -288,27 +290,6 @@ class DisplaySearch extends Display { } } - async getClipboardText() { - /* - Notes: - apiClipboardGet doesn't work on Firefox because document.execCommand('paste') - results in an empty string on the web extension background page. - This may be a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1603985 - Therefore, navigator.clipboard.readText() is used on Firefox. - - navigator.clipboard.readText() can't be used in Chrome for two reasons: - * Requires page to be focused, else it rejects with an exception. - * When the page is focused, Chrome will request clipboard permission, despite already - being an extension with clipboard permissions. It effectively asks for the - non-extension permission for clipboard access. - */ - try { - return this.isFirefox ? await navigator.clipboard.readText() : await apiClipboardGet(); - } catch (e) { - return null; - } - } - isWanakanaEnabled() { return this.wanakanaEnable !== null && this.wanakanaEnable.checked; } @@ -399,17 +380,6 @@ class DisplaySearch extends Display { const match = /^[^?#]*\?(?:[^&#]*&)?query=([^&#]*)/.exec(url); return match !== null ? decodeURIComponent(match[1]) : null; } - - static async _isFirefox() { - const {browser} = await apiGetEnvironmentInfo(); - switch (browser) { - case 'firefox': - case 'firefox-mobile': - return true; - default: - return false; - } - } } DisplaySearch.onKeyDownIgnoreKeys = { -- cgit v1.2.3