简体   繁体   English

Chrome 扩展本地存储不起作用

[英]Chrome Extension local storage not working

I have written a simple html page that has one input and three links.我写了一个简单的 html 页面,它有一个输入和三个链接。

"OK" --> Stores whatever is in the filed to local storage “OK”--> 将归档中的任何内容存储到本地存储

"WhoamI" --> Alerts with the value that was saved to local storage "WhoamI" --> 带有保存到本地存储的值的警报

"Check" --> if that value is "asd", it replys with "asd" or not "Asd" “检查”--> 如果该值为“asd”,则回复“asd”或不回复“Asd”

Pretty simple and I have tested this as just a regular html page and it works.非常简单,我已经将其作为常规 html 页面进行了测试,并且可以正常工作。 All of the functions behave as they should.所有功能都按其应有的方式运行。

Here is index.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>

and here is the javascript main.js这是 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");
    }
}

在此处输入图片说明

However, when I import the same exact code as a google chrome extension, everything stops working.但是,当我导入与 google chrome 扩展程序完全相同的代码时,一切都停止工作。

Here is my manifest.json code.这是我的 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"]
}

I am assuming this is the issue:我假设这是问题:问题屏幕截图

I read throughout this but I could not figure it out.我通读了这篇文章,但我无法弄清楚。

Any help will be appreciated任何帮助将不胜感激

Thanks谢谢

It seems you did not completely understand the link you referred to :), there is an inline script section in referenced link, which says inline script will not be executed.看来您没有完全理解您引用的链接:),引用的链接中有一个内联脚本部分,表示不会执行内联脚本。

Changes Made to index.html对 index.html 所做的更改

1) Removed all INLINE JS of 1) 删除了所有的内联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>

to

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

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

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

by assigning id's通过分配id

Final index.html最终 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>

Changes made to main.js对 main.js 所做的更改

1) Added Event handlers to all 3 functions on click of <a/> tags as shown here 1) 点击<a/>标签为所有 3 个函数添加事件处理程序,如下所示

window.onload = function () { window.onload = 函数 () {

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

} }

Final main.js最终的 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;
}

Changes Made to manifest.json对 manifest.json 所做的更改

1) Eliminated un-necessary permissions section in manifest.json 1) 消除了manifest.json不必要的权限部分

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

Final Output最终输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM