move apiClipboardGet Firefox handling to Backend

This commit is contained in:
siikamiika 2020-01-25 18:11:19 +02:00
parent ddc7c71e4f
commit 679e42c21c
2 changed files with 31 additions and 44 deletions

View File

@ -522,6 +522,22 @@ class Backend {
}
async _onApiClipboardGet() {
/*
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();
@ -530,6 +546,7 @@ class Backend {
clipboardPasteTarget.value = '';
return result;
}
}
async _onApiGetDisplayTemplatesHtml() {
const url = chrome.runtime.getURL('/mixed/display-templates.html');

View File

@ -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 = {