preserve current URL params in openManage

This commit is contained in:
tophf 2022-09-06 00:54:52 +03:00
parent 3aae3f181a
commit b45825c015
2 changed files with 22 additions and 59 deletions

View File

@ -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<chrome.tabs.Tab>} */
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
},
/**

View File

@ -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<chrome.tabs.Tab>} 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,