From d5c1fb330ca4494e439ea1a5bccf46c4297217da Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Fri, 25 Oct 2024 15:54:19 -0400 Subject: [PATCH 1/3] Adjust storage api usage for safari compatibility --- src/background/dynamic_content_scripts.ts | 17 ++++++++++++++--- src/background/main.ts | 9 +++++++++ src/popup/main.tsx | 15 +++++++++------ src/service.ts | 1 + src/types.ts | 1 + 5 files changed, 34 insertions(+), 9 deletions(-) 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", } From 8a4c236abced330cf1aa3cc6c223c95016f27e1a Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Fri, 25 Oct 2024 16:53:23 -0400 Subject: [PATCH 2/3] Do it better --- src/background/dynamic_content_scripts.ts | 4 ---- src/background/main.ts | 9 --------- src/popup/main.tsx | 13 +++++-------- src/types.ts | 1 - 4 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/background/dynamic_content_scripts.ts b/src/background/dynamic_content_scripts.ts index 2acfe1a..ae8c58d 100644 --- a/src/background/dynamic_content_scripts.ts +++ b/src/background/dynamic_content_scripts.ts @@ -55,7 +55,3 @@ 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 46c8dd9..45b6041 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -4,15 +4,8 @@ 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); @@ -38,8 +31,6 @@ 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 25e6d82..8cd3f5a 100644 --- a/src/popup/main.tsx +++ b/src/popup/main.tsx @@ -142,14 +142,11 @@ const Popup = () => { } } - await browser.runtime.sendMessage({ - type: MessageType.SET_STORAGE_VALUES, - payload: { - [useSelfHostedStorageKey]: useSelfHosted, - [selfHostedCodecovURLStorageKey]: codecovUrl, - [selfHostedGitHubURLStorageKey]: githubUrl, - [selfHostedCodecovApiToken]: codecovApiToken, - }, + await browser.storage.sync.set({ + [useSelfHostedStorageKey]: useSelfHosted, + [selfHostedCodecovURLStorageKey]: codecovUrl, + [selfHostedGitHubURLStorageKey]: githubUrl, + [selfHostedCodecovApiToken]: codecovApiToken, }); resetEphemeralState(); diff --git a/src/types.ts b/src/types.ts index 801ce51..769435e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -43,5 +43,4 @@ 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", } From 30303130751b5ae083bb6f73a9f86a3ce91156ee Mon Sep 17 00:00:00 2001 From: Spencer Murray Date: Mon, 28 Oct 2024 12:21:43 -0400 Subject: [PATCH 3/3] Make error handling better --- src/background/dynamic_content_scripts.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/background/dynamic_content_scripts.ts b/src/background/dynamic_content_scripts.ts index ae8c58d..3c926b8 100644 --- a/src/background/dynamic_content_scripts.ts +++ b/src/background/dynamic_content_scripts.ts @@ -41,8 +41,18 @@ export async function unregisterContentScriptIfExists( registrations = await browser.scripting.getRegisteredContentScripts({ ids: [dynamicContentScriptRegistrationId], }); - } catch (error) { - return true; + } catch (error: any) { + // Safari throws if the script id doesn't exist, so handle that gracefully + if ( + error instanceof Error && + error.message.match( + /Invalid call to scripting.getRegisteredContentScripts\(\)\. No script with ID '.*'/ + ) + ) { + return true; + } else { + throw error; + } } if (registrations.length === 0) {