I'm trying to create a chrome extension which will contain a button and once the user will click on the button it will trigger a download event. However, I'm not sure why the button event is not getting triggered.
I'm providing various snippets of my code which I tried :
var buttonExists = document.getElementById("sync");
console.log(buttonExists);
// onClick's logic below:
if (buttonExists) {
buttonExists.addEventListener("click", () => getData());
}
I even tried this code :
document.addEventListener("DOMContentLoaded", function () {
console.log("Dom content loaded");
var buttonExists = document.getElementById("sync");
console.log(buttonExists);
// onClick's logic below:
if (buttonExists) {
buttonExists.addEventListener("click", () => getData());
}
});
getData function :
function getData() {
console.log("In get data");
console.log("Token", token);
console.log("Url", url);
}
Background.html file :
<html>
<head> </head>
<body>
<form>
<button id="sync">Sync</button>
<p id="demo"></p>
</form>
<script type="text/javascript" src="background.js"></script>
</body>
</html>
Manifest.json:
{
"name": "Extension boilerplate",
"version": "0.0.1",
"manifest_version": 2,
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"background": {
"page": "src/bg/background.html",
"persistent": true
},
"page_action": {
"default_icon": "icons/icon19.png",
"default_title": "page action demo",
"default_popup": "src/bg/background.html"
},
"permissions": [
"url",
"webRequest",
"declarativeContent"
],
"content_scripts": [
{
"matches": ["url"],
"js": ["src/bg/background.js"]
}
]
}
Any help is appreciated.