Skip to content

determine background or extension page by this #220

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 13 additions & 6 deletions src/middleware/wer-middleware.raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>`;
Expand Down Expand Up @@ -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);
Expand All @@ -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"));
Expand Down Expand Up @@ -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 }) => {
Expand All @@ -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:
Expand All @@ -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 */
Expand Down