plasmo icon indicating copy to clipboard operation
plasmo copied to clipboard

[RFC] Inject content scripts into pages on install event

Open Acorn221 opened this issue 1 year ago • 3 comments

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

  • [X] I would like to contribute to this RFC via a PR

Verify canary release

  • [X] I verified that the issue exists in plasmo canary release

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct
  • [X] I checked the current issues for duplicate problems.

Acorn221 avatar Jan 18 '24 23:01 Acorn221

Is this feature available now?

duriann avatar Jun 17 '24 03:06 duriann

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

PatrickMatthiesen avatar Aug 16 '24 19:08 PatrickMatthiesen