diff --git a/package-lock.json b/package-lock.json index 848a816fd3..0e75b1d4e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -174,6 +174,7 @@ "@testing-library/user-event": "^14.5.1", "@total-typescript/ts-reset": "^0.5.1", "@types/chrome": "^0.0.216", + "@types/dom-navigation": "^1.0.3", "@types/dompurify": "^3.0.5", "@types/downloadjs": "^1.4.6", "@types/gapi": "0.0.47", @@ -7795,6 +7796,12 @@ "integrity": "sha512-w5jZ0ee+HaPOaX25X2/2oGR/7rgAQSYII7X7pp0m9KgBfMP7uKfMfTvcpl5Dj+eDBbpxKGiqE+flqDr6XTd2RA==", "dev": true }, + "node_modules/@types/dom-navigation": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@types/dom-navigation/-/dom-navigation-1.0.3.tgz", + "integrity": "sha512-nbkhQ2o6UUBn1uLwSrA//rFkqs8XRk5d7cE6jzzTl8MAIUs/nFMtFqVESwiYY1HGZxflzbFlXsZ8NNzTaoFs1Q==", + "dev": true + }, "node_modules/@types/dompurify": { "version": "3.0.5", "resolved": "https://registry.npmjs.org/@types/dompurify/-/dompurify-3.0.5.tgz", diff --git a/package.json b/package.json index b737f6c31a..ad9b89b30b 100644 --- a/package.json +++ b/package.json @@ -196,6 +196,7 @@ "@testing-library/user-event": "^14.5.1", "@total-typescript/ts-reset": "^0.5.1", "@types/chrome": "^0.0.216", + "@types/dom-navigation": "^1.0.3", "@types/dompurify": "^3.0.5", "@types/downloadjs": "^1.4.6", "@types/gapi": "0.0.47", diff --git a/src/background/navigation.ts b/src/background/navigation.ts index a0b9ec50e3..e749c7240d 100644 --- a/src/background/navigation.ts +++ b/src/background/navigation.ts @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -import { reactivateTab, handleNavigate } from "@/contentScript/messenger/api"; +import { reactivateTab } from "@/contentScript/messenger/api"; import { forEachTab } from "@/utils/extensionUtils"; import { type Target } from "@/types/messengerTypes"; import { canAccessTab } from "@/permissions/permissionsUtils"; @@ -51,12 +51,6 @@ async function onNavigation({ tabId, frameId }: Target): Promise { if (syncFlagOn("navigation-trace")) { await traceNavigation({ tabId, frameId }); } - - if (await canAccessTab({ tabId, frameId })) { - // The content script will already be injected, so we don't have to call ensureContentScript before messaging the - // content script with handleNavigate. - handleNavigate({ tabId, frameId }); - } } // Some sites use the hash to encode page state (e.g., filters). There are some non-navigation scenarios where the hash diff --git a/src/contentScript/contentScriptCore.ts b/src/contentScript/contentScriptCore.ts index 018f95d9e5..4f44cf5ef3 100644 --- a/src/contentScript/contentScriptCore.ts +++ b/src/contentScript/contentScriptCore.ts @@ -26,7 +26,7 @@ import registerMessenger from "@/contentScript/messenger/registration"; import registerBuiltinBricks from "@/bricks/registerBuiltinBricks"; import registerContribBlocks from "@/contrib/registerContribBlocks"; import brickRegistry from "@/bricks/registry"; -import { handleNavigate } from "@/contentScript/lifecycle"; +import { handleNavigate, initNavigation } from "@/contentScript/lifecycle"; import { initTelemetry } from "@/background/messenger/api"; import { ENSURE_CONTENT_SCRIPT_READY } from "@/contentScript/ready"; import { initToaster } from "@/utils/notify"; @@ -66,6 +66,7 @@ export async function init(): Promise { initToaster(); await handleNavigate(); + initNavigation(); void initSidebarActivation(); diff --git a/src/contentScript/lifecycle.ts b/src/contentScript/lifecycle.ts index 7c40523037..1aa70cc871 100644 --- a/src/contentScript/lifecycle.ts +++ b/src/contentScript/lifecycle.ts @@ -638,3 +638,10 @@ export async function reactivateTab(): Promise { // Force navigate event even though the href hasn't changed await handleNavigate({ force: true }); } + +export function initNavigation() { + window.navigation?.addEventListener("navigate", async (event) => { + // Delay slightly to avoid catching events that navigate away from the page + setTimeout(handleNavigate, 0); + }); +} diff --git a/src/contentScript/messenger/api.ts b/src/contentScript/messenger/api.ts index 2293201f2c..28f796d741 100644 --- a/src/contentScript/messenger/api.ts +++ b/src/contentScript/messenger/api.ts @@ -70,7 +70,6 @@ export const getInstalledExtensionPoints = getMethod( "INSTALLED_EXTENSION_POINTS", ); export const checkAvailable = getMethod("CHECK_AVAILABLE"); -export const handleNavigate = getNotifier("HANDLE_NAVIGATE"); export const runBrick = getMethod("RUN_BRICK"); export const cancelSelect = getMethod("CANCEL_SELECT_ELEMENT"); export const selectElement = getMethod("SELECT_ELEMENT"); diff --git a/src/contentScript/messenger/registration.ts b/src/contentScript/messenger/registration.ts index 2678ba474d..43e9a159ed 100644 --- a/src/contentScript/messenger/registration.ts +++ b/src/contentScript/messenger/registration.ts @@ -22,7 +22,6 @@ import { handleMenuAction } from "@/contentScript/contextMenus"; import { ensureInstalled, getActiveExtensionPoints, - handleNavigate, queueReactivateTab, reactivateTab, removePersistedExtension, @@ -129,7 +128,6 @@ declare global { INSTALLED_EXTENSION_POINTS: typeof getActiveExtensionPoints; ENSURE_EXTENSION_POINTS_INSTALLED: typeof ensureInstalled; CHECK_AVAILABLE: typeof checkAvailable; - HANDLE_NAVIGATE: typeof handleNavigate; RUN_BRICK: typeof runBrick; CANCEL_SELECT_ELEMENT: typeof cancelSelect; SELECT_ELEMENT: typeof selectElement; @@ -198,7 +196,6 @@ export default function registerMessenger(): void { INSTALLED_EXTENSION_POINTS: getActiveExtensionPoints, ENSURE_EXTENSION_POINTS_INSTALLED: ensureInstalled, CHECK_AVAILABLE: checkAvailable, - HANDLE_NAVIGATE: handleNavigate, RUN_BRICK: runBrick, CANCEL_SELECT_ELEMENT: cancelSelect, diff --git a/src/pageEditor/pageEditor.tsx b/src/pageEditor/pageEditor.tsx index a6cb8ab23d..8d614b1b1d 100644 --- a/src/pageEditor/pageEditor.tsx +++ b/src/pageEditor/pageEditor.tsx @@ -23,7 +23,6 @@ import "@/extensionContext"; import "@/development/darkMode.js"; import { initMessengerLogging } from "@/development/messengerLogging"; -import registerMessenger from "@/pageEditor/messenger/registration"; import ReactDOM from "react-dom"; import React from "react"; @@ -39,7 +38,6 @@ markAppStart(); void initMessengerLogging(); void initRuntimeLogging(); void initPerformanceMonitoring(); -registerMessenger(); watchNavigation(); initToaster(); diff --git a/src/tsconfig.strictNullChecks.json b/src/tsconfig.strictNullChecks.json index 54a4b8359e..f86dcc4062 100644 --- a/src/tsconfig.strictNullChecks.json +++ b/src/tsconfig.strictNullChecks.json @@ -320,8 +320,6 @@ "./pageEditor/fields/makeLockableFieldProps.tsx", "./pageEditor/fields/selectorListItem/SelectorListItem.tsx", "./pageEditor/hooks/useEscapeHandler.ts", - "./pageEditor/messenger/api.ts", - "./pageEditor/messenger/registration.ts", "./pageEditor/panes/BetaPane.tsx", "./pageEditor/panes/RestrictedPane.tsx", "./pageEditor/panes/StaleSessionPane.tsx",