Skip to content

Commit

Permalink
Merge pull request #87 from codecov/spalmurray/safari-storage-api
Browse files Browse the repository at this point in the history
ref: handle safari error throwing
  • Loading branch information
spalmurray-codecov authored Oct 28, 2024
2 parents 755114d + 3030313 commit 10b334a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
23 changes: 20 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,25 @@ 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: 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) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion 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
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

0 comments on commit 10b334a

Please sign in to comment.