Skip to content

Commit

Permalink
Stop using isDevToolsPage
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jan 21, 2024
1 parent c2a7420 commit 9584538
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/background/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ async function safelyRunBrick(
}

// This must follow the tab existence checks or else it returns false even if the tab simply doesn't exist
if (!(await canAccessTab(tabId))) {
throw new BusinessError("PixieBrix doesn't have access to the tab");
if (!(await canAccessTab({ tabId, frameId }))) {
throw new BusinessError("PixieBrix doesn't have access to the page");
}

throw error;
Expand Down
12 changes: 3 additions & 9 deletions src/contrib/google/sheets/core/useCurrentOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { isDevToolsPage, isOptionsPage } from "webext-detect-page";
import { isOptionsPage } from "webext-detect-page";
import { useEffect } from "react";
import notify from "@/utils/notify";
import useAsyncState from "@/hooks/useAsyncState";
import { isPageEditor } from "@/utils/expectContext";

/**
* Get the current origin for where code is running. Used to set the origin on the
Expand All @@ -40,14 +41,7 @@ function useCurrentOrigin(): string | null {
return browser.runtime.getURL("");
}

if (
// Checks location.pathname against the 'devtools_page' value in manifest.json
// This won't match for dev tools panels created by the devtool
// page (i.e. the Page Editor)
isDevToolsPage() ||
// Check for the page editor pagePath in the location pathname
location.pathname === "/pageEditor.html"
) {
if (isPageEditor()) {
return "devtools://devtools";
}

Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/hooks/useCurrentUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const startWatching = once(async () => {
});

export default function useCurrentUrl(): string {
expectContext("devTools");
expectContext("pageEditor");

const [url, setUrl] = useState(tabUrl);

Expand Down
2 changes: 1 addition & 1 deletion src/pageEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { CustomFormRenderer } from "@/bricks/renderers/customForm";
import MapValues from "@/bricks/transformers/controlFlow/MapValues";

export async function getCurrentURL(): Promise<string> {
expectContext("devTools");
expectContext("pageEditor");

const tab = await browser.tabs.get(chrome.devtools.inspectedWindow.tabId);
return tab.url;
Expand Down
6 changes: 3 additions & 3 deletions src/telemetry/BackgroundLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import { type Logger, type MessageContext } from "@/types/loggerTypes";
import { type JsonObject } from "type-fest";
import { isBackground, isDevToolsPage } from "webext-detect-page";
import { isBackground } from "webext-detect-page";
import {
notifyContextInvalidated,
wasContextInvalidated,
} from "@/errors/contextInvalidated";
import { recordLog } from "@/background/messenger/api";
import { expectContext } from "@/utils/expectContext";
import { expectContext, isPageEditor } from "@/utils/expectContext";
import reportError from "@/telemetry/reportError";

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ class BackgroundLogger implements Logger {
}

async error(error: unknown, data: JsonObject): Promise<void> {
if (wasContextInvalidated() && !isBackground() && !isDevToolsPage()) {
if (wasContextInvalidated() && !isBackground() && !isPageEditor()) {
void notifyContextInvalidated();
}

Expand Down
4 changes: 0 additions & 4 deletions src/testUtils/detectPageMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export function isOptionsPage() {
return _context === "options";
}

export function isDevToolsPage() {
return _context === "devToolsPage";
}

export function isBackground() {
return _context === "background";
}
Expand Down
8 changes: 6 additions & 2 deletions src/utils/expectContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function isBrowserSidebar(): boolean {
return isExtensionContext() && location.pathname === "/sidebar.html";
}

export function isPageEditor(): boolean {
return location.pathname === "/pageEditor.html";
}

/**
* Accepts 'This is my error' | new Error('This is my error') | Error;
* The constructor would be used to create a custom error with the default message
Expand All @@ -49,16 +53,16 @@ function createError(
}

// eslint-disable-next-line local-rules/persistBackgroundData -- Static
const contexts = [...contextNames, "sidebar"] as const;
const contexts = [...contextNames, "sidebar", "pageEditor"] as const;

// eslint-disable-next-line local-rules/persistBackgroundData -- Functions
const contextMap = new Map<(typeof contexts)[number], () => boolean>([
["web", isWebPage],
["extension", isExtensionContext],
["background", isBackground],
["pageEditor", isPageEditor],
["contentScript", isContentScript],
["sidebar", isBrowserSidebar],
["devTools", () => "devtools" in chrome],
]);

/**
Expand Down

0 comments on commit 9584538

Please sign in to comment.