0

I just made a Chrome extension. I followed this tutorial to get a nice button in my toolbar. Now if I load a page, the javascript in my extension gets executed.

Unfortunately, if I click the extension's button in my toolbar, nothing happens.

Question: How do I make the js get executed when I click the extension's button in my toolbar?

Thanks for your help.

Edit I added background.html and it still doesn't work:

<html>
<head>
<script>
  // Called when the user clicks on the browser action.
  chrome.browserAction.onClicked.addListener(function(tab) {
    var action_url = "javascript:mathjax.js";
    chrome.tabs.update(tab.id, {url: action_url});
  });
</script>
</head>
</html>
Matt N.
  • 1,239
  • 2
  • 11
  • 26

2 Answers2

3

You have two options. Either, you can specify a "popup": "popup.html" in your manifest.json, then put the code in popup.html. In this case, the code will get executed in the popup (though of course you can communicate with your background page or anything else).

Probably better would be to put a callback in background.html by attaching a listener to chrome.browserAction.onClicked.

Alex Churchill
  • 4,887
  • 5
  • 30
  • 42
  • Lovely, thank you, can you tell me how to attach a listener to onClicked? – Matt N. Dec 06 '11 at 07:56
  • I put this into background.html, but it still won't trigger: ` Background Page ` – Matt N. Dec 06 '11 at 08:13
  • Odd... I'll play with it later today and see what's up. What happens if you just try to surface an alert onClicked? – Alex Churchill Dec 06 '11 at 23:56
  • Thank you! I think the problem is with the mathjax queue. I posted a separate question: http://stackoverflow.com/questions/8397331/chrome-extension-revisited – Matt N. Dec 07 '11 at 11:51
0

Chrome extension doesn't support inline javascript code in background.html.You must specify external javascript file.Also instead of specifying background.html you can directly specify external file in manifest file in background property.

I have answered similar question here.

Community
  • 1
  • 1
yogesh kumar
  • 940
  • 6
  • 10