Skip to content

Commit d5c1fb3

Browse files
committed
Adjust storage api usage for safari compatibility
1 parent a4c016a commit d5c1fb3

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

src/background/dynamic_content_scripts.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export async function registerContentScript(payload: any): Promise<boolean> {
88
const { url } = payload;
99

1010
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
11+
1112
if (!tabs[0]?.url?.startsWith(url)) {
1213
return false;
1314
}
@@ -35,9 +36,15 @@ export async function registerContentScript(payload: any): Promise<boolean> {
3536
export async function unregisterContentScriptIfExists(
3637
payload: any
3738
): Promise<boolean> {
38-
const registrations = await browser.scripting.getRegisteredContentScripts({
39-
ids: [dynamicContentScriptRegistrationId],
40-
});
39+
let registrations: browser.Scripting.RegisteredContentScript[];
40+
try {
41+
registrations = await browser.scripting.getRegisteredContentScripts({
42+
ids: [dynamicContentScriptRegistrationId],
43+
});
44+
} catch (error) {
45+
return true;
46+
}
47+
4148
if (registrations.length === 0) {
4249
return true;
4350
}
@@ -48,3 +55,7 @@ export async function unregisterContentScriptIfExists(
4855

4956
return true;
5057
}
58+
59+
export async function setStorageValues(payload: any): Promise<void> {
60+
await browser.storage.sync.set(payload);
61+
}

src/background/main.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import { MessageType } from "src/types";
44
import { Codecov } from "src/service";
55
import {
66
registerContentScript,
7+
setStorageValues,
78
unregisterContentScriptIfExists,
89
} from "./dynamic_content_scripts";
10+
import {
11+
selfHostedCodecovApiToken,
12+
selfHostedCodecovURLStorageKey,
13+
selfHostedGitHubURLStorageKey,
14+
useSelfHostedStorageKey,
15+
} from "src/constants";
916

1017
async function main(): Promise<void> {
1118
browser.runtime.onMessage.addListener(handleMessages);
@@ -31,6 +38,8 @@ async function handleMessages(message: {
3138
return registerContentScript(message.payload);
3239
case MessageType.UNREGISTER_CONTENT_SCRIPTS:
3340
return unregisterContentScriptIfExists(message.payload);
41+
case MessageType.SET_STORAGE_VALUES:
42+
return setStorageValues(message.payload);
3443
}
3544
}
3645

src/popup/main.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { ChangeEvent, useEffect, useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { createRoot } from "react-dom/client";
33
import browser from "webextension-polyfill";
44
import clsx from "clsx";
@@ -142,11 +142,14 @@ const Popup = () => {
142142
}
143143
}
144144

145-
await browser.storage.sync.set({
146-
[useSelfHostedStorageKey]: useSelfHosted,
147-
[selfHostedCodecovURLStorageKey]: codecovUrl,
148-
[selfHostedGitHubURLStorageKey]: githubUrl,
149-
[selfHostedCodecovApiToken]: codecovApiToken,
145+
await browser.runtime.sendMessage({
146+
type: MessageType.SET_STORAGE_VALUES,
147+
payload: {
148+
[useSelfHostedStorageKey]: useSelfHosted,
149+
[selfHostedCodecovURLStorageKey]: codecovUrl,
150+
[selfHostedGitHubURLStorageKey]: githubUrl,
151+
[selfHostedCodecovApiToken]: codecovApiToken,
152+
},
150153
});
151154

152155
resetEphemeralState();

src/service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class Codecov {
2727
selfHostedGitHubURLStorageKey,
2828
selfHostedCodecovApiToken,
2929
]);
30+
3031
const useSelfHosted = result[useSelfHostedStorageKey] || false;
3132
// self hosted not selected
3233
if (!useSelfHosted) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ export enum MessageType {
4343
FETCH_COMPONENTS_LIST = "fetch_components_list",
4444
REGISTER_CONTENT_SCRIPTS = "register_content_scripts",
4545
UNREGISTER_CONTENT_SCRIPTS = "unregister_content_scripts",
46+
SET_STORAGE_VALUES = "set_storage_values",
4647
}

0 commit comments

Comments
 (0)