Skip to content

Commit 10b334a

Browse files
Merge pull request #87 from codecov/spalmurray/safari-storage-api
ref: handle safari error throwing
2 parents 755114d + 3030313 commit 10b334a

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/background/dynamic_content_scripts.ts

Lines changed: 20 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,25 @@ 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: any) {
45+
// Safari throws if the script id doesn't exist, so handle that gracefully
46+
if (
47+
error instanceof Error &&
48+
error.message.match(
49+
/Invalid call to scripting.getRegisteredContentScripts\(\)\. No script with ID '.*'/
50+
)
51+
) {
52+
return true;
53+
} else {
54+
throw error;
55+
}
56+
}
57+
4158
if (registrations.length === 0) {
4259
return true;
4360
}

src/popup/main.tsx

Lines changed: 1 addition & 1 deletion
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";

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) {

0 commit comments

Comments
 (0)