Chrome Extensions - How can i perform an action, when chrome.browserAction.onClicked has fired?

风流意气都作罢 提交于 2019-12-25 13:26:06

问题


I want to make a browserAction extension, with an icon and a listener on it.

I have a manifest file, and a background script, the script is the following:

chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.executeScript(null,{code:'some code here'});
});

The code works on the page, i tried it on a different way (popup and a button what fires the action). But if i try it with a browserAction onclick method, nothing happens:(

The manifest:

{
  "name": "somename",
  "version": "1.0",
  "manifest_version": 2,
  "description": "sometext",
  "browser_action": {
    "default_icon": "images/icon.png",
    "default_title": "MyStyle"
  },
  "background": {
    "scripts": ["js/code.js"]
  },
  "permissions": [
    "tabs",  
    "https://www.examplesite.ex/*",
    "http://www.examplesite.ex/*",
    "http://*.ex/*"
  ]
}

Can anybody help me?:/


回答1:


Since the original question has been solved in the comments, I'll answer the follow-up question:
"Next step to make it automatic, without any click".

This can be done easily using Content scripts. When you don't have to access global variables, the following code is sufficient. Otherwise, inject the script using the techniques as mentioned here:

js/code.js

document.title = "newtitle";

manifest.json

{
  "name": "somename",
  "version": "1.0",
  "manifest_version": 2,
  "description": "sometext",
  "content_scripts": {
    "js": ["js/code.js"],
    "matches": [ "*://www.examplesite.ex/*", "http://*.ex/*" ]
  },
  "permissions": [ "*://www.examplesite.ex/*", "http://*.ex/*" ]
}


来源:https://stackoverflow.com/questions/10041595/chrome-extensions-how-can-i-perform-an-action-when-chrome-browseraction-oncli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!