Chrome plugin Development (ii) introductory article (content script)

Source: Internet
Author: User
Tags min

1:content scripts is a script that page action will inject into a specific page

The content script in the Chrome plugin is run in a running environment called isolated world, and the script on the page does not interfere with each other, because it is not in a running environment, so it is not possible to invoke the method of script definition on the page. Of course, Google also gives a solution: http://code.google.com/chrome/extensions/content_scripts.html


Take a look at the JS referenced in code content_scripts will be executed on the page that matches matches

Run_at has Document_start | Idel | End three values selectable

{  
  	"name": "Coffee",
  	"Manifest_version": 2,
	"version": "1.0",  
	"description": "Coffee Test",  
	" Browser_action ": {  
	   " Default_icon ":" Icon.png ",
	   " Default_title ":" My Task List ",
    	" Default_popup ":" Popup.html "
	 },
	 " background ": {
	    " page ":" Background.html "
	  },
	 " content_scripts ": [{
	     " Matches ": [" http://*/* "," https://*/* "], 
	     " JS ": [" Js/jquery-1.9.1.min.js "," Js/test.js "], 
	     " run_at ":" Document_start ",
	     " All_frames ": True 
	}],
  	" Permissions ": [   
   	 " Tabs "," http://*/* "," https://*/* "  
  	]   
}

Test.js

Each tab page is opened, and access to the URL specified by matches will pop up alert when the document is loaded.

Alert (' Hello ')

Note: The content script has limitations: That is, the JS file can only use variables or methods defined by other pages or content scripts

Use variables or functions defined by Web pages or by other content scripts

In addition: Content_script can also be added in the following ways

Background.html

Chrome.browserAction.onClicked.addListener (function (tab) {
     chrome.tabs.executeScript (tab.id, {file: ') Jquery.min.js '});
     Chrome.tabs.executeScript (tab.id, {file: ' content.js ');
 });

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.