tab url may be empty for about:blank tabs

fixes #1254
This commit is contained in:
tophf 2021-05-27 14:18:28 +03:00
parent 440a9f4763
commit 18265b94c6
3 changed files with 31 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/* global API msg */// msg.js
/* global CHROME URLS stringAsRegExp tryRegExp */// toolbox.js
/* global CHROME URLS stringAsRegExp tryRegExp tryURL */// toolbox.js
/* global bgReady compareRevision */// common.js
/* global calcStyleDigest styleCodeEmpty styleSectionGlobal */// sections-util.js
/* global db */
@ -607,14 +607,14 @@ const styleMan = (() => {
},
get urlWithoutParams() {
if (!urlWithoutParams) {
const u = createURL(url);
const u = tryURL(url);
urlWithoutParams = u.origin + u.pathname;
}
return urlWithoutParams;
},
get domain() {
if (!domain) {
const u = createURL(url);
const u = tryURL(url);
domain = u.hostname;
}
return domain;
@ -634,27 +634,6 @@ const styleMan = (() => {
}
}
function createURL(url) {
try {
return new URL(url);
} catch (err) {
return {
hash: '',
host: '',
hostname: '',
href: '',
origin: '',
password: '',
pathname: '',
port: '',
protocol: '',
search: '',
searchParams: new URLSearchParams(),
username: '',
};
}
}
function uuidv4() {
const seeds = crypto.getRandomValues(new Uint16Array(8));
// 00001111-2222-M333-N444-555566667777

View File

@ -11,8 +11,8 @@
debounce
getOwnTab
sessionStore
tryCatch
tryJSONparse
tryURL
*/// toolbox.js
'use strict';
@ -74,7 +74,7 @@ const baseInit = (() => {
const id = Number(params.get('id'));
const style = id && await API.styles.get(id) || {
name: params.get('domain') ||
tryCatch(() => new URL(params.get('url-prefix')).hostname) ||
tryURL(params.get('url-prefix')).hostname ||
'',
enabled: true,
sections: [

View File

@ -18,6 +18,7 @@
stringAsRegExp
tryCatch
tryRegExp
tryURL
waitForTabUrl
*/
@ -150,13 +151,13 @@ function urlToMatchPattern(url, ignoreSearch) {
}
async function findExistingTab({url, currentWindow, ignoreHash = true, ignoreSearch = false}) {
url = new URL(url);
url = tryURL(url);
const tabs = await browser.tabs.query({
url: urlToMatchPattern(url, ignoreSearch),
currentWindow,
});
return tabs.find(tab => {
const tabUrl = new URL(tab.pendingUrl || tab.url);
const tabUrl = tryURL(tab.pendingUrl || tab.url);
return tabUrl.protocol === url.protocol &&
tabUrl.username === url.username &&
tabUrl.password === url.password &&
@ -282,6 +283,29 @@ function tryJSONparse(jsonString) {
} catch (e) {}
}
function tryURL(
url,
fallback = {
hash: '',
host: '',
hostname: '',
href: '',
origin: '',
password: '',
pathname: '',
port: '',
protocol: '',
search: '',
searchParams: new URLSearchParams(),
username: '',
}) {
try {
return new URL(url);
} catch (e) {
return fallback;
}
}
function debounce(fn, delay, ...args) {
clearTimeout(debounce.timers.get(fn));
debounce.timers.set(fn, setTimeout(debounce.run, delay, fn, ...args));