Warm tip: This article is reproduced from serverfault.com, please click

why cant I see my lastclicked values inside my local storage?

发布于 2020-12-02 04:06:48

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.

stop btn

local storage image

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);
    }); 
}    
Questioner
studentcs
Viewed
0
Nadia Chibrikova 2020-12-03 04:23:26

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}