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 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 bgReady compareRevision */// common.js
/* global calcStyleDigest styleCodeEmpty styleSectionGlobal */// sections-util.js /* global calcStyleDigest styleCodeEmpty styleSectionGlobal */// sections-util.js
/* global db */ /* global db */
@ -607,14 +607,14 @@ const styleMan = (() => {
}, },
get urlWithoutParams() { get urlWithoutParams() {
if (!urlWithoutParams) { if (!urlWithoutParams) {
const u = createURL(url); const u = tryURL(url);
urlWithoutParams = u.origin + u.pathname; urlWithoutParams = u.origin + u.pathname;
} }
return urlWithoutParams; return urlWithoutParams;
}, },
get domain() { get domain() {
if (!domain) { if (!domain) {
const u = createURL(url); const u = tryURL(url);
domain = u.hostname; domain = u.hostname;
} }
return domain; 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() { function uuidv4() {
const seeds = crypto.getRandomValues(new Uint16Array(8)); const seeds = crypto.getRandomValues(new Uint16Array(8));
// 00001111-2222-M333-N444-555566667777 // 00001111-2222-M333-N444-555566667777

View File

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

View File

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