4

我写了一个简单的 html 页面,它有一个输入和三个链接。

"OK" --> 将文件中的任何内容存储到本地存储

"WhoamI" --> 带有保存到本地存储的值的警报

"Check" --> 如果该值为 "asd",则回复 "asd" 或不回复 "Asd"

非常简单,我已经将它作为一个普通的 html 页面进行了测试,它可以工作。所有功能都按应有的方式运行。

这是 index.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> System Information </title>
<script src="main.js"></script>
</head>
<body>
<input type="text" name="text" id="text">
<a href="#" onclick="saveChanges(); return false">OK</a>
<a href="#" onclick="who(); return false">WhoAmI</a>
<a href="#" onclick="check(); return false">check</a>

</body>
</html>

这是javascript main.js

function saveChanges() {
  var theValue = text.value;

localStorage["ChromeLogin"] = theValue;
}
function who() {
    alert(localStorage["ChromeLogin"]);
}
function check(){
    var test = localStorage["ChromeLogin"];
    if (test == "asd"){
        alert("it is asd");
    }else{
        alert("It is NOT asd");
    }
}

在此处输入图像描述

但是,当我导入与 google chrome 扩展相同的代码时,一切都停止工作了。

这是我的 manifest.json 代码。

{
  "name": "testLocalStorage",
  "version": "0.0.1",
  "description": "Capture a tab",
  "options_page": "index.html",

  "manifest_version": 2,

  "browser_action": {
  "default_title": "Capture tab"
  },
  "permissions" : ["tabs", "<all_urls>", "storage"]
}

我假设这是问题所在: 问题截图

我通读了这篇文章,但我无法弄清楚。

任何帮助将不胜感激

谢谢

4

1 回答 1

3

看来你没有完全理解你提到的链接:),在引用的链接中有一个内联脚本部分,它说内联脚本不会被执行。

对 index.html 所做的更改

1)删除了所有的INLINE JS

<a href="#" onclick="saveChanges(); return false">OK</a>

<a href="#" onclick="who(); return false">WhoAmI</a>

<a href="#" onclick="check(); return false">check</a>

<a href="#" id="ok">OK</a>

<a href="#" id="who">WhoAmI</a>

<a href="#" id="check">check</a>

通过分配 id

最终 index.html

<html>
<head>
<title> System Information </title>
<script src="main.js"></script>
</head>
<body>
<input type="text" name="text" id="text">
<a href="#" id="ok">OK</a>
<a href="#" id="who">WhoAmI</a>
<a href="#" id="check">check</a>

</body>
</html>

对 main.js 所做的更改

1) 向所有 3 个函数添加事件处理程序,点击<a/>标签,如下所示

window.onload = 函数(){

document.getElementById('ok').onclick = saveChanges;
document.getElementById('check').onclick = check;
document.getElementById('who').onclick = who; 

}

最终 main.js

function saveChanges() {
  var theValue = text.value;

  localStorage["ChromeLogin"] = theValue;
}
function who() {
    alert(localStorage["ChromeLogin"]);
}
function check(){
    var test = localStorage["ChromeLogin"];
    if (test == "asd"){
        alert("it is asd");
    }else{
        alert("It is NOT asd");
    }
}
window.onload=function (){
    document.getElementById('ok').onclick=saveChanges;
    document.getElementById('check').onclick=check;
    document.getElementById('who').onclick=who;
}

对 manifest.json 所做的更改

1)消除了不必要的权限部分manifest.json

最终清单.json

{
  "name": "testLocalStorage",
  "version": "0.0.1",
  "description": "Capture a tab",
  "options_page": "index.html",

  "manifest_version": 2,

  "browser_action": {
  "default_title": "Capture tab"
  }
}

最终输出

在此处输入图像描述

于 2012-12-01T05:16:19.523 回答