问题
I have a chrome extension which I want to fire whenever I press the popup. Currently I can do this only if I remove the popup.html from manifest.json, since there's a restriction on that. However, I need the popup.html
How do I get both?
I have tried placing the exact same thing everywhere, in the popup.html, in the popup.js (which is linked correctly in the popup.html) and nothing.
In short, if I place this in the background.js:
chrome.browserAction.onClicked.addListener(function(info, tab) {
speakMe();
});
It works.
However, that implies no popup.html in the manifest. So, naturally, I try placing this:
<script>
chrome.browserAction.onClicked.addListener(function(info, tab) {
speakMe();
});
</script>
in the popup.html. It doesn't work. Pressing the icon displays the popup normally, but nothing happens.
I tried placing it in the popup.js too, still nothing. What am I doing wrong?
回答1:
Add an onload listener into your popup.js:
onload = function() { console.log("I loaded!"); };
In that function you can do interesting things when the popup appears. If you want to get more fancy than that, you might run into issues described in https://code.google.com/p/chromium/issues/detail?id=31262.
By the way, you haven't told us what "It doesn't work" means (which line fails? Are there console error messages?), so it's hard to be sure that this answer (or any) is helpful to you. See https://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist for steps to follow to make sure that answers given are answers to your question.
来源:https://stackoverflow.com/questions/17645977/bypassing-the-no-popup-if-i-want-a-javascript-to-run-when-extension-clicked