Bypassing the "no popup" if I want a javascript to run when extension clicked
Asked Answered
M

1

3

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?

Mercury answered 15/7, 2013 at 2:37 Comment(0)
B
2

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.

Beamy answered 15/7, 2013 at 4:8 Comment(2)
Hi. The onload works! However the function that I need to run resides in another file (the background.js file for the extension). Can I somehow call it?Mercury
You should accept this answer if it answers your question rather than asking a second question in the comments. As for your second question, that appears to be a duplicate of #13778387 or something similar.Beamy

© 2022 - 2024 — McMap. All rights reserved.