From dd3334ed218e16255478e96532d36a77830edff6 Mon Sep 17 00:00:00 2001 From: eight Date: Mon, 18 Mar 2019 23:27:29 +0800 Subject: [PATCH] Fix: handle invalid URLs --- background/style-manager.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index ddb82146..f0601573 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -45,6 +45,21 @@ const styleManager = (() => { const compileSloppyRe = createCompiler(text => `^${text}$`); const compileExclusion = createCompiler(buildGlob); + const DUMMY_URL = { + hash: '', + host: '', + hostname: '', + href: '', + origin: '', + password: '', + pathname: '', + port: '', + protocol: '', + search: '', + searchParams: new URLSearchParams(), + username: '' + }; + handleLivePreviewConnections(); return ensurePrepared({ @@ -555,18 +570,26 @@ const styleManager = (() => { }, get urlWithoutParams() { if (!urlWithoutParams) { - const u = new URL(url); + const u = createURL(url); urlWithoutParams = u.origin + u.pathname; } return urlWithoutParams; }, get domain() { if (!domain) { - const u = new URL(url); + const u = createURL(url); domain = u.hostname; } return domain; } }; } + + function createURL(url) { + try { + return new URL(url); + } catch (err) { + return DUMMY_URL; + } + } })();