From 9d2854c2729c78883a9fd5347cf818c33aff7fe0 Mon Sep 17 00:00:00 2001 From: eight Date: Thu, 16 Dec 2021 21:04:22 +0800 Subject: [PATCH] Fix: compatibility with kiwi (#1368) --- background/usercss-install-helper.js | 38 +++++++++++++++++----------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/background/usercss-install-helper.js b/background/usercss-install-helper.js index a51af3a5..0773ffb5 100644 --- a/background/usercss-install-helper.js +++ b/background/usercss-install-helper.js @@ -86,7 +86,7 @@ bgReady.all.then(() => { const inTab = url.startsWith('file:') && !chrome.app; const code = await (inTab ? loadFromFile : loadFromUrl)(tabId, url); if (!/^\s* { openInstallerPage(tabId, url, {}); // Silently suppress navigation. // Don't redirect to the install URL as it'll flash the text! - return {redirectUrl: 'javascript:void 0'}; // eslint-disable-line no-script-url + return {cancel: true}; } } - function openInstallerPage(tabId, url, {code, inTab} = {}) { + async function openInstallerPage(tabId, url, {code, inTab} = {}) { const newUrl = `${URLS.installUsercss}?updateUrl=${encodeURIComponent(url)}`; if (inTab) { - browser.tabs.get(tabId).then(tab => - openURL({ - url: `${newUrl}&tabId=${tabId}`, - active: tab.active, - index: tab.index + 1, - openerTabId: tabId, - currentWindow: null, - })); - } else { - const timer = setTimeout(clearInstallCode, 10e3, url); - installCodeCache[url] = {code, timer}; - chrome.tabs.update(tabId, {url: newUrl}); + const tab = await browser.tabs.get(tabId); + return openURL({ + url: `${newUrl}&tabId=${tabId}`, + active: tab.active, + index: tab.index + 1, + openerTabId: tabId, + currentWindow: null, + }); + } + const timer = setTimeout(clearInstallCode, 10e3, url); + installCodeCache[url] = {code, timer}; + try { + await browser.tabs.update(tabId, {url: newUrl}); + } catch (err) { + // FIXME: remove this when kiwi supports tabs.update + // https://github.com/openstyles/stylus/issues/1367 + if (/Tabs cannot be edited right now/i.test(err.message)) { + return browser.tabs.create({url: newUrl}); + } + throw err; } }