1 manifest.json
{ "name": "Hello Extensions", "description": "Base Level Extension", "version": "1.0", "manifest_version": 2, "browser_action": { // "default_popup": "hello.html", "default_icon": "hello_extensions.png" }, "commands": { "_execute_browser_action": { "suggested_key": { "default": "Ctrl+Shift+F", "mac": "MacCtrl+Shift+F" }, "description": "Opens hello.html" } }, "permissions": [ "tabs", "unlimitedStorage", "notifications", "history", "activeTab", "storage", "webRequest", "webRequestBlocking", "http://*/*", "https://*/*" ], "background": { "scripts": ["background.js"], "persistent": true } }
2 background.js
增加jquery注入
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.executeScript(null, { file: "jquery-3.1.0.min.js" }, function () {
chrome.tabs.executeScript(null, { file: "wolbo.js" });
});
});
3 wolbo.js
console.log("hehe");
alert("hehe");
成功注入了
注意事项:
1 要么选中popup 要么选择运行脚本 只能二选一
2 Cannot access contents of url “data:text/html,chromewebdata” 是缺少权限增加权限就行了
3 webrequest权限需要开启常驻 "persistent": true
参考:
1 https://stackoverflow.com/questions/7168362/run-script-each-time-chrome-extension-icon-clicked
2 https://stackoverflow.com/questions/13326105/using-webrequest-api-with-event-page