From 59fb9fa57cddc65914d3d3173587a49ee37daad0 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 6 Jun 2022 14:42:50 +0800 Subject: [PATCH 1/7] Cleanup `webext-polyfill-kinda` usage --- src/contrib/google/auth.ts | 5 ++--- src/hooks/useDeployments.ts | 5 ++--- src/options/pages/settings/AdvancedSettings.tsx | 3 +-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/contrib/google/auth.ts b/src/contrib/google/auth.ts index 62002b6ea4..54babe885c 100644 --- a/src/contrib/google/auth.ts +++ b/src/contrib/google/auth.ts @@ -15,7 +15,6 @@ * along with this program. If not, see . */ -import chromeP from "webext-polyfill-kinda"; import { getErrorMessage } from "@/errors/errorHelpers"; import { forbidContext } from "@/utils/expectContext"; @@ -33,7 +32,7 @@ export async function ensureAuth( } try { - const token = await chromeP.identity.getAuthToken({ + const token = await browser.identity.getAuthToken({ interactive, scopes, }); @@ -80,7 +79,7 @@ export async function handleRejection( } if ([403, 401].includes(status)) { - await chromeP.identity.removeCachedAuthToken({ token }); + await browser.identity.removeCachedAuthToken({ token }); console.debug( "Bad Google OAuth token. Removed the auth token from the cache so the user can re-authenticate" ); diff --git a/src/hooks/useDeployments.ts b/src/hooks/useDeployments.ts index 95574ba4d9..fc78753313 100644 --- a/src/hooks/useDeployments.ts +++ b/src/hooks/useDeployments.ts @@ -32,7 +32,6 @@ import { mergePermissions } from "@/utils/permissions"; import { Permissions } from "webextension-polyfill"; import { IExtension, RegistryId, UUID } from "@/core"; import { maybeGetLinkedApiClient } from "@/services/apiClient"; -import chromeP from "webext-polyfill-kinda"; import extensionsSlice from "@/store/extensionsSlice"; import useFlags from "@/hooks/useFlags"; import { @@ -190,7 +189,7 @@ function useDeployments(): DeploymentState { } if (checkExtensionUpdateRequired(deployments)) { - await chromeP.runtime.requestUpdateCheck(); + await browser.runtime.requestUpdateCheck(); notify.warning( "You must update the PixieBrix browser extension to activate the deployment" ); @@ -226,7 +225,7 @@ function useDeployments(): DeploymentState { }, [deployments, dispatch, installedExtensions]); const updateExtension = useCallback(async () => { - await chromeP.runtime.requestUpdateCheck(); + await browser.runtime.requestUpdateCheck(); browser.runtime.reload(); }, []); diff --git a/src/options/pages/settings/AdvancedSettings.tsx b/src/options/pages/settings/AdvancedSettings.tsx index 48a03140de..d78721d033 100644 --- a/src/options/pages/settings/AdvancedSettings.tsx +++ b/src/options/pages/settings/AdvancedSettings.tsx @@ -21,7 +21,6 @@ import { Button, Card, Form } from "react-bootstrap"; import { DEFAULT_SERVICE_URL, useConfiguredHost } from "@/services/baseService"; import React, { useCallback } from "react"; import { clearExtensionAuth } from "@/auth/token"; -import chromeP from "webext-polyfill-kinda"; import notify from "@/utils/notify"; import useFlags from "@/hooks/useFlags"; import settingsSlice from "@/store/settingsSlice"; @@ -56,7 +55,7 @@ const AdvancedSettings: React.FunctionComponent = () => { }, []); const requestExtensionUpdate = useCallback(async () => { - const status = await chromeP.runtime.requestUpdateCheck(); + const status = await browser.runtime.requestUpdateCheck(); if (status === "update_available") { browser.runtime.reload(); } else if (status === "throttled") { From 639eddd9cbe2383bb2563200314b6aa4287ca6d6 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 6 Jun 2022 14:43:05 +0800 Subject: [PATCH 2/7] Avoid mistakenly setting a TypeScript global --- src/tinyPages/devtools.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tinyPages/devtools.ts b/src/tinyPages/devtools.ts index 7535d0cb88..89c35040fc 100644 --- a/src/tinyPages/devtools.ts +++ b/src/tinyPages/devtools.ts @@ -15,11 +15,10 @@ * along with this program. If not, see . */ -const { tabId } = chrome.devtools.inspectedWindow; -if (typeof tabId === "number") { +if (typeof chrome.devtools.inspectedWindow.tabId === "number") { chrome.devtools.panels.create( "PixieBrix", "", - `pageEditor.html?tabId=${tabId}` + `pageEditor.html?tabId=${chrome.devtools.inspectedWindow.tabId}` ); } From c7c8169717d1129f31211cf630a649a7f8642b30 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 6 Jun 2022 14:43:37 +0800 Subject: [PATCH 3/7] Make `$` and `browser` available in the console --- src/extensionContext.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/extensionContext.ts b/src/extensionContext.ts index f8606698bc..510c6f1924 100644 --- a/src/extensionContext.ts +++ b/src/extensionContext.ts @@ -25,3 +25,11 @@ enrichAxiosErrors(); // https://webpack.js.org/guides/public-path/#on-the-fly __webpack_public_path__ = chrome.runtime.getURL("/"); + +// @ts-expect-error For debugging only +globalThis.$ = $; + +if (!("browser" in globalThis)) { + // @ts-expect-error For debugging only + globalThis.browser = browser; +} From 655727f1c2a56faa88e3594da71f2a8057baed58 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 6 Jun 2022 14:44:02 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Remove=20GAPI=E2=80=99s=20PermissionsError?= =?UTF-8?q?=20outdate=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contrib/google/auth.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/contrib/google/auth.ts b/src/contrib/google/auth.ts index 54babe885c..b8a600504f 100644 --- a/src/contrib/google/auth.ts +++ b/src/contrib/google/auth.ts @@ -53,10 +53,9 @@ class PermissionsError extends Error { public readonly status: number; - constructor(m: string, status: number) { - super(m); + constructor(message: string, status: number) { + super(message); this.status = status; - Object.setPrototypeOf(this, Error.prototype); } } From f1fa4fd2ca3d7bd7d0ec1da27fe821d64232b47b Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 31 May 2022 17:44:55 +0800 Subject: [PATCH 5/7] Move liftBackground mock to app --- .../@/background/externalProtocol.ts | 71 ------------------- 1 file changed, 71 deletions(-) delete mode 100644 src/__mocks__/@/background/externalProtocol.ts diff --git a/src/__mocks__/@/background/externalProtocol.ts b/src/__mocks__/@/background/externalProtocol.ts deleted file mode 100644 index 56085e5b20..0000000000 --- a/src/__mocks__/@/background/externalProtocol.ts +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2022 PixieBrix, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -jest.mock("webext-detect-page"); - -import { - HandlerOptions, - isErrorResponse, - SerializableResponse, - toErrorResponse, -} from "@/messaging/protocol"; - -import * as detect from "webext-detect-page"; - -import { deserializeError } from "serialize-error"; - -const MESSAGE_PREFIX = "@@pixiebrix/background-mock/"; - -export function liftBackground< - Arguments extends unknown[], - R extends SerializableResponse ->( - type: string, - method: (...args: Arguments) => Promise, - { asyncResponse = true }: HandlerOptions = {} -): (...args: Arguments) => Promise { - const fullType = `${MESSAGE_PREFIX}${type}`; - - return async (...args: Arguments) => { - console.debug(`running fake ${fullType}`, { fullType, args }); - - if (!asyncResponse) { - throw new Error("background notifications not implemented"); - } - - (detect.isBackground as any).mockReturnValue(true); - - try { - let handlerResult: unknown; - - try { - handlerResult = await method(...args); - } catch (error) { - console.log("Error running method", error); - handlerResult = toErrorResponse(fullType, error); - } - - if (isErrorResponse(handlerResult)) { - throw deserializeError(handlerResult.$$error); - } - - return handlerResult as R; - } finally { - (detect.isBackground as any).mockReturnValue(false); - } - }; -} From 65ff1f5fec6df74a1e457b14be0eed86380474c7 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Tue, 31 May 2022 17:49:26 +0800 Subject: [PATCH 6/7] Remove self from dependabot reviewers --- .eslintrc.js | 2 +- .github/dependabot.yml | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 6712879eef..2c4dbf529a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -79,7 +79,7 @@ module.exports = { node: true, jest: true, }, - // Overridden rules: https://github.com/fregante/eslint-config-pixiebrix/blob/main/server.js + // Overridden rules: https://github.com/pixiebrix/eslint-config-pixiebrix/blob/main/server.js extends: ["pixiebrix/server"], }, ], diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 833dc82549..4a954b1779 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -13,5 +13,3 @@ updates: time: "08:42" timezone: "Etc/UTC" open-pull-requests-limit: 20 - reviewers: - - "fregante" From d3a9313cc4c1d7eb6beeeb7bdbab4cfb96d1e554 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Wed, 8 Jun 2022 14:19:08 +0700 Subject: [PATCH 7/7] Fix reload sequence --- src/pageEditor/sidebar/Sidebar.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pageEditor/sidebar/Sidebar.tsx b/src/pageEditor/sidebar/Sidebar.tsx index d255ad1185..bd596a1cd9 100644 --- a/src/pageEditor/sidebar/Sidebar.tsx +++ b/src/pageEditor/sidebar/Sidebar.tsx @@ -79,9 +79,10 @@ const ReloadButton: React.VoidFunctionComponent = () => ( className="mt-auto" onClick={async (event) => { if (event.shiftKey) { - browser.runtime?.reload(); // Not guaranteed await browser.tabs.reload(browser.devtools.inspectedWindow.tabId); + browser.runtime?.reload(); // Not guaranteed + // We must wait before reloading or else the loading fails // https://github.com/pixiebrix/pixiebrix-extension/pull/2381 await sleep(2000);