Let's start with the configuration in Manifest.json.
Introducing Injection Scripts
"Content_scripts": [
{
"JS": ["Content_script.js"],
Under what circumstances the script is used
"Matches": [
"Http://*/*",
"Https://*/*"
],
When to run the "Document loading start"
"Run_at": "Document_start"
}
],
{
"Permissions": [ "Tabs"]
}
1. Get the URL of the current page
can be obtained in popup.js (Chrome.tabs.getCurrent already out, return value is undefined)
Chrome.tabs.getSelected (nullfunction (tab) { console.log (tab.url); });
2. Get the HTML content of the current page
Content-script.js is the script that is injected into the tab page Popup.js is the way the PopOver script communicates with each other
SendMessage(msg, callback),
Onmeassage(callback) (SendRequest, ONrequest is out, API is no longer supported) Popup.js: Send Message
chrome.tabs.getSelected (NULL, Function (tab) {//) get the tabid of the current page first
Chrome.tabs.sendMessage (tab.id, {greeting: "Hello"}, Function (response) {
Console.log (response); Send request information to Content-script.js
});
});
Content-script.js: Response message
var html = Document.body.innerHTML; Chrome.extension.onMessage.addListener ( function(request, sender, SendMessage) {if (request.greeting = = "Hello") sendMessage (HTML) ; Else sendMessage (// snub them. });
Chrome Extender Gets the current page URL and HTML content