【问题标题】:Chrome Extension local storage not workingChrome 扩展本地存储不起作用
【发布时间】:2012-11-19 06:27:59
【问题描述】:

我编写了一个简单的 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"]
}

我假设这是问题所在:

我通篇阅读了this,但我无法弄清楚。

任何帮助将不胜感激

谢谢

【问题讨论】:

    标签: javascript html google-chrome-extension local-storage


    【解决方案1】:

    看来你没有完全理解你提到的link:),在引用的链接中有一个inline script section,上面写着inline script will not be executed.

    对 index.html 的更改

    1) 移除所有INLINE JS 的

    &lt;a href="#" onclick="saveChanges(); return false"&gt;OK&lt;/a&gt;

    &lt;a href="#" onclick="who(); return false"&gt;WhoAmI&lt;/a&gt;

    &lt;a href="#" onclick="check(); return false"&gt;check&lt;/a&gt;

    到

    &lt;a href="#" id="ok"&gt;OK&lt;/a&gt;

    &lt;a href="#" id="who"&gt;WhoAmI&lt;/a&gt;

    &lt;a href="#" id="check"&gt;check&lt;/a&gt;

    通过分配 id

    最终索引.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) 在点击&lt;a/&gt; 标签时为所有 3 个函数添加了事件处理程序,如下所示

    window.onload = function () {

    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"
      }
    }
    

    最终输出

    【讨论】:

    • 非常感谢。这太棒了。现在我明白他们的意思是“无内联”:D
    猜你喜欢
    • 2011-07-16
    • 2012-12-06
    • 1970-01-01
    • 2013-02-23
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多