diff --git a/apps/browser/src/platform/polyfills/zone-patch-chrome-runtime.ts b/apps/browser/src/platform/polyfills/zone-patch-chrome-runtime.ts index fa731840f8..055518fc75 100644 --- a/apps/browser/src/platform/polyfills/zone-patch-chrome-runtime.ts +++ b/apps/browser/src/platform/polyfills/zone-patch-chrome-runtime.ts @@ -2,35 +2,43 @@ * Monkey patch `chrome.runtime.onMessage` event listeners to run in the Angular zone. */ Zone.__load_patch("ChromeRuntimeOnMessage", (global: any, Zone: ZoneType, api: _ZonePrivate) => { - const onMessage = global.chrome.runtime.onMessage; if (typeof global?.chrome?.runtime?.onMessage === "undefined") { return; } + const onMessage = global.chrome.runtime.onMessage; // eslint-disable-next-line @typescript-eslint/ban-types - api.patchMethod(onMessage, "addListener", (delegate: Function) => (self: any, args: any[]) => { - const callback = args.length > 0 ? args[0] : null; - if (typeof callback === "function") { - const wrapperedCallback = Zone.current.wrap(callback, "ChromeRuntimeOnMessage"); - callback[api.symbol("chromeRuntimeOnMessageCallback")] = wrapperedCallback; - return delegate.call(self, wrapperedCallback); - } else { - return delegate.apply(self, args); - } + const nativeAddListener = onMessage.addListener as Function; + api.ObjectDefineProperty(chrome.runtime.onMessage, "addListener", { + value: function (...args: any[]) { + const callback = args.length > 0 ? args[0] : null; + if (typeof callback === "function") { + const wrapperedCallback = Zone.current.wrap(callback, "ChromeRuntimeOnMessage"); + callback[api.symbol("chromeRuntimeOnMessageCallback")] = wrapperedCallback; + return nativeAddListener.call(onMessage, wrapperedCallback); + } else { + return nativeAddListener.apply(onMessage, args); + } + }, + writable: false, }); // eslint-disable-next-line @typescript-eslint/ban-types - api.patchMethod(onMessage, "removeListener", (delegate: Function) => (self: any, args: any[]) => { - const callback = args.length > 0 ? args[0] : null; - if (typeof callback === "function") { - const wrapperedCallback = callback[api.symbol("chromeRuntimeOnMessageCallback")]; - if (wrapperedCallback) { - return delegate.call(self, wrapperedCallback); + const nativeRemoveListener = onMessage.removeListener as Function; + api.ObjectDefineProperty(chrome.runtime.onMessage, "removeListener", { + value: function (...args: any[]) { + const callback = args.length > 0 ? args[0] : null; + if (typeof callback === "function") { + const wrapperedCallback = callback[api.symbol("chromeRuntimeOnMessageCallback")]; + if (wrapperedCallback) { + return nativeRemoveListener.call(onMessage, wrapperedCallback); + } else { + return nativeRemoveListener.apply(onMessage, args); + } } else { - return delegate.apply(self, args); + return nativeRemoveListener.apply(onMessage, args); } - } else { - return delegate.apply(self, args); - } + }, + writable: false, }); });