谷歌浏览器,只有一个按钮的插件

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

 

 

 

 

 

参考:

https://stackoverflow.com/questions/7168362/run-script-each-time-chrome-extension-icon-clicked

https://stackoverflow.com/questions/13326105/using-webrequest-api-with-event-page  

https://stackoverflow.com/questions/37093152/unchecked-runtime-lasterror-while-running-tabs-executescript-cannot-access-cont

https://www.cnblogs.com/giggle/p/8082672.html

 

posted @ 2020-06-03 20:59  wolbo  阅读(535)  评论(0编辑  收藏  举报