Chrome存储空间同步设置并无法正常工作

时间:2019-01-16 20:57:21

标签: javascript jquery google-chrome-extension

我正在尝试添加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;
        });
    });
}

0 个答案:

没有答案