将本地存储转换为Chrome存储以便获取和设置 [英] Converting local storage to Chrome storage for get and set

查看:87
本文介绍了将本地存储转换为Chrome存储以便获取和设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个userscript,我已经变成Chrome扩展程序,供Chrome用户使用。 userscript设置主题,并有主题可供选择,但仅使用localStorage,除主子域之外的任何内容都将具有默认主题。我希望使用Chrome的存储API可以缓解这种情况,但我很难理解如何让它工作。



以下是原始代码:

  hasGM:typeof GM_deleteValue!==undefined,
get:function(name)
{
var val = this.hasGM?
GM_getValue(NAMESPACE + name):
localStorage.getItem(NAMESPACE + name);

if(val!= undefined)
return JSON.parse(val);

return defaultConfig [name];
},
set:function(name,val)
{
name = NAMESPACE + name;

if(typeof val!==number)
val = JSON.stringify(val);

返回this.hasGM?
GM_setValue(name,val):
localStorage.removeItem(name,val),
localStorage.setItem(name,val);




$ b $ p
$ b这里是我到目前为止的情况,其中val打印为未定义,即使我可以在控制台中完成console.log结果[NAMESPACE + name]和值。

  hasGM:typeof GM_deleteValue!==undefined,
get:function(name)
{
names = NAMESPACE + name;
var val = this.hasGM?
GM_getValue(names):
chrome.storage.local.get(names,function(result){return result [NAMESPACE + name];});

if(val!= undefined)
return JSON.parse(val);
console.log(val);
return defaultConfig [name];
},
set:function(name,val)
{
name = NAMESPACE + name;
setObj = {};
setObj [name] = val;


if(typeof val!==number&& this.hasGM)
val = JSON.stringify(val);
返回this.hasGM?
GM_setValue(name,val):
chrome.storage.local.remove(name),
chrome.storage.local.set(setObj);
}

基本上我无法把头围住它,需要一些帮助

  for(var key in defaultConfig)
$ SS.conf [key] = parseVal(key,this.get(key,函数(_arg){
var val;

val = _arg [key];
return callback(val);
}));


解决方案

您可以 做,我忽略 hasGM 的东西,使事情变得更简单:

  get:function(name,callback){

names = NAMESPACE + name;
chrome.storage.local.get(name,function(val){
if(val!= undefined){
callback(JSON.parse(val));
} else {
callback(defaultConfig [name]);
}
})
},

所以这意味着调用 get 函数的代码也必须改变,我很抱歉,但实际上并没有另一个办法。你可以(也可以是大多数人)转换你的同步的东西来使用回调模式,就像这样:

  get:函数(名称,回调){

名称= NAMESPACE +名称;
if(this.hasGM){
callback(GM_getValue(names));
} else {
chrome.storage.local.get(names,function(val){
if(val!= undefined){
callback(JSON.parse(val) );
} else {
callback(defaultConfig [name]);
}
})
}
},

处理异步函数的一些规则:


    <李>他们不回报任何东西! (除非它们是基于承诺的)
  • 在回调中返回的内容是毫无意义的,它不会去任何地方!
  • 回调之外的代码
  • 将通常在回调之前运行,总是假定回调将在稍后执行,因此您不能依赖任何变量设置/回调中发生的事情!


I have a userscript which I've turned into a Chrome extension for my users that have Chrome. The userscript sets themes and has themes to choose from, but with just using localStorage, anything other than the main subdomain will just have the default theme. I'm hoping using Chrome's storage API will alleviate that, but I'm stumped as to how to get it working.

Here's the original code:

  hasGM: typeof GM_deleteValue !== "undefined",
  get: function(name)
  {
    var val = this.hasGM ?
          GM_getValue(NAMESPACE + name) :
          localStorage.getItem(NAMESPACE + name);

    if (val != undefined)
      return JSON.parse(val);

    return defaultConfig[name];
  },
  set: function(name, val)
  {
    name = NAMESPACE + name;

    if (typeof val !== "number")
      val = JSON.stringify(val);

    return this.hasGM ?
        GM_setValue(name, val) :
        localStorage.removeItem(name, val),
        localStorage.setItem(name, val);
  }
},

And here's what I have so far, where val is printed as undefined even though I can do a console.log of result[NAMESPACE + name] and the values perfectly in the console.

  hasGM: typeof GM_deleteValue !== "undefined",
  get: function(name)
  {
    names = NAMESPACE + name;
    var val = this.hasGM ?
          GM_getValue(names) :
          chrome.storage.local.get(names, function(result){return result[NAMESPACE + name];});

    if (val != undefined)
      return JSON.parse(val);
    console.log(val);
    return defaultConfig[name];
  },
  set: function(name, val)
  {
    name = NAMESPACE + name;
    setObj={};
    setObj[name]=val;


    if (typeof val !== "number" && this.hasGM)
      val = JSON.stringify(val);
    return this.hasGM ?
        GM_setValue(name, val) :
        chrome.storage.local.remove(name),
        chrome.storage.local.set(setObj);
  }

Basically I just cannot wrap my head around it and need some help

    for (var key in defaultConfig)
      $SS.conf[key] = parseVal(key, this.get(key, function(_arg) {
        var val;

        val = _arg[key];
        return callback(val);
      }));

解决方案

here's something you could do, I'm omitting the hasGM stuff to make things simpler :

get: function(name, callback) {

  names = NAMESPACE + name;
  chrome.storage.local.get(names, function(val){
    if (val != undefined) {
      callback(JSON.parse(val));
    } else {
      callback(defaultConfig[name]);
    }
  })
},

So this means the code calling your get function would have to change too, I'm sorry, but there really isn't another way. You can (and most people would) convert your synchronous stuff to use the callback pattern as well though, like this :

get: function(name, callback) {

  names = NAMESPACE + name;
  if(this.hasGM) {
    callback(GM_getValue(names));
  } else {
    chrome.storage.local.get(names, function(val){
      if (val != undefined) {
        callback(JSON.parse(val));
      } else {
        callback(defaultConfig[name]);
      }
    })
  }
},

Some rules for dealing with asynchronous functions :

  • They don't return anything! (unless they're promise based)
  • returning something in the callback is pointless, it won't go anywhere!
  • code outside the callback will usually be run before the callback, always assume the callback will be executed at some later time, and therefore you cannot rely on any variable setting/getting happening in the callback!

这篇关于将本地存储转换为Chrome存储以便获取和设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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