mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.13k stars 22.45k forks source link

Explain choice of persistent or non-persistent content script #19711

Open rebloor opened 2 years ago

rebloor commented 2 years ago

MDN URL

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

Support for persistAcrossSessions in scripting.RegisteredContentScript was added and documented in Support for persistAcrossSessions #19196. However, this change didn't explain why developer might choose between persistent and non-persistent.

Notes for consideration:

Persistent is the default because the idea behind this API is that it's the dynamic version of the content_scripts key in manifest.json. Both registrations result in script executions at predictable* times, with the difference being that the new API allows an extension to dynamically (un)register scripts.

There are extensions that would like to have the script execution capabilities of registered content scripts, but not necessarily the persistent part of it. For example, if an extension (on behalf of a user) temporarily activated a content script with the goal of running it for content in the current session only. While it's not possible to restrict the registered content script to a specific tab, it is the closest that an extension can have if their goal is to schedule a content script for execution at the next load.

Although the specific topic is not clearly covered, this API design doc from Chrome may be helpful: https://docs.google.com/document/d/1nRJ6iW-W1MVSpJnQzNrRQFLMsr0RycwsNym06TD5i18/edit

What did you expect to see?

Description of when to choose non-persistent over persistent.

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

Hellx2 commented 2 years ago

You should choose non-persistent for things that won't save over sessions, for example, counters. With a counter for a user on a website, if you want it to start at zero then it should just have a value of zero and not be persistent.