preserve current URL params in openManage
This commit is contained in:
parent
3aae3f181a
commit
b45825c015
|
@ -8,7 +8,7 @@
|
||||||
/* global usercssMan */
|
/* global usercssMan */
|
||||||
/* global usoApi */
|
/* global usoApi */
|
||||||
/* global uswApi */
|
/* global uswApi */
|
||||||
/* global FIREFOX UA activateTab findExistingTab openURL */ // toolbox.js
|
/* global FIREFOX UA activateTab openURL */ // toolbox.js
|
||||||
/* global colorScheme */ // color-scheme.js
|
/* global colorScheme */ // color-scheme.js
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
@ -87,28 +87,25 @@ addAPI(/** @namespace API */ {
|
||||||
|
|
||||||
/** @returns {Promise<chrome.tabs.Tab>} */
|
/** @returns {Promise<chrome.tabs.Tab>} */
|
||||||
async openManage({options = false, search, searchMode} = {}) {
|
async openManage({options = false, search, searchMode} = {}) {
|
||||||
let url = chrome.runtime.getURL('manage.html');
|
const setUrlParams = url => {
|
||||||
if (search) {
|
const u = new URL(url);
|
||||||
url += `?search=${encodeURIComponent(search)}&searchMode=${searchMode}`;
|
if (search) u.searchParams.set('search', search);
|
||||||
}
|
if (searchMode) u.searchParams.set('searchMode', searchMode);
|
||||||
if (options) {
|
if (options) u.hash = '#stylus-options';
|
||||||
url += '#stylus-options';
|
return u.href;
|
||||||
}
|
};
|
||||||
const tab = await findExistingTab({
|
const base = chrome.runtime.getURL('manage.html');
|
||||||
url,
|
const url = setUrlParams(base);
|
||||||
currentWindow: null,
|
const tabs = await browser.tabs.query({url: base + '*'});
|
||||||
ignoreHash: true,
|
const same = tabs.find(t => t.url === url);
|
||||||
ignoreSearch: true,
|
let tab = same || tabs[0];
|
||||||
});
|
if (!tab) {
|
||||||
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
|
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
|
tab = await openURL({url, newTab: true});
|
||||||
|
} else if (!same) {
|
||||||
|
msg.sendTab(tab.id, {method: 'pushState', url: setUrlParams(tab.url)});
|
||||||
|
}
|
||||||
|
return activateTab(tab); // activateTab unminimizes the window
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -160,40 +160,6 @@ async function getActiveTab() {
|
||||||
return (await browser.tabs.query({currentWindow: true, active: true}))[0];
|
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,
|
* Opens a tab or activates an existing one,
|
||||||
* reuses the New Tab page or about:blank if it's focused now
|
* 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} [_.active=true] `true` to activate the tab
|
||||||
* @param {Boolean|null} [_.currentWindow=true] `null` to check all windows
|
* @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 {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
|
* @returns {Promise<chrome.tabs.Tab>} Promise -> opened/activated tab
|
||||||
*/
|
*/
|
||||||
async function openURL({
|
async function openURL({
|
||||||
|
@ -214,12 +180,12 @@ async function openURL({
|
||||||
active = true,
|
active = true,
|
||||||
currentWindow = true,
|
currentWindow = true,
|
||||||
newWindow,
|
newWindow,
|
||||||
ignoreExisting,
|
newTab,
|
||||||
}) {
|
}) {
|
||||||
if (!url.includes('://')) {
|
if (!url.includes('://')) {
|
||||||
url = chrome.runtime.getURL(url);
|
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) {
|
if (tab) {
|
||||||
return activateTab(tab, {
|
return activateTab(tab, {
|
||||||
index,
|
index,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user