chrome.storage.local.set不会在本地保存数据 [英] chrome.storage.local.set doesn't save data locally

查看:405
本文介绍了chrome.storage.local.set不会在本地保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个chrome扩展,我必须在本地存储一些数据。下面给出的代码是我的popup.html,我不明白为什么这不会在本地存储中设置值。我的manifest.json具有存储和标签权限。

I'm trying to build a chrome extension in which I have to store some data locally. Code given below is my popup.html and I can't understand why this doesn't set values in local storage. My manifest.json has "storage" and "tabs" permission.

<script type="text/javascript">
function save() {
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  chrome.storage.local.set({'username': username}, function(){});
  chrome.storage.local.set({'password': password}, function(){}); 
}
</script>
Roll Number
<input type="text" name="username" id="username"></input>
Password
<input type="password" name="password" id="password"></input>
<button onclick="save()" type="submit" name="submit">Save</button>

这是我的manifest.json文件。

This is my manifest.json file.

{
  "manifest_version": 2,

  "name": "Autologin",
  "description": "Blah bhla.",
  "version": "1.0",
  "permissions": [
    "http://xxxxxxxxxxxxx/*",
    "storage"
  ],
  "content_scripts": [
    {
      "matches": ["http://xxxxxxxxxxxxx/*"],
      "js": ["autologin.js"]
    }
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "autologin.html"
  }
}


推荐答案

通过创建一个 popup.js 并将其包含在您的html中。

You need to move the script part out of html by creating a popup.js and include it in your html.

popup.js

function save() {
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  chrome.storage.local.set({
   'username': username ,
   'password': password
  }, function(){});

}

document.getElementByName("submit").onclick=save;

popup.html

< script> ...< / script> 作为< script src = popup.js> < / script>

manifest.json中

In manifest.json

"default_popup": "popup.html" //the name of your popup html file

这篇关于chrome.storage.local.set不会在本地保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆