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
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
popup.js.popup.js.popup.jspopup.htmlIf you test the previous code, you’ll notice that it does not work. That’s because the contents of
popup.jsis 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):<script src="popup.js"></script>before the closing</body>tag inpopup.html. Then, you’re certain that all elements do exist when the script is executed.Alternative: Wrap the event listener call in a
DOMContentLoadedevent:Background page.
For a more thorough explanation on the available options, see the middle of this answer.
"background": {"scripts": ["background.js"]}.background.htmltobackground.js, and remove<script>and</script>from the file.