From 83687065257b27d5e70475ced050a08af355f4ad Mon Sep 17 00:00:00 2001 From: wanhose Date: Sat, 5 Oct 2024 12:30:42 +0200 Subject: [PATCH] fix(browser-extension): browser.webRequest.onErrorOccurred listener --- .../src/scripts/background.js | 52 +++++++++++++------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/packages/browser-extension/src/scripts/background.js b/packages/browser-extension/src/scripts/background.js index 169204e..069ddc2 100644 --- a/packages/browser-extension/src/scripts/background.js +++ b/packages/browser-extension/src/scripts/background.js @@ -44,7 +44,35 @@ const storage = browser.storage.local; const suppressLastError = () => void browser.runtime.lastError; /** - * @description Map match to pattern format + * @description Calculate current hostname + * @param {string} url + * @returns {string} + */ +function getHostname(url) { + return new URL(url).hostname.split('.').slice(-3).join('.').replace('www.', ''); +} + +/** + * @description Format number to avoid errors + * @param {number} [value] + * @returns {string | null} + */ +function formatNumber(value) { + if (value) { + if (value >= 1e6) { + return `${Math.floor(value / 1e6)}M`; + } else if (value >= 1e3) { + return `${Math.floor(value / 1e3)}K`; + } else { + return value.toString(); + } + } + + return null; +} + +/** + * @description Convert match string to pattern string * @param {string} match * @returns {string} */ @@ -182,7 +210,7 @@ browser.runtime.onMessage.addListener((message, sender, callback) => { case 'SET_BADGE': if (isPage && tabId !== undefined) { browser.action.setBadgeBackgroundColor({ color: '#6b7280' }); - browser.action.setBadgeText({ tabId, text: message.value ? `${message.value}` : null }); + browser.action.setBadgeText({ tabId, text: formatNumber(message.value) }); } break; case 'SET_HOSTNAME_STATE': @@ -257,9 +285,8 @@ browser.webRequest.onBeforeRequest.addListener( return; } - const hostname = new URL(url).hostname.split('.').slice(-3).join('.').replace('www.', ''); - const { data, ...store } = await storage.get(['data', hostname]); - const state = store[hostname] ?? { enabled: true }; + const hostname = getHostname(url); + const { data, [hostname]: state = { enabled: true } } = await storage.get(['data', hostname]); if (data?.rules?.length) { const rules = data.rules.map((rule) => ({ @@ -284,17 +311,12 @@ browser.webRequest.onErrorOccurred.addListener( async (details) => { const { error, tabId, url } = details; - if (tabId > -1) { - const hostname = new URL(url).hostname.split('.').slice(-3).join('.').replace('www.', ''); - const { data, ...store } = await storage.get(['data', hostname]); - const state = store[hostname] ?? { enabled: true }; + if (error === 'net::ERR_BLOCKED_BY_CLIENT' && tabId > -1) { + const hostname = getHostname(url); + const { [hostname]: state = { enabled: true } } = await storage.get(hostname); - if (error === 'net::ERR_BLOCKED_BY_CLIENT' && state.enabled) { - const sessionRules = await browser.declarativeNetRequest.getSessionRules(); - - if (sessionRules.some((rule) => new RegExp(rule.condition.urlFilter).test(url))) { - await browser.tabs.sendMessage(tabId, { type: 'INCREASE_ACTIONS_COUNT' }); - } + if (state.enabled) { + await browser.tabs.sendMessage(tabId, { type: 'INCREASE_ACTIONS_COUNT' }); } } },