1 回答
TA贡献1853条经验 获得超6个赞
我在我的扩展中尝试了您的代码(进行了一些细微的修改)并且它有效,所以我怀疑您之前的尝试可能导致存储中卡住了一些东西。这是我所得到的:
var urlSet = new Map();
chrome.storage.sync.get(["activeTabs"], function(items){
if(!items.activeTabs)
return;
urlSet = new Map(JSON.parse(items.activeTabs));
AddLinks();
});
function AddLinks()
{
//There are better ways of doing this!
var myLinks="";
urlSet.forEach(function(value, key) {
myLinks+="<a href='"+value+"' target='_blank'>"+key+"</a>";
});
// You may want to come up with a way to delete links
//before they get out of control and proclaim independence
document.getElementById('mydiv').innerHTML = myLinks;
}
saveItem.onclick = function(element) {
chrome.tabs.query({active: true}, function(tabs) {
urlSet.set(tabs[0].title, tabs[0].url);
chrome.storage.sync.set({ "activeTabs":JSON.stringify(Array.from(urlSet.entries())) });
AddLinks();
});
};
更新似乎将内联脚本添加到弹出窗口会导致安全问题。虽然可以将您的页面列入白名单以使用自己的脚本,但添加新元素的更好方法是使用 document.createElement 创建它们,而不是生成字符串,以便让您了解它的外观:
var delButton = document.createElement("button");
delButton.innerHTML = "delete";
delButton.addEventListener('click',function(){/*something useful happens here*/});
document.getElementById('mydiv').append(delButton);
添加回答
举报