Skip to content

Commit a78683f

Browse files
Merge pull request #1580 from input-output-hk/feat/detect-background-script-on-firefox
feat(web-extension): detect bg script when on firefox - LW-11944
2 parents f89fba9 + b4b540d commit a78683f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

packages/web-extension/src/messaging/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BackgroundMessenger, createBackgroundMessenger, generalizeBackgroundMessenger } from './BackgroundMessenger';
22
import { ChannelName, ConsumeRemoteApiOptions, ExposeApiProps, MessengerDependencies } from './types';
3-
import { FinalizationRegistryDestructor } from './util';
3+
import { FinalizationRegistryDestructor, isBackgroundProcess } from './util';
44
import { consumeMessengerRemoteApi, exposeMessengerApi } from './remoteApi';
55
import { createNonBackgroundMessenger } from './NonBackgroundMessenger';
66

@@ -15,7 +15,7 @@ export * from './errors';
1515

1616
export type BaseChannel = { baseChannel: ChannelName };
1717

18-
const isInBackgroundProcess = typeof window === 'undefined';
18+
const isInBackgroundProcess = isBackgroundProcess();
1919

2020
const getBackgroundMessenger = (() => {
2121
let backgroundMessenger: BackgroundMessenger | null = null;

packages/web-extension/src/messaging/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ export class FinalizationRegistryDestructor implements Destructor {
7777
this.#registry.register(obj, objectId);
7878
}
7979
}
80+
81+
// Firefox addons use a generated background page to run the background script, so they have a window object
82+
export const isBackgroundProcess = () =>
83+
typeof window === 'undefined' || window.location.href.includes('_generated_background');

0 commit comments

Comments
 (0)