我将尝试使用 Chrome/Firefox 扩展程序删除 JS 中的 localStorage 项,但它似乎不起作用,现在我想知道如何调试此问题,这里是我的代码popup.js:
var btn = document.createElement("BUTTON");
btn.innerText = "Delete matchs";
btn.onclick = function () {
localStorage.removeItem('save_to_storage');
};
我的 popup.html:
<!DOCTYPE html>
<html lang="de">
<head>
<script src='popup.js' defer></script>
</head>
<body>
<div id='button'></div>
</body>
</html>
我的 manifest.json:
{
"name": "TEST",
"version": "2021.07.31",
"manifest_version": 2,
"description": "Test",
"author": "lucki1000",
"browser_action": {
"default_icon": "favicon.ico",
"default_popup": "popup.html"
},
"content_script":[
{
"js": ["popup.js"]
}
],
"permissions": ["tabs", "<all_urls>", "storage", "activeTab"]
}
如果我按下按钮 Delete matchs
,save_to_storage
项目仍会出现在我的开发人员工具中,但如果我直接在控制台中输入 localStorage.removeItem('save_to_storage');
,它会按预期工作。
我在 Firefox 和 Chromium 中对其进行了测试。
我还尝试通过在 popup.js 中使用 alert(localStorage.getItem('save_to_storage'));
而不是 localStorage.removeItem('save_to_storage');
来获取此项目的值,但此警报为空。
会不会是我的popup.js无法访问这个项目?如果是,我该如何解决这个问题?
测试环境:
Chromium Version 92.0.4515.107 (Offical Build) Arch Linux (64-Bit)
Mozilla Firefox 90.0.2 (64-Bit) for Arch Linux
问候
幸运1000