From b45825c01528dd961048924b1c457545764300a8 Mon Sep 17 00:00:00 2001 From: tophf Date: Tue, 6 Sep 2022 00:54:52 +0300 Subject: [PATCH] preserve current URL params in openManage --- background/background.js | 41 +++++++++++++++++++--------------------- js/toolbox.js | 40 +++------------------------------------ 2 files changed, 22 insertions(+), 59 deletions(-) diff --git a/background/background.js b/background/background.js index 16c60801..8e54a8b2 100644 --- a/background/background.js +++ b/background/background.js @@ -8,7 +8,7 @@ /* global usercssMan */ /* global usoApi */ /* global uswApi */ -/* global FIREFOX UA activateTab findExistingTab openURL */ // toolbox.js +/* global FIREFOX UA activateTab openURL */ // toolbox.js /* global colorScheme */ // color-scheme.js 'use strict'; @@ -87,28 +87,25 @@ addAPI(/** @namespace API */ { /** @returns {Promise} */ async openManage({options = false, search, searchMode} = {}) { - let url = chrome.runtime.getURL('manage.html'); - if (search) { - url += `?search=${encodeURIComponent(search)}&searchMode=${searchMode}`; + const setUrlParams = url => { + const u = new URL(url); + if (search) u.searchParams.set('search', search); + if (searchMode) u.searchParams.set('searchMode', searchMode); + if (options) u.hash = '#stylus-options'; + return u.href; + }; + const base = chrome.runtime.getURL('manage.html'); + const url = setUrlParams(base); + const tabs = await browser.tabs.query({url: base + '*'}); + const same = tabs.find(t => t.url === url); + let tab = same || tabs[0]; + if (!tab) { + API.prefsDb.get('badFavs'); // prime the cache to avoid flicker/delay when opening the page + tab = await openURL({url, newTab: true}); + } else if (!same) { + msg.sendTab(tab.id, {method: 'pushState', url: setUrlParams(tab.url)}); } - if (options) { - url += '#stylus-options'; - } - const tab = await findExistingTab({ - url, - currentWindow: null, - ignoreHash: true, - ignoreSearch: true, - }); - if (tab) { - await activateTab(tab); - if (url !== (tab.pendingUrl || tab.url)) { - await msg.sendTab(tab.id, {method: 'pushState', url}).catch(console.error); - } - return tab; - } - API.prefsDb.get('badFavs'); // prime the cache to avoid flicker/delay when opening the page - return openURL({url, ignoreExisting: true}).then(activateTab); // activateTab unminimizes the window + return activateTab(tab); // activateTab unminimizes the window }, /** diff --git a/js/toolbox.js b/js/toolbox.js index 9afedf9f..ea153a6d 100644 --- a/js/toolbox.js +++ b/js/toolbox.js @@ -160,40 +160,6 @@ async function getActiveTab() { return (await browser.tabs.query({currentWindow: true, active: true}))[0]; } -function urlToMatchPattern(url, ignoreSearch) { - // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns - if (!/^(http|https|ws|wss|ftp|data|file)$/.test(url.protocol)) { - return undefined; - } - if (ignoreSearch) { - return [ - `${url.protocol}//${url.hostname}/${url.pathname}`, - `${url.protocol}//${url.hostname}/${url.pathname}?*`, - ]; - } - // FIXME: is %2f allowed in pathname and search? - return `${url.protocol}//${url.hostname}/${url.pathname}${url.search}`; -} - -async function findExistingTab({url, currentWindow, ignoreHash = true, ignoreSearch = false}) { - url = tryURL(url); - const tabs = await browser.tabs.query({ - url: urlToMatchPattern(url, ignoreSearch), - currentWindow, - }); - return tabs.find(tab => { - const tabUrl = tryURL(tab.pendingUrl || tab.url); - return tabUrl.protocol === url.protocol && - tabUrl.username === url.username && - tabUrl.password === url.password && - tabUrl.hostname === url.hostname && - tabUrl.port === url.port && - tabUrl.pathname === url.pathname && - (ignoreSearch || tabUrl.search === url.search) && - (ignoreHash || tabUrl.hash === url.hash); - }); -} - /** * Opens a tab or activates an existing one, * reuses the New Tab page or about:blank if it's focused now @@ -204,7 +170,7 @@ async function findExistingTab({url, currentWindow, ignoreHash = true, ignoreSea * @param {Boolean} [_.active=true] `true` to activate the tab * @param {Boolean|null} [_.currentWindow=true] `null` to check all windows * @param {chrome.windows.CreateData} [_.newWindow] creates a new window with these params if specified - * @param {boolean} [_.ignoreExisting] specify to skip findExistingTab + * @param {boolean} [_.newTab] `true` to force a new tab instead of switching to an existing tab * @returns {Promise} Promise -> opened/activated tab */ async function openURL({ @@ -214,12 +180,12 @@ async function openURL({ active = true, currentWindow = true, newWindow, - ignoreExisting, + newTab, }) { if (!url.includes('://')) { url = chrome.runtime.getURL(url); } - let tab = !ignoreExisting && await findExistingTab({url, currentWindow}); + let tab = !newTab && (await browser.tabs.query({url: url.split('#')[0], currentWindow}))[0]; if (tab) { return activateTab(tab, { index,