PlasmoHQ / plasmo

🧩 The Browser Extension Framework
https://www.plasmo.com
MIT License
10.03k stars 346 forks source link

[RFC] Inject content scripts into pages on install event #868

Open Acorn221 opened 7 months ago

Acorn221 commented 7 months ago

How do you envision this feature/change to look/work like?

This would just inject all the content scripts on the extension install event. For pages with content scripts in the "MAIN" world, the script could just be injected via the scripting api and I think for the "ISOLATED" scripts, the tab would have to be refreshed? It would also be nice to just have this as an option in the content script config, so it wouldn't cause problems with pages that potentially have some kind of data on them that would be lost by refreshing.

What is the purpose of this change/feature? Why?

On my uninstall feedback form, I keep getting "this does not work" responses for my extension, which I've just realised is likely due to my users needing to refresh the tab for the extension to do something. I've manually now added a script to refresh all tabs that match the cs inject criteria, however this could be really helpful to just have built into plasmo.

(OPTIONAL) Example implementations

export const config: PlasmoCSConfig = {
  matches: ['*://*.plasmo.com/*'],
  run_at: 'document_start',
  refresh_on_install: true,
};
export const config: PlasmoCSConfig = {
  matches: ['*://*.plasmo.com/*'],
  run_at: 'document_start',
  world: 'MAIN',
  inject_on_install: true,
};

This is how it could look, but I'm not 100% sure about what the option should be called and how it should be labelled.

(OPTIONAL) Contribution

Verify canary release

Code of Conduct

duriann commented 2 months ago

Is this feature available now?

PatrickMatthiesen commented 2 weeks ago

Maybe related or unrelated, but just for anyone else stumbling on this. If you need your extension to change something when the user performs an action in the popup, you can wake up the service worker by sending it a message with the @plasmohq/messaging package.

I needed the service worker to pick up on a change in the local storage, but it was inactive. Simply sending the service worker a dummy message wakes it up and makes it ready to look for changes in local storage or whatever you might have a listener on.

Also if you want to make the service worker inactive on purpose, then Chrome has a page for that chrome://serviceworker-internals/?devtools

source: https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle#chrome_109