首页 > 解决方案 > Chrome 存储同步设置并无法正常工作

问题描述

我正在尝试制作 chrome 扩展以使我的工作更轻松,但我的代码有问题。我遇到的问题是我试图将一个变量传递给 chrome.storage.sync.set 但我认为它的注册并不像它需要的那样,因为它总是显示未定义。

$(document).ready(function() {
    //Check if output is not empty
    if(output.innerHTML != ""){
        //Onclick function
        $("a").click(function() {
            //Get id of element and pass into clickedElemID
            var clickedElemID = $(this).attr('id');
            //Run handler functin
            handler(clickedElemID);
        });
    }
    //If empty, set innerHTML to No Selection Found
    else{
        output.innerHTML = "No selection found";
    }
});

function handler(elem){
    //Get innerHTML of element and pass into output
    var output = document.getElementById("output").innerHTML;
    //Set sync storage for specified element id and store output
    chrome.storage.sync.set({[elem]: output}, function() {
        //Annonymous function as follows:
        //Get sync storage for same element
        chrome.storage.sync.get(elem, function(data) {
            document.getElementById(elem).nextElementSibling.classList.remove('na');
            document.getElementById(elem).nextElementSibling.classList.add('tx4');
            document.getElementById(elem).nextElementSibling.innerHTML = data.elem;
        });
    });
}

标签: javascriptjquerygoogle-chrome-extension

解决方案


推荐阅读