Matt N.
Matt N.

Reputation: 1239

Chrome extension

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>

Upvotes: 0

Views: 1266

Answers (2)

yogesh kumar
yogesh kumar

Reputation: 950

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.

Upvotes: 0

Alex Churchill
Alex Churchill

Reputation: 4957

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.

Upvotes: 3

Related Questions