diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2020-02-10 00:39:05 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2020-02-10 00:39:05 +0200 |
commit | 89729d8c20e8d1113e640a46f448a9734da1fe56 (patch) | |
tree | cf7eb0f9310587e802c9f4ffdf79081548e84d58 | |
parent | 56f1f8384dba7da6f1373768129bd37c24147520 (diff) |
reuse existing popup window
-rw-r--r-- | ext/bg/js/backend.js | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 9565a8d9..adfb4f10 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -614,17 +614,21 @@ class Backend { return baseUrl2 === baseUrl && (queryParams2.mode === mode || (!queryParams2.mode && mode === 'existingOrNewTab')); }; + const openInTab = async () => { + const tab = await Backend._findTab(1000, isTabMatch); + if (tab !== null) { + await Backend._focusTab(tab); + if (queryParams.query) { + await new Promise((resolve) => chrome.tabs.update(tab.id, {url}, resolve)); + } + return true; + } + }; + switch (mode) { case 'existingOrNewTab': try { - const tab = await Backend._findTab(1000, isTabMatch); - if (tab !== null) { - await Backend._focusTab(tab); - if (queryParams.query) { - await new Promise((resolve) => chrome.tabs.update(tab.id, {url}, resolve)); - } - return; - } + if (await openInTab()) { return; } } catch (e) { // NOP } @@ -634,18 +638,23 @@ class Backend { chrome.tabs.create({url}); return; case 'popup': - if (!isObject(chrome.windows)) { + try { // chrome.windows not supported (e.g. on Firefox mobile) - return; - } - if (this.popupWindow !== null) { - const callback = () => this.checkLastError(chrome.runtime.lastError); - chrome.windows.remove(this.popupWindow.id, callback); + if (!isObject(chrome.windows)) { return; } + if (await openInTab()) { return; } + // if the previous popup is open in an invalid state, close it + if (this.popupWindow !== null) { + const callback = () => this.checkLastError(chrome.runtime.lastError); + chrome.windows.remove(this.popupWindow.id, callback); + } + // open new popup + this.popupWindow = await new Promise((resolve) => chrome.windows.create( + {url, width: popupWidth, height: popupHeight, type: 'popup'}, + resolve + )); + } catch (e) { + // NOP } - this.popupWindow = await new Promise((resolve) => chrome.windows.create( - {url, width: popupWidth, height: popupHeight, type: 'popup'}, - resolve - )); return; } } |