diff --git a/src/background/messaging/sendRequestToContent.ts b/src/background/messaging/sendRequestToContent.ts index 6c32456..4bab684 100644 --- a/src/background/messaging/sendRequestToContent.ts +++ b/src/background/messaging/sendRequestToContent.ts @@ -193,10 +193,13 @@ export async function sendRequestToContent( ); } - frameId = (frameId ?? toAllFrames.has(request.type)) ? undefined : 0; - request.frameId = frameId; + // If frameId is provided we send it to that frame. If not we send it to the + // main frame or to all frames depending on if it is in toAllFrames. + const targetFrameId = + frameId ?? (toAllFrames.has(request.type) ? undefined : 0); + request.frameId = targetFrameId; return browser.tabs.sendMessage(targetTabId, request, { - frameId, + frameId: targetFrameId, }); } diff --git a/src/content/content.ts b/src/content/content.ts index 1768f81..cb9d260 100644 --- a/src/content/content.ts +++ b/src/content/content.ts @@ -7,34 +7,26 @@ import { markHintsAsKeyboardReachable, restoreKeyboardReachableHints, } from "./actions/keyboardClicking"; -import { updateHintsInTab } from "./utils/getHintsInTab"; import { runRangoActionWithTarget } from "./actions/runRangoActionWithTarget"; import { runRangoActionWithoutTarget } from "./actions/runRangoActionWithoutTarget"; -import { reclaimHints } from "./wrappers/wrappers"; import { reclaimHintsFromCache } from "./hints/hintsCache"; +import { deleteHintsInFrame } from "./hints/hintsInFrame"; +import { synchronizeHints } from "./hints/hintsRequests"; import { allowToastNotification, notify, notifyTogglesStatus, } from "./notify/notify"; -import { initContentScriptOrWait } from "./setup/initContentScript"; -import { setNavigationToggle } from "./settings/toggles"; import { updateHintsEnabled } from "./observe"; -import { getFrameId } from "./setup/contentScriptContext"; -import { deleteHintsInFrame } from "./hints/hintsInFrame"; -import { synchronizeHints } from "./hints/hintsRequests"; +import { setNavigationToggle } from "./settings/toggles"; +import { initContentScriptOrWait } from "./setup/initContentScript"; import { getTitleBeforeDecoration, initTitleDecoration, removeDecorations, } from "./utils/decorateTitle"; - -// Sending to specific frames from the background script is buggy in Safari, we -// need to check that the request was actually intended for this frame. -async function isWrongFrame(request: RequestFromBackground) { - const frameId = await getFrameId(); - return request.frameId !== undefined && frameId !== request.frameId; -} +import { updateHintsInTab } from "./utils/getHintsInTab"; +import { reclaimHints } from "./wrappers/wrappers"; browser.runtime.onMessage.addListener( async ( @@ -44,7 +36,6 @@ browser.runtime.onMessage.addListener( > => { const request = message as RequestFromBackground; await initContentScriptOrWait(); - if (await isWrongFrame(request)) return; if ("target" in request) { return runRangoActionWithTarget(request); diff --git a/src/content/hints/hintsCache.ts b/src/content/hints/hintsCache.ts index 94cbe0d..a00b00a 100644 --- a/src/content/hints/hintsCache.ts +++ b/src/content/hints/hintsCache.ts @@ -1,4 +1,4 @@ -import { isMainframe } from "../setup/contentScriptContext"; +import { isMainFrame } from "../setup/contentScriptContext"; import { reclaimHints } from "../wrappers/wrappers"; import { clearHintsInFrame } from "./hintsInFrame"; import { @@ -88,7 +88,7 @@ export async function clearHintsCache() { clearHintsInFrame(); - if (isMainframe()) await initStack(); + if (isMainFrame()) await initStack(); } // For debugging purposes diff --git a/src/content/notify/notify.tsx b/src/content/notify/notify.tsx index 7d19e4f..3774a20 100644 --- a/src/content/notify/notify.tsx +++ b/src/content/notify/notify.tsx @@ -1,7 +1,7 @@ import { createRoot } from "react-dom/client"; import { type ToastOptions, toast } from "react-toastify"; import { getSetting } from "../settings/settingsManager"; -import { isCurrentTab, isMainframe } from "../setup/contentScriptContext"; +import { isCurrentTab, isMainFrame } from "../setup/contentScriptContext"; import { Toast } from "./Toast"; import { ToastIcon } from "./ToastIcon"; import { ToastMessage } from "./ToastMessage"; @@ -37,7 +37,7 @@ async function shouldNotify() { !notificationAllowed || document.visibilityState !== "visible" || !getSetting("enableNotifications") || - !isMainframe() || + !isMainFrame() || !(await isCurrentTab()) ) { return false; diff --git a/src/content/setup/contentScriptContext.ts b/src/content/setup/contentScriptContext.ts index a4cba18..864d7ac 100644 --- a/src/content/setup/contentScriptContext.ts +++ b/src/content/setup/contentScriptContext.ts @@ -27,7 +27,7 @@ export async function isCurrentTab(): Promise { }); } -export function isMainframe() { +export function isMainFrame() { if (frameId === undefined) { throw new Error( "Unable to retrieve frameId. Context script context is not loaded" @@ -36,11 +36,3 @@ export function isMainframe() { return frameId === 0; } - -export async function getFrameId() { - if (!frameId) { - await loadContentScriptContext(); - } - - return frameId; -} diff --git a/src/content/utils/decorateTitle.ts b/src/content/utils/decorateTitle.ts index 4f4fb5a..4398e11 100644 --- a/src/content/utils/decorateTitle.ts +++ b/src/content/utils/decorateTitle.ts @@ -1,6 +1,6 @@ import browser from "webextension-polyfill"; import { throttle } from "lodash"; -import { isMainframe } from "../setup/contentScriptContext"; +import { isMainFrame } from "../setup/contentScriptContext"; import { getToggles } from "../settings/toggles"; import { getSetting, onSettingChange } from "../settings/settingsManager"; @@ -108,7 +108,7 @@ const throttledMutationCallback = throttle(async () => { let mutationObserver: MutationObserver | undefined; export async function initTitleDecoration() { - if (!isMainframe()) return; + if (!isMainFrame()) return; const previousUrlInTitle = urlInTitle; const previousIncludeTabMarkers = includeTabMarkers;