I'm encountering an error even though I've already loaded the DOM. I have no idea how to fix this issue and I've been sitting here for hours trying to troubleshoot. The error message I'm getting is:
btncheck.js:10 Uncaught TypeError: Cannot read property 'checked' of null
at save_options (btncheck.js:10)
at btncheck.js:28
Below is my popup.js code snippet:
(function() {
if (window.addEventListener) {
window.addEventListener('DOMContentLoaded', domReady, false);
} else {
window.attachEvent('onload', domReady);
}
}());
function domReady() {
onoffbtn = document.getElementById('onoffbtn').checked
}
function save_options() {
chrome.storage.sync.set({
onoffbtn: onoffbtn
}, function() {
setTimeout(function() {
}, 750);
});
}
function restore_options() {
chrome.storage.sync.get({
onoffbtn: true
}, function(items) {
document.getElementById('onoffbtn').checked = items.onoffbtn;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
setInterval(function() {
save_options();
}, 1000);
// Additional code snippets...
This is a shortened version of my manifest.json file:
{
"name": "Hello Extensions",
"description" : "Base Level Extension",
"version": "1.0",
"manifest_version": 2,
"permissions": ["tabs",
"*://*.beta.esportal.se/*",
// Other permissions...
],
"background" : {
// Background script configuration...
},
"content_scripts":[
{
// Content scripts configuration...
}
],
"browser_action": {
"default_popup": "startpage.html"
}
}
This is a section of my background.js file:
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.tabs.executeScript({
file: 'popup.js'
});
}
});
And finally, the content.js file contains the following code:
chrome.runtime.onMessage.addListener(gotMessage);
function gotMessage(message,sender,sendResponse){
if(message == 1){
// Modify the page based on message value being 1
console.log(1);
}else{
console.log(0);
}
}