Reinject content scripts: a necessary step after a Chrome extension upgrade or installation
P粉066224086
2023-08-24 12:03:01
<p>After installing or upgrading a Chrome extension I'm developing, the content script (specified in the manifest) is not re-injected, so a page refresh is required for the extension to work properly. Is there a way to force the script to be re-injected? </p>
<p>I believe I could inject them again by programmatically removing them from the manifest and then processing the page to be injected in a background page, but that's not a good solution. </p>
<p>I don't want to automatically refresh the user's tabs as this might result in some data loss. Safari automatically refreshes all pages when an extension is installed or upgraded. </p>
The only way to force the injection of a content script without refreshing the page is through programmatic injection.
You can use Chrome's Tag API to get all tags and inject code into them. For example, you could store a version number in local storage and every time you check if the version number in the background page is out of date (if it is), you could get all active tags and inject your code programmatically, or whatever Other solutions to ensure the extension is updated.
Use the following code to get all tags:
chrome.tabs.query
and inject your code into all pages
chrome.tabs.executeScript(tabId, {file: "content_script.js"});
There is a way to make content script-heavy extensions continue to work properly after upgrading and have them take effect immediately after installation.
Installation/Upgrade
The way to install it is to simply loop through all tabs in all windows and programmatically inject some script in the tab with matching URL.
ManifestV3
manifest.json:
These host_permissions should be the same as the
matches
of the content script.background.js:
This is a simplified example that does not deal with frames. You can use the getAllFrames API and match URLs yourself, see the matching patterns documentation.
ManifestV2
Obviously you have to do this in a background page or event page script declared in manifest.json:
background.js:
Historical Fun Facts
In ancient Chrome 26 and earlier, content scripts could restore connections to background scripts. This was fixed in 2013http://crbug.com/168263. You can see an example of this technique in an earlier version of this answer.