Chrome Extender Gets the current page URL and HTML content

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.