diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-09-04 17:57:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-04 17:57:51 -0400 |
commit | 8d534749456c9275686f260db82e9414481dcb2b (patch) | |
tree | a0fa4981113a78973bef8131cb2521db800f2d40 /ext/bg | |
parent | 21fc0a80f2382f383ae5df221b17313e72b809db (diff) |
Add api.isTabSearchPopup (#763)
* Add api.isTabSearchPopup
* Fix missing asyncs
Diffstat (limited to 'ext/bg')
-rw-r--r-- | ext/bg/js/backend.js | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 0c901c68..2c741dd7 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -125,7 +125,8 @@ class Backend { ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}], ['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}], ['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}], - ['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}] + ['getOrCreateSearchPopup', {async: true, contentScript: true, handler: this._onApiGetOrCreateSearchPopup.bind(this)}], + ['isTabSearchPopup', {async: true, contentScript: true, handler: this._onApiIsTabSearchPopup.bind(this)}] ]); this._messageHandlersWithProgress = new Map([ ['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}] @@ -800,6 +801,12 @@ class Backend { return {tabId: tab.id, windowId: tab.windowId}; } + async _onApiIsTabSearchPopup({tabId}) { + const baseUrl = chrome.runtime.getURL('/bg/search.html'); + const tab = typeof tabId === 'number' ? await this._checkTabUrl(tabId, (url) => url.startsWith(baseUrl)) : null; + return (tab !== null); + } + // Command handlers async _onCommandSearch(params) { @@ -883,19 +890,9 @@ class Backend { // Reuse same tab const baseUrl = chrome.runtime.getURL('/bg/search.html'); if (this._searchPopupTabId !== null) { - const tabId = this._searchPopupTabId; - const tab = await new Promise((resolve) => { - chrome.tabs.get( - tabId, - (result) => { resolve(chrome.runtime.lastError ? null : result); } - ); - }); + const tab = await this._checkTabUrl(this._searchPopupTabId, (url) => url.startsWith(baseUrl)); if (tab !== null) { - const url = await this._getTabUrl(tabId); - const isValidTab = (url !== null && url.startsWith(baseUrl)); - if (isValidTab) { - return {tab, created: false}; - } + return {tab, created: false}; } this._searchPopupTabId = null; } @@ -1521,4 +1518,18 @@ class Backend { chrome.tabs.sendMessage(...args, callback); }); } + + async _checkTabUrl(tabId, urlPredicate) { + const tab = await new Promise((resolve) => { + chrome.tabs.get( + tabId, + (result) => { resolve(chrome.runtime.lastError ? null : result); } + ); + }); + if (tab === null) { return null; } + + const url = await this._getTabUrl(tabId); + const isValidTab = urlPredicate(url); + return isValidTab ? tab : null; + } } |