Attila Kling
Attila Kling

Reputation: 1787

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

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?:/

Upvotes: 0

Views: 2938

Answers (1)

Rob W
Rob W

Reputation: 349252

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/*" ]
}

Upvotes: 1

Related Questions