Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Log error in case of multiple injections #176

Merged
merged 6 commits into from
Nov 15, 2023
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webext-messenger",
"version": "0.24.0",
"version": "0.25.0-0",
"description": "Browser Extension component messaging framework",
"keywords": [],
"repository": "pixiebrix/webext-messenger",
Expand Down
2 changes: 2 additions & 0 deletions source/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import type { Browser } from "webextension-polyfill";

declare global {
const browser: Browser;
// eslint-disable-next-line no-var -- `let/const` behave differently https://stackoverflow.com/a/69208755/288906
var __webextMessenger: string;
}
2 changes: 2 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export { toggleLogging } from "./logging.js";

import { initPrivateApi } from "./thisTarget.js";

// Required side effect to better track errors:
// https://github.com/pixiebrix/webext-messenger/pull/80
initPrivateApi();
15 changes: 15 additions & 0 deletions source/thisTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
return true;
}

// TODO: Test this in Jest, outside the browser

Check warning on line 89 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Test this in Jest, outside the...'

Check warning on line 89 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Test this in Jest, outside the...'

Check warning on line 89 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Test this in Jest, outside the...'
export function getActionForMessage(
from: Sender,
message: Message
Expand Down Expand Up @@ -173,6 +173,21 @@
}

export function initPrivateApi(): void {
// Improve DX by informing the developer that it's being loaded the wrong way
// https://github.com/pixiebrix/webext-messenger/issues/88
if (globalThis.__webextMessenger) {
// TODO: Use Error#cause after https://bugs.chromium.org/p/chromium/issues/detail?id=1211260

Check warning on line 179 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Use Error#cause after...'

Check warning on line 179 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Use Error#cause after...'

Check warning on line 179 in source/thisTarget.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: Use Error#cause after...'
console.log(
globalThis.__webextMessenger.replace(/^Error/, "webext-messenger")
);
console.error(
"webext-messenger: Duplicate execution. This is a fatal error.\nhttps://github.com/pixiebrix/webext-messenger/issues/88"
);
return;
}

// Use Error to capture the stack and make it easier to find the cause
globalThis.__webextMessenger = new Error("First execution").stack!;
if (isExtensionContext()) {
// Only `runtime` pages can handle this message but I can't remove it because its listener
// also serves the purpose of throwing a specific error when no methods have been registered.
Expand Down
Loading