diff --git a/src/middleware/wer-middleware.raw.ts b/src/middleware/wer-middleware.raw.ts index 2f6089ab..8d739dce 100644 --- a/src/middleware/wer-middleware.raw.ts +++ b/src/middleware/wer-middleware.raw.ts @@ -5,9 +5,13 @@ /* This will be converted into a lodash templ., any */ /* external argument must be provided using it */ /* -------------------------------------------------- */ -(function(window) { +(function(_window) { + let window: any + if (_window.document !== undefined) { + window = _window + } - const injectionContext = this || window || {browser: null}; + const injectionContext = this || {browser: null}; (function() { `<%= polyfillSource %>`; @@ -39,13 +43,14 @@ // ========================== Called only on content scripts ============================== // function contentScriptWorker() { + // console.log('contentScriptWorker') runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg)); runtime.onMessage.addListener(({ type, payload }: { type: string; payload: any }) => { switch (type) { case SIGN_RELOAD: logger("Detected Changes. Reloading..."); - reloadPage && window.location.reload(); + reloadPage && window?.location.reload(); break; case SIGN_LOG: console.info(payload); @@ -58,6 +63,7 @@ // ======================== Called only on background scripts ============================= // function backgroundWorker(socket: WebSocket) { + // console.log('backgroundWorker') runtime.onMessage.addListener((action: { type: string; payload: any }, sender) => { if (action.type === SIGN_CONNECT) { return Promise.resolve(formatter("Connected to Web Extension Hot Reloader")); @@ -114,6 +120,7 @@ // ======================== Called only on extension pages that are not the background ============================= // function extensionPageWorker() { + // console.log('extensionPageWorker') runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg)); runtime.onMessage.addListener(({ type, payload }: { type: string; payload: any }) => { @@ -122,7 +129,7 @@ logger("Detected Changes. Reloading..."); // Always reload extension pages in the foreground when they change. // This option doesn't make sense otherwise - window.location.reload(); + window?.location.reload(); break; case SIGN_LOG: @@ -137,9 +144,9 @@ // ======================= Bootstraps the middleware =========================== // runtime.reload - ? extension.getBackgroundPage() === window ? backgroundWorker(new WebSocket(wsHost)) : extensionPageWorker() + ? !window ? backgroundWorker(new WebSocket(wsHost)) : extensionPageWorker() : contentScriptWorker(); -})(window); +})(this); /* ----------------------------------------------- */ /* End of Webpack Hot Extension Middleware */