I'm currently learning to develop a google chrome extension. I'm trying to get a button to list all of the span
elements in the console for now. However, I can't seem to set the function of the button and keep getting a weird Uncaught TypeError: Cannot set property 'onclick' of null
error.
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Simple Greeting Dashboard</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<button id="main-switch">Update prices:</button>
<script src="main.js" defer></script>
</body>
</html>
main.js:
window.onload = function() {
console.log("This is working!");
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'steamcommunity.com' },
})],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
function update_prices() {
var elements = document.getElementsByTagName('span');
console.log("Elements:", elements);
}
document.getElementById('main-switch').onclick = update_prices;
};
manifest.json:
{
"name": "Steam price helper",
"version": "1.0",
"description": "Helps with prices on the Steam community market",
"incognito": "split",
"background": {
"scripts": ["main.js"],
"persistent": true
},
"permissions": [
"activeTab",
"storage",
"declarativeContent"
],
"page_action": {
"default_popup": "index.html",
"default_icon": {
"256": "download.png"
}
},
"manifest_version": 2
}
I've looked at other questions that have the same problem, but none of those solutions seem to work. Thanks in advance!