From 18265b94c6a11c224c0033c3f33594a419708433 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 27 May 2021 14:18:28 +0300 Subject: [PATCH] tab `url` may be empty for about:blank tabs fixes #1254 --- background/style-manager.js | 27 +++------------------------ edit/base.js | 4 ++-- js/toolbox.js | 28 ++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index bdbf3748..d8867387 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -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 diff --git a/edit/base.js b/edit/base.js index 117394f7..84c52d9a 100644 --- a/edit/base.js +++ b/edit/base.js @@ -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: [ diff --git a/js/toolbox.js b/js/toolbox.js index 589a35e9..0a1cfb34 100644 --- a/js/toolbox.js +++ b/js/toolbox.js @@ -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));