Code Monkey home page Code Monkey logo

Comments (2)

nicooprat avatar nicooprat commented on June 1, 2024

Really interested to know why, too! What's more, background scripts make extensions pretty laggy in some cases: https://stackoverflow.com/questions/56500742/why-is-my-google-chrome-extensions-popup-ui-laggy-on-external-monitors-but-not

For reference, here's the section about this in this repo wiki: https://github.com/rubenspgcavalcante/webpack-extension-reloader/wiki/General-Information#wcer-e2

from webpack-extension-reloader.

vjpr avatar vjpr commented on June 1, 2024

I bumped into same problem and was not actually notified that the background page was missing from manifest...because I didn't provide the optional manifest option.

@nicooprat Really interested to know why

The code that is running in the extension pages is actually really simple.

wer uses the following template that is injected into all files matching the entries option.

// ========================== Called only on content scripts ============================== //
function contentScriptWorker() {
runtime.sendMessage({ type: SIGN_CONNECT }).then(msg => console.info(msg));
runtime.onMessage.addListener(({ type, payload }: IAction) => {
switch (type) {
case SIGN_RELOAD:
logger("Detected Changes. Reloading ...");
reloadPage && window.location.reload();
break;
case SIGN_LOG:
console.info(payload);
break;
}
});
}
// ======================== Called only on background scripts ============================= //
function backgroundWorker(socket: WebSocket) {
runtime.onMessage.addListener((action: IAction, sender) => {
if (action.type === SIGN_CONNECT) {
return Promise.resolve(formatter("Connected to Extension Hot Reloader"));
}
return true;
});
socket.addEventListener("message", ({ data }: MessageEvent) => {
const { type, payload } = JSON.parse(data);
if (type === SIGN_CHANGE && (!payload || !payload.onlyPageChanged)) {
tabs.query({ status: "complete" }).then(loadedTabs => {
loadedTabs.forEach(
tab => tab.id && tabs.sendMessage(tab.id, { type: SIGN_RELOAD }),
);
socket.send(
JSON.stringify({
type: SIGN_RELOADED,
payload: formatter(
`${timeFormatter(new Date())} - ${
manifest.name
} successfully reloaded`,
),
}),
);
runtime.reload();
});
} else {
runtime.sendMessage({ type, payload });
}
});

You can see that the background script connects to WebSocket instance, and the content scripts and extension pages connect to the background script.

This makes sense as there is only one websocket connection to manage, and the background script takes care of reloading any tabs that have changed content scripts running in them.

from webpack-extension-reloader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.