I seem to be having trouble running a script in my Chrome extension popup.
Here is the info about my project first:
The file popup.html contains a button with the id of newDetails
and an <h1>
tag with the class title
:
popup.html:
<button id="newDetails">Click for details</button>
<h1 class="title">Mr Sanderson</h1>
In a file called background3.js, I have this script:
$(document).ready(function() {
console.log("Test");
document.getElementById("newDetails").addEventListener("click", testClick);
});
function testClick() {
$('.title').html("The New Title");
}
In my manifest.json file I have this:
"content_scripts": [ {
"js": [ "jquery.min.js", "background3.js" ],
"matches": [ "http://*/*", "https://*/*"]
}]
I found this answer which has a similar title, and this is why I added the event listener:
However this is also not working. I made the function only include a console.log("test")
and still nothing.
Eventually, I want to run an AJAX script that gets details from an online JSON file but I can't even seem to get this to work.
How do I get the click of the button to run the function I want?
Additional notes: There is nothing in the console for the page nor the background page.
edit for @Makyen
The HTML I put up earlier in the post is all the HTML in the popup.html file. Apart from the <html>
, <head>
and <body>
tags.
Here is the manifest section you asked for:
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
For your question: Why have you named a content_scripts background3.js? I got this basic template app for Chrome extensions to work with from GitHub. It said to name the JavaScript file whatever I want. So, I just coped what was there, added a 3 and then removed it to be blank. I then added the code above to it. This is, after all, only a play around to learn it so I did not think it would matter on the name.
Next you say: Please do not load jQuery into every http and https page. This was part of the template I got. Again not for production. But thank you for the heads up. I did not know so I will make a note of that. :)
Anything else you need to know?