Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: ignore unexpected message #354

Merged
merged 2 commits into from
Feb 17, 2024
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
7 changes: 7 additions & 0 deletions src/content/client/WindowMessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import type { Key, Schema, Request } from "../../messaging/schema/window";

type WindowMessageSender = SimplexSender<Schema>;

/**
* The window.postMessage() is used to identify a frame id of the <iframe>
* element through the background script. Only Firefox supports to get the
* frame id of the <iframe> elementa and the other browsers do not support.
Copy link

Choose a reason for hiding this comment

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

elementa

*
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/getFrameId
*/
export const newSender = (target: Window) => {
const sender = new SimplexSender<Schema>((type: Key, args: Request) => {
const msg = JSON.stringify({ args, type });
Expand Down
8 changes: 2 additions & 6 deletions src/content/messaging/WindowMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ export default class WindowMessageListener {
try {
message = JSON.parse(event.data);
} catch (e) {
// eslint-disable-next-line no-console
console.warn("unexpected message format:", e);
// ignore unexpected message
return;
}

if (typeof message !== "object" && message !== null) {
// eslint-disable-next-line no-console
console.warn("unexpected message format:", message);
return;
}

Expand All @@ -61,8 +58,7 @@ export default class WindowMessageListener {
typeof type !== "string" ||
(typeof args !== "undefined" && typeof args !== "object")
) {
// eslint-disable-next-line no-console
console.warn("unexpected message format:", message);
// ignore unexpected message
return;
}

Expand Down
Loading