I'm writing a Chrome extension and trying to overlay When I access the Do I have to use messaging between background.html and popup.html to access the DOM of the web page? I'd like to do it all in popup.html and also use jQuery if possible. document.body.insertBefore
method from popup.html, it overwrites the
Some examples of using programmatic injection to add an expanding popup script to this div.
List V3
Don't forget to add permissions in manifest.json, see other answers for more information.
Simple call:
Note: In Chrome 91 or earlier,
func:
should befunction:
.Call with parameters and receive results
Requires Chrome 92 as it implements
args
.Example 1:
Example 2:
List V2
Simple call:
Call with parameters and receive the result:
This example uses the
inContent
function to automatically convert the code to a string, the benefit of this is that the IDE can apply syntax highlighting and linting. The obvious disadvantage is that the browser wastes time parsing the code, but it's usually less than 1 millisecond so it's negligible.Problem: Extension pages (pop-ups, options, background pages in MV2, etc.) are separated from the web page, they have their own DOM,
document
, window andchrome-extension://
URLs.Solution: Use content scripts to access web pages or interact with their content.
Method 1. Declarative
manifest.json:
It will run once when the page loads. After that, use messaging.
warn! It cannot send DOM elements, Maps, Sets, ArrayBuffers, classes, functions, etc. It can only send JSON-compatible simple objects and types, so you need to manually extract the required data and pass it as a simple array or object.
Method 2. Programming
ManifestV3:
In extension scripts (such as pop-up windows), content scripts/functions can be injected into tabs as needed.
The result of this method is the last expression in the content script and therefore can be used to extract data. Data must be JSON compatible, see warning above.
RequiredPermissions
in
manifest.json:"script"
- mandatory;"activeTab"
- Ideal scenario, suitable for responding to user actions (usually clicking an extension icon in the toolbar). Do not show any permission warnings when installing extensions.If the ideal situation is not possible, add allowed sites to
host_permissions
in manifest.json:"*://*.example.com/"
and any other website you want.""
or"*://*/"
These will place your extension in the very slow review queue in the Chrome Web Store Due to extensive host permissions.Differences between ManifestV2 and the above:
Permissions
.