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

Add alert for failed sidebar injection in extension console #4208

Merged
merged 8 commits into from
Sep 6, 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
20 changes: 19 additions & 1 deletion src/background/browserAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ import { rehydrateSidebar } from "@/contentScript/messenger/api";
import { executeScript, isScriptableUrl } from "webext-content-scripts";
import pMemoize from "p-memoize";
import webextAlert from "./webextAlert";
import { isMac } from "@/utils";
import { notify } from "@/options/messenger/api";

const ERR_UNABLE_TO_OPEN =
"PixieBrix was unable to open the Sidebar. Try refreshing the page.";

const keyboardShortcut = isMac() ? "Cmd+Opt+C" : "Ctrl+Shift+C";
const MSG_NO_SIDEBAR_ON_OPTIONS_PAGE = `PixieBrix Tip 💜\n If you want to create a new blueprint, first navigate to the page you want to modify, then open PixieBrix in the DevTools (${keyboardShortcut}).`;

// The sidebar is always injected to into the top level frame
const TOP_LEVEL_FRAME_ID = 0;

Expand Down Expand Up @@ -74,7 +79,20 @@ async function _toggleSidebar(tabId: number, tabUrl: string): Promise<void> {
async function handleBrowserAction(tab: Tabs.Tab): Promise<void> {
// The URL might not be available in certain circumstances. This silences these
// cases and just treats them as "not allowed on this page"
await toggleSidebar(tab.id, String(tab.url));
const url = String(tab.url);
const optionsPage = browser.runtime.getURL("options.html");

if (url.startsWith(optionsPage)) {
notify.info(
{ tabId: tab.id, page: "/options.html" },
{
id: "MSG_NO_SIDEBAR_ON_OPTIONS_PAGE",
message: MSG_NO_SIDEBAR_ON_OPTIONS_PAGE,
}
);
} else {
await toggleSidebar(tab.id, url);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We need a return here because no further action is necessary

}

export default function initBrowserAction() {
Expand Down
25 changes: 25 additions & 0 deletions src/options/messenger/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

/* Do not use `registerMethod` in this file */
import { getNotifier } from "webext-messenger";

export const notify = {
info: getNotifier("NOTIFY_INFO"),
error: getNotifier("NOTIFY_ERROR"),
success: getNotifier("NOTIFY_SUCCESS"),
};
39 changes: 39 additions & 0 deletions src/options/messenger/registration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/

/* Do not use `getMethod` in this file; Keep only registrations here, not implementations */
import { registerMethods } from "webext-messenger";
import { expectContext } from "@/utils/expectContext";
import notify from "@/utils/notify";

expectContext("extension");

declare global {
interface MessengerMethods {
NOTIFY_INFO: typeof notify.info;
NOTIFY_ERROR: typeof notify.error;
NOTIFY_SUCCESS: typeof notify.success;
}
}

export default function registerMessenger(): void {
registerMethods({
NOTIFY_INFO: notify.info,
NOTIFY_ERROR: notify.error,
NOTIFY_SUCCESS: notify.success,
});
}
2 changes: 2 additions & 0 deletions src/options/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import React from "react";
import App from "@/options/App";
import initGoogle from "@/contrib/google/initGoogle";
import { initToaster } from "@/utils/notify";
import registerMessenger from "@/options/messenger/registration";

function init(): void {
render(<App />, document.querySelector("#container"));
}

registerMessenger();
initGoogle();
initToaster();
init();
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export async function logPromiseDuration<P>(

export function isMac(): boolean {
// https://stackoverflow.com/a/27862868/402560
return navigator.platform.includes("Mac");
return globalThis.navigator?.platform.includes("Mac");
}

/** Tests a target string against a list of strings (full match) or regexes (can be mixed) */
Expand Down