Skip to content

Commit

Permalink
Adjust storage api usage for safari compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmurray-codecov committed Oct 25, 2024
1 parent a4c016a commit d5c1fb3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/background/dynamic_content_scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export async function registerContentScript(payload: any): Promise<boolean> {
const { url } = payload;

const tabs = await browser.tabs.query({ active: true, currentWindow: true });

if (!tabs[0]?.url?.startsWith(url)) {
return false;
}
Expand Down Expand Up @@ -35,9 +36,15 @@ export async function registerContentScript(payload: any): Promise<boolean> {
export async function unregisterContentScriptIfExists(
payload: any
): Promise<boolean> {
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;
}
Expand All @@ -48,3 +55,7 @@ export async function unregisterContentScriptIfExists(

return true;
}

export async function setStorageValues(payload: any): Promise<void> {
await browser.storage.sync.set(payload);
}
9 changes: 9 additions & 0 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
browser.runtime.onMessage.addListener(handleMessages);
Expand All @@ -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);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/popup/main.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Codecov {
selfHostedGitHubURLStorageKey,
selfHostedCodecovApiToken,
]);

const useSelfHosted = result[useSelfHostedStorageKey] || false;
// self hosted not selected
if (!useSelfHosted) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

0 comments on commit d5c1fb3

Please sign in to comment.