1

i got a simple question i have these3 files popup.html :

<script>
    function buttonClicked(button) 
    {
        chrome.extension.sendRequest({command:button.id});
    }
</script>
    <input style="width:100%" type="button" value="Click me" id="click" onclick="buttonClicked(this)"/><br/>

background.html :

<script>
    function processRequest(request, sender, sendResponse) 
    {
        alert('hi');
        sendResponse({});
    }
    chrome.extension.onRequest.addListener(processRequest);
</script>

and contentscript.js

s = document.getElementsByClassName('st');
if (s[0].innerText != '') { 
st = new Array(); 
for (i = 0;i<s.length;i++) {
    st[i] = s[i].innerText;
}
chrome.extension.sendMessage({"message" : st}, function(response) {});
}

i would like to fire up the contentscript each time i click on the button in the popup page be cause somehow the script in the contentscript dosnt work neither or background.html nor on popup.html?

thank you

Ouerghi Yassine
  • 1,835
  • 7
  • 43
  • 72
  • Thanks to manifest version 2 and the CSP. Solution: Move the JavaScript to an external file. Possible duplicate of [Port error while changing chrome extension from manifest v1 to v2](http://stackoverflow.com/questions/11913575/port-error-while-changing-chrome-extension-from-manifest-v1-to-v2) – Rob W Aug 12 '12 at 08:45
  • if manifest 2 caused this error can i just use the version 1?! – Ouerghi Yassine Aug 12 '12 at 09:07
  • Manifest v1 support is being phased out. Why don't you use the solution I presented? It's the recommended way to get your extension to work. – Rob W Aug 12 '12 at 09:14
  • well that dosnt really solve my problem, all what i want is to execute the contentscript when i click on the button in the popup page thats it, because when i execute it from the background page or the popup page i cant really get to the current tab elemnts.* – Ouerghi Yassine Aug 12 '12 at 13:26
  • I guess that you didn't understand my other comment and answer. The solution is to move the inline JavaScript to an external file, and refer to that external file using ` – Rob W Aug 12 '12 at 13:42
  • so in my case i put the background script into a external file, then just call it? how it will be executed?! – Ouerghi Yassine Aug 12 '12 at 13:48

1 Answers1

2

I've attempted to explain the solution and why to use it in the comments, three times. Since you don't understand it, I'll explain it again with references to your code:

Popup

  • Move the inline script to an external file, say popup.js.
  • Delete the inline event listener, and also move it to popup.js.

popup.js

function buttonClicked(button) {
    chrome.extension.sendRequest({command: button.id});
}
document.getElementById('click').addEventListener('click', function() {
    buttonClicked(this);
});

popup.html

<script src="popup.js"></script>
<input style="width:100%" type="button" value="Click me" id="click"><br>

If you test the previous code, you'll notice that it does not work. That's because the contents of popup.js is executed before the button (which occurs after the <script src> is created. This can be solved in two ways (use either method, but not both):

  • Recommended: Place <script src="popup.js"></script> before the closing </body> tag in popup.html. Then, you're certain that all elements do exist when the script is executed.
  • Alternative: Wrap the event listener call in a DOMContentLoaded event:

    document.addEventListener('DOMContentLoaded', function() {
        document.getElementByUd('click').addEventListener( ... );
    });
    

Background page.

For a more thorough explanation on the available options, see the middle of this answer.

  • Modify the manifest file, use "background": {"scripts": ["background.js"]}.
  • Rename background.html to background.js, and remove <script> and </script> from the file.
Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • so i have done as u said, it kinda work, but it didnt actually work, i mean the script works ... but not the way i wanted, i wanted the popup.js gets access to the page elements ! not the popup.html elements ... like the content script, when a page is fully loaded, i can get to the current page elements – Ouerghi Yassine Aug 13 '12 at 07:44
  • ok, i think i found a solution, is to use executeScript and execute the contentScript.js in executeScript. function hello(){ chrome.tabs.executeScript(null,{ code: "s = document.getElementsByClassName('st');" + "st = s[0].innerText;"+ "chrome.extension.sendMessage({\"message\":st});"}) }; but is there another way other that executeScript?? – Ouerghi Yassine Aug 13 '12 at 20:48
  • @yassine_hell Yes, you can inject a content script via the manifest file, which defines a message listener. Then, use [`chrome.tabs.sendMessage`](http://code.google.com/chrome/extensions/extension.html#method-sendMessage) to send a message, and use the `sendResponse` callback to send back *JSON-serializable* data (no nodes, but strings!). But your current solution looks OK, because it only uses content scripts when necessary. – Rob W Aug 13 '12 at 20:53
  • ok thanks for your help, and im sorry for ur time ... just started with google extensions :s ... – Ouerghi Yassine Aug 13 '12 at 21:19