0

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

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
Attila Kling
  • 1,717
  • 4
  • 18
  • 32
  • The code is working correctly, provided that you click the button at one of the URIs as mentioned at the `permissions` section. When the button is clicked at another location (eg. `about:blank`), the following expected error will show up: `"Error during tabs.executeScript: Unknown error."` – Rob W Apr 06 '12 at 09:35
  • But for me, when i visit one site of the allowed ones, and click to the icon.., nothing happens. For example, code injected is really simple: **'document.title="newtitle";'** Although, title does not change! :( – Attila Kling Apr 06 '12 at 14:04
  • Are the match patterns really valid? Go to `chrome://extensions/`, Developer mode, and open the console for the background page. Does any error show up? – Rob W Apr 06 '12 at 14:38
  • hmm.. i made the extension at job, and now, as i arrived home, i launched the extension, and its going :O strange. Thanks for the help by the way. :) Next step to make it automatic, without any click, i think it will be a page action. – Attila Kling Apr 06 '12 at 18:48

1 Answers1

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/*" ]
}
Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678