Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

browser polyfill cleanup, debugging improvements #3615

Merged
merged 7 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
Expand Down
2 changes: 0 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ updates:
time: "08:42"
timezone: "Etc/UTC"
open-pull-requests-limit: 20
reviewers:
- "fregante"
71 changes: 0 additions & 71 deletions src/__mocks__/@/background/externalProtocol.ts

This file was deleted.

10 changes: 4 additions & 6 deletions src/contrib/google/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import chromeP from "webext-polyfill-kinda";
import { getErrorMessage } from "@/errors/errorHelpers";
import { forbidContext } from "@/utils/expectContext";

Expand All @@ -33,7 +32,7 @@ export async function ensureAuth(
}

try {
const token = await chromeP.identity.getAuthToken({
const token = await browser.identity.getAuthToken({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chromeP was only ever used because browser lacked the types. I fixed this a while ago:

interactive,
scopes,
});
Expand All @@ -54,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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remnant of pre-class X extends Error times

}
}

Expand All @@ -80,7 +78,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"
);
Expand Down
8 changes: 8 additions & 0 deletions src/extensionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lets us finally use browser.* APIs in the console.

pageScript also has a similar jQuery global, so I thought I'd expose that too.

5 changes: 2 additions & 3 deletions src/hooks/useDeployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
);
Expand Down Expand Up @@ -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();
}, []);

Expand Down
3 changes: 1 addition & 2 deletions src/options/pages/settings/AdvancedSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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") {
Expand Down
3 changes: 2 additions & 1 deletion src/pageEditor/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we request the reload, browser.tabs becomes unavailable, so the rest fails.


// We must wait before reloading or else the loading fails
// https://github.com/pixiebrix/pixiebrix-extension/pull/2381
await sleep(2000);
Expand Down
5 changes: 2 additions & 3 deletions src/tinyPages/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

const { tabId } = chrome.devtools.inspectedWindow;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was inexplicably providing a tabId global type available in every file. 😰

Mysteries of TypeScript.

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}`
);
}