Skip to content

Commit

Permalink
Fix messages to content being sent to all frames instead of the inten…
Browse files Browse the repository at this point in the history
…ded frame (#351)

Also rename isMainframe to isMainFrame
  • Loading branch information
david-tejada authored Oct 16, 2024
1 parent 81094a1 commit 9aa6339
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 33 deletions.
9 changes: 6 additions & 3 deletions src/background/messaging/sendRequestToContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
21 changes: 6 additions & 15 deletions src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/content/hints/hintsCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isMainframe } from "../setup/contentScriptContext";
import { isMainFrame } from "../setup/contentScriptContext";
import { reclaimHints } from "../wrappers/wrappers";
import { clearHintsInFrame } from "./hintsInFrame";
import {
Expand Down Expand Up @@ -88,7 +88,7 @@ export async function clearHintsCache() {

clearHintsInFrame();

if (isMainframe()) await initStack();
if (isMainFrame()) await initStack();
}

// For debugging purposes
Expand Down
4 changes: 2 additions & 2 deletions src/content/notify/notify.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -37,7 +37,7 @@ async function shouldNotify() {
!notificationAllowed ||
document.visibilityState !== "visible" ||
!getSetting("enableNotifications") ||
!isMainframe() ||
!isMainFrame() ||
!(await isCurrentTab())
) {
return false;
Expand Down
10 changes: 1 addition & 9 deletions src/content/setup/contentScriptContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function isCurrentTab(): Promise<boolean> {
});
}

export function isMainframe() {
export function isMainFrame() {
if (frameId === undefined) {
throw new Error(
"Unable to retrieve frameId. Context script context is not loaded"
Expand All @@ -36,11 +36,3 @@ export function isMainframe() {

return frameId === 0;
}

export async function getFrameId() {
if (!frameId) {
await loadContentScriptContext();
}

return frameId;
}
4 changes: 2 additions & 2 deletions src/content/utils/decorateTitle.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9aa6339

Please sign in to comment.