javascript - 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;
});
});
}
解决方案
推荐阅读
- ios - Salesforce 使用移动 sdk 集成响应原生 APP 始终将应用程序创建为模板
- google-sheets - 设置负值以显示并将其视为零与其他公式结合
- javascript - 我的阅读更多/阅读更少没有显示标题标签
- android - 如何将服务器图像 url 转换为 Drawable Int
- cmd - 有没有办法在 Windows 命令提示符下以秒为单位获取当前时间?
- r - 如何删除警告:Rcpp/DataFrame.h:136:18: 警告未使用的变量“数据”?
- python - TypeError:一元+的错误操作数类型:'str'当我认为代码正确时
- spring-boot - 当消费者的处理时间超过 max.poll.interval.ms 时,消费者没有下线
- python - 我想在 profile.html 模板-django 中显示注册人的详细信息,例如(名字、电子邮件、电话号码)
- json - 如何修复“预计解码字典
但找到了一个数组。”,基础错误:无