Add api.isTabSearchPopup (#763)

* Add api.isTabSearchPopup

* Fix missing asyncs
This commit is contained in:
toasted-nutbread 2020-09-04 17:57:51 -04:00 committed by GitHub
parent 21fc0a80f2
commit 8d53474945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 13 deletions

View File

@ -125,7 +125,8 @@ class Backend {
['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}], ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}],
['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}], ['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}],
['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.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([ this._messageHandlersWithProgress = new Map([
['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}] ['deleteDictionary', {async: true, contentScript: false, handler: this._onApiDeleteDictionary.bind(this)}]
@ -800,6 +801,12 @@ class Backend {
return {tabId: tab.id, windowId: tab.windowId}; 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 // Command handlers
async _onCommandSearch(params) { async _onCommandSearch(params) {
@ -883,19 +890,9 @@ class Backend {
// Reuse same tab // Reuse same tab
const baseUrl = chrome.runtime.getURL('/bg/search.html'); const baseUrl = chrome.runtime.getURL('/bg/search.html');
if (this._searchPopupTabId !== null) { if (this._searchPopupTabId !== null) {
const tabId = this._searchPopupTabId; const tab = await this._checkTabUrl(this._searchPopupTabId, (url) => url.startsWith(baseUrl));
const tab = await new Promise((resolve) => {
chrome.tabs.get(
tabId,
(result) => { resolve(chrome.runtime.lastError ? null : result); }
);
});
if (tab !== null) { if (tab !== null) {
const url = await this._getTabUrl(tabId); return {tab, created: false};
const isValidTab = (url !== null && url.startsWith(baseUrl));
if (isValidTab) {
return {tab, created: false};
}
} }
this._searchPopupTabId = null; this._searchPopupTabId = null;
} }
@ -1521,4 +1518,18 @@ class Backend {
chrome.tabs.sendMessage(...args, callback); 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;
}
} }

View File

@ -201,6 +201,10 @@ const api = (() => {
return this._invoke('getOrCreateSearchPopup', isObject(details) ? details : {}); return this._invoke('getOrCreateSearchPopup', isObject(details) ? details : {});
} }
isTabSearchPopup(tabId) {
return this._invoke('isTabSearchPopup', {tabId});
}
// Invoke functions with progress // Invoke functions with progress
deleteDictionary(dictionaryName, onProgress) { deleteDictionary(dictionaryName, onProgress) {