chrome外掛開發(二) 入門篇(content script
阿新 • • 發佈:2018-11-09
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
1: content scripts是page action將注入到特定頁面中的指令碼
chrome外掛中的 content script 是執行在一個被稱為isolated world
看一下程式碼 content_scripts中引用的js將會在匹配matches的頁面中被執行
run_at有document_start | idel | end 三個值可選
{ "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
每開啟一個tab頁,訪問matches指定的url 在document載入完成後 ,會彈出alert
alert('hello')
注意:content script有限制:即:js檔案中只能使用被其他頁面或者content scripts定義的變數或方法
Use variables or functions defined by web pages or by other content scripts
另外:content_script也可以採用如下方式新增
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'}); });