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,13 +522,30 @@ class Backend {
} }
async _onApiClipboardGet() { async _onApiClipboardGet() {
const clipboardPasteTarget = this.clipboardPasteTarget; /*
clipboardPasteTarget.value = ''; Notes:
clipboardPasteTarget.focus(); document.execCommand('paste') doesn't work on Firefox.
document.execCommand('paste'); This may be a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1603985
const result = clipboardPasteTarget.value; Therefore, navigator.clipboard.readText() is used on Firefox.
clipboardPasteTarget.value = '';
return result; 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() { async _onApiGetDisplayTemplatesHtml() {

View File

@ -36,8 +36,6 @@ class DisplaySearch extends Display {
this.introVisible = true; this.introVisible = true;
this.introAnimationTimer = null; this.introAnimationTimer = null;
this.isFirefox = false;
this.clipboardMonitorTimerId = null; this.clipboardMonitorTimerId = null;
this.clipboardMonitorTimerToken = null; this.clipboardMonitorTimerToken = null;
this.clipboardInterval = 250; this.clipboardInterval = 250;
@ -53,7 +51,6 @@ class DisplaySearch extends Display {
async prepare() { async prepare() {
try { try {
await this.initialize(); await this.initialize();
this.isFirefox = await DisplaySearch._isFirefox();
if (this.search !== null) { if (this.search !== null) {
this.search.addEventListener('click', (e) => this.onSearch(e), false); this.search.addEventListener('click', (e) => this.onSearch(e), false);
@ -250,13 +247,18 @@ class DisplaySearch extends Display {
startClipboardMonitor() { startClipboardMonitor() {
// 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 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. // call will exit early if the reference has changed.
const token = {}; const token = {};
const intervalCallback = async () => { const intervalCallback = async () => {
this.clipboardMonitorTimerId = null; this.clipboardMonitorTimerId = null;
let text = await this.getClipboardText(); let text = null;
try {
text = await apiClipboardGet();
} catch (e) {
// NOP
}
if (this.clipboardMonitorTimerToken !== token) { return; } if (this.clipboardMonitorTimerToken !== token) { return; }
if ( 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() { isWanakanaEnabled() {
return this.wanakanaEnable !== null && this.wanakanaEnable.checked; return this.wanakanaEnable !== null && this.wanakanaEnable.checked;
} }
@ -399,17 +380,6 @@ class DisplaySearch extends Display {
const match = /^[^?#]*\?(?:[^&#]*&)?query=([^&#]*)/.exec(url); const match = /^[^?#]*\?(?:[^&#]*&)?query=([^&#]*)/.exec(url);
return match !== null ? decodeURIComponent(match[1]) : null; 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 = { DisplaySearch.onKeyDownIgnoreKeys = {