Chrome扩展:从localstorage切换到chrome.storage.local [英] chrome extension: switching from localstorage to chrome.storage.local

查看:3920
本文介绍了Chrome扩展:从localstorage切换到chrome.storage.local的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于得到了我的第一个JS脚本,按照计划在Firefox中运行。现在我需要切换到chrome.storage,因为它不会在我的扩展中运行。



基本上我想更新一个HTML文件中的Value,它可以被改变通过输入一个提交框。



欢迎您在我的代码中挑选并告诉我什么是完全不正常的。但我对JS很新,只是试图让它起作用。由于这与localstorage工作,我想我必须让它与铬存储一起工作。一旦我开始工作,清理这个烂摊子就可以开始了。先谢谢你! (我试图解决这个问题,因为大约12个小时没有,无法抓住它)。

检查脚本时出现的错误:

 未捕获错误:形式调用get(string)不匹配定义get(可选的字符串或数组(扩展::存储区域:35:14)
at normalizeArgumentsAndValidate(extensions :: schemaUtils:112:11)
at StorageArea.self。(anonymous function)[as get]
at chrome-extension://fcbadpjebgjnohhcihmkopdbnbjjmnod/initialize_id.js:5:26

<

  strong> options.html 这是设置文件,其中一个输入可以更改的id。 < /头> 
< body>

从tsindex.com输入您的ID以下< / p>
< form>
< input id =ts_id_js_htmltype =textvalue =128856>< / input>
< br>
< br>
< button onclick =store()>保存/加载< /按钮>
< / form>

< script src =initialize_id.js>< / script>
< script src =options.js>< / script>
< script src =load_id.js>< / script>
< / body>
< / html>

options.js 这会将输入字段中的给定ID存储在chrome中.storage.local(在localstorage之前)

  function store(){
var input_id = document.getElementById(ts_id_js_html )。值;
chrome.storage.local.set(ts_id_js,input_id);
}

initialize.js if chrome.storage.local未定义执行X一次。否则它会获得存储值并替换该字段中的输入。

  var init_count; 

if(chrome.storage.local.get(ts_id_js)=== undefined&& init_count == undefined){
init_count ++; //现在忽略它
} else {
document.getElementById(ts_id_js_html)。value = chrome.storage.local.get(ts_id_js);
}

load_id_to_popup.js .html被打开,并在必要的字段中输入给定的ID。

  document.getElementById('input_id')。dataset。 serverid = 
chrome.storage.local.get(ts_id_js);

IGNORE! load_id.js 这是目前的REDUNDANT,忽略此。

  document.getElementById(ts_id_js_html) .value = 
chrome.storage.local.get(ts_id_js);


解决方案

chrome.storage.local .get 是异步的,这意味着Chrome会将回调函数的值返回给客户端:

  chrome.storage .local.get(ts_id_js,function(obj){
let value = obj [ts_id_js];
});


I finally got one of my first JS scripts running as planned in Firefox. Now I need to switch to chrome.storage because it will otherwise not run in my extension.

Basically I want to update a Value in a HTML file, which can be changed by input in a submission box.

You are very welcome to pick around in my code, and tell me what is totally abnormal. But I am quite new to JS, and am simply trying to get this to work. Since this worked with localstorage, I guess I have to get it to work with chrome storage too. Once I get it to work, cleaning this mess can begin. Thank you in advance! (I am trying to solve this since around 12 hours not. Can't grasp it.)

the error i get when examining the script:

Uncaught Error: Invocation of form get(string) doesn't match definition get(optional string or array or object keys, function callback)
    at normalizeArgumentsAndValidate (extensions::schemaUtils:112:11)
    at StorageArea.self.(anonymous function) [as get] (extensions::StorageArea:35:14)
    at chrome-extension://fcbadpjebgjnohhcihmkopdbnbjjmnod/initialize_id.js:5:26

options.html this is the "settings" file where one inputs the id which shall be changeable.

      </head>
  <body>
    <p>ENTER YOUR ID FROM tsindex.com BELOW</p>
      <form>
          <input id="ts_id_js_html" type="text" value="128856"></input>
          <br>
          <br>
          <button onclick="store()">Save/Load</button>
      </form> 

<script src="initialize_id.js"></script>
<script src="options.js"></script>
<script src="load_id.js"></script>
  </body>
</html>

options.js this stores the given ID from the input field in chrome.storage.local (before localstorage)

function store(){
 var input_id = document.getElementById("ts_id_js_html").value;
 chrome.storage.local.set("ts_id_js", input_id);
}

initialize.js if chrome.storage.local is undefined executes X once. else it gets the stored value and replaces the input in the filed.

var init_count;

if (chrome.storage.local.get("ts_id_js") === undefined && init_count == undefined) {
    init_count++; //ignore this for now
} else {
    document.getElementById("ts_id_js_html").value = chrome.storage.local.get("ts_id_js");
}

load_id_to_popup.js this gets loaded when the popup.html gets opened, and inputs the given ID in the necessary field.

 document.getElementById('input_id').dataset.serverid =
 chrome.storage.local.get("ts_id_js");

IGNORE ! load_id.js THIS IS CURRENTLY REDUNDANT, ignore this.

 document.getElementById("ts_id_js_html").value =
 chrome.storage.local.get("ts_id_js");

解决方案

chrome.storage.local.get is asynchronous, it is mean, Chrome returns value to callback function:

chrome.storage.local.get("ts_id_js", function(obj) {
    let value = obj["ts_id_js"];
});

这篇关于Chrome扩展:从localstorage切换到chrome.storage.local的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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