Skip to content

Commit

Permalink
Log error in case of multiple injections (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Nov 15, 2023
1 parent b3aed0c commit 98c4493
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
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 @@ -173,6 +173,21 @@ export async function getTopLevelFrame(): Promise<TopLevelFrame> {
}

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...'
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

0 comments on commit 98c4493

Please sign in to comment.