I am creating a google chrome extension and I'm having trouble using local storage to save my last clicked button value. I am doing this so that the last clicked button continues to show after I exit off popup. I feel like I'm getting close but for some reason it doesn't want to show the same button after I exit popup. I've tried using local storage so the last value will be saved and so the button stays the same.
However when I go to application >> local storage while inspecting, I don't see anything of lastclicked values being saved in there.
I may be going about this wrong but do anyone know what the issue may be?
//Start and Stop buttons for logging
const btnStart = document.getElementById("click-start");
const btnStop = document.getElementById("click-stop");
var lastClicked = document.getElementById("btnStartID");
//button to start/stop logging
document.addEventListener("DOMContentLoaded", function () {
//get lastClicked first to make decisons
chrome.storage.local.get(['lastClicked'], function(result) {
if (result == document.getElementById("btnStartID")) {
//works
btnStart.style.display= "none";
btnStop.style.display= "block";
console.log("last clicked is start button");
Logger(true);
} else if (result == document.getElementById("btnStopID")) {
//not working
btnStart.style.display= "block";
btnStop.style.display= "none";
Logger(false);
console.log("last clicked is stop button");
} else {
//to see if its grabbing id elements
console.log("else statement");
Logger(true);
}
//works
btnStart.addEventListener("click", function() {
lastClicked = document.getElementById("btnStartID");
Logger(true);
chrome.storage.local.set({'lastClicked': lastClicked}, function() {
console.log('logging started successful');
});
});
btnStop.addEventListener("click", function() {
lastClicked = document.getElementById("btnStopID");
Logger(false);
chrome.storage.local.set({'lastClicked': lastClicked}, function() {
console.log('logging stopped successful');
});
});
});
});
chrome.storage.local.get(['key'], function(result) {
console.log('value currently is ' + result.key);
});
//attempt to get start/stop logging buttons to work--underwork
function Logger(isLogging) {
let logger =''
if (isLogging){
btnStart.style.display= "none";
btnStop.style.display= "block";
logger = 'logging'
addRow();
} else {
btnStart.style.display= "block";
btnStop.style.display= "none";
logger = 'not logging'
}
//using storage API to save data for last btn pressed--underwork
chrome.storage.local.set({key: logger}, function() {
console.log('value is set to ' + logger);
});
}
You can only save strings to local storage, so Chrome ignores your attempt to save a whole button, A better solution will be to save button id, so you will have
if (result.lastClicked == "btnStartID") {
...
chrome.storage.local.set({'lastClicked': lastClicked.id}
Hello, I keep reciving an Uncaught TypeError: Cannot read property 'id' of null at HTMLButtonElement.<anonymous> error @Nadia Chibrikova
Looks like your lastClicked is null, not a big problem in terms of saving into storage, as you know the id, you just found the element based on this id. In fact you don't need to look up the element as you don't do anything with it, just save the id. However - which button you click to get the error? your ids seem a bit inconsistent is it "click-start" or "btnStartID"?
The "click-start" / "click-stop" is whats being clicked. The 'click-start'/'click-stop' are my buttons and they show on my html page. I was trying to track what was last clicked so btnStartID is what I'm trying to get to show in local storage. If that makes sense.
Tbh I'm not sure what btnStartID is supposed to do. Try replacing it with click-start, same for stop
@ Nadia Chibrikova Thank you! That actually got it to work. I guess I was going about it the wrong way.