diff --git a/src/background/dynamic_content_scripts.ts b/src/background/dynamic_content_scripts.ts index 64d939a..2acfe1a 100644 --- a/src/background/dynamic_content_scripts.ts +++ b/src/background/dynamic_content_scripts.ts @@ -8,6 +8,7 @@ export async function registerContentScript(payload: any): Promise { const { url } = payload; const tabs = await browser.tabs.query({ active: true, currentWindow: true }); + if (!tabs[0]?.url?.startsWith(url)) { return false; } @@ -35,9 +36,15 @@ export async function registerContentScript(payload: any): Promise { export async function unregisterContentScriptIfExists( payload: any ): Promise { - const registrations = await browser.scripting.getRegisteredContentScripts({ - ids: [dynamicContentScriptRegistrationId], - }); + let registrations: browser.Scripting.RegisteredContentScript[]; + try { + registrations = await browser.scripting.getRegisteredContentScripts({ + ids: [dynamicContentScriptRegistrationId], + }); + } catch (error) { + return true; + } + if (registrations.length === 0) { return true; } @@ -48,3 +55,7 @@ export async function unregisterContentScriptIfExists( return true; } + +export async function setStorageValues(payload: any): Promise { + await browser.storage.sync.set(payload); +} diff --git a/src/background/main.ts b/src/background/main.ts index 45b6041..46c8dd9 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -4,8 +4,15 @@ import { MessageType } from "src/types"; import { Codecov } from "src/service"; import { registerContentScript, + setStorageValues, unregisterContentScriptIfExists, } from "./dynamic_content_scripts"; +import { + selfHostedCodecovApiToken, + selfHostedCodecovURLStorageKey, + selfHostedGitHubURLStorageKey, + useSelfHostedStorageKey, +} from "src/constants"; async function main(): Promise { browser.runtime.onMessage.addListener(handleMessages); @@ -31,6 +38,8 @@ async function handleMessages(message: { return registerContentScript(message.payload); case MessageType.UNREGISTER_CONTENT_SCRIPTS: return unregisterContentScriptIfExists(message.payload); + case MessageType.SET_STORAGE_VALUES: + return setStorageValues(message.payload); } } diff --git a/src/popup/main.tsx b/src/popup/main.tsx index 6b6b6c1..25e6d82 100644 --- a/src/popup/main.tsx +++ b/src/popup/main.tsx @@ -1,4 +1,4 @@ -import React, { ChangeEvent, useEffect, useState } from "react"; +import React, { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import browser from "webextension-polyfill"; import clsx from "clsx"; @@ -142,11 +142,14 @@ const Popup = () => { } } - await browser.storage.sync.set({ - [useSelfHostedStorageKey]: useSelfHosted, - [selfHostedCodecovURLStorageKey]: codecovUrl, - [selfHostedGitHubURLStorageKey]: githubUrl, - [selfHostedCodecovApiToken]: codecovApiToken, + await browser.runtime.sendMessage({ + type: MessageType.SET_STORAGE_VALUES, + payload: { + [useSelfHostedStorageKey]: useSelfHosted, + [selfHostedCodecovURLStorageKey]: codecovUrl, + [selfHostedGitHubURLStorageKey]: githubUrl, + [selfHostedCodecovApiToken]: codecovApiToken, + }, }); resetEphemeralState(); diff --git a/src/service.ts b/src/service.ts index 9aa0c16..b98773f 100644 --- a/src/service.ts +++ b/src/service.ts @@ -27,6 +27,7 @@ export class Codecov { selfHostedGitHubURLStorageKey, selfHostedCodecovApiToken, ]); + const useSelfHosted = result[useSelfHostedStorageKey] || false; // self hosted not selected if (!useSelfHosted) { diff --git a/src/types.ts b/src/types.ts index 769435e..801ce51 100644 --- a/src/types.ts +++ b/src/types.ts @@ -43,4 +43,5 @@ export enum MessageType { FETCH_COMPONENTS_LIST = "fetch_components_list", REGISTER_CONTENT_SCRIPTS = "register_content_scripts", UNREGISTER_CONTENT_SCRIPTS = "unregister_content_scripts", + SET_STORAGE_VALUES = "set_storage_values", }