1

我正在尝试在 Chrome 的本地存储中存储一些值的列表,以便在 Chrome 扩展中使用。由于每个列表都与一个 URL 相关联,因此我尝试将 URL 用作键值存储中的键。但是,由于某种原因set(),使用 URL 作为键时似乎会失败(即使typeof(url_variable)显示它只是一个字符串),但是如果我使用一些人为的字符串,例如"hello",我可以正常检索存储的对象。

使用 URL 作为键是否有限制?API中没有提到它。

应该注意的是,Chrome 没有设置runtime.lastError,当我尝试使用get()以前set()带有 URL 的键时,查找只会失败。

这是代码,供参考:

function addNode(url, referrer) {
  nodes = chrome.storage.local;
  edge = {
    in_node: referrer,
    timestamp: Date()
  };

  nodes.get(url, function(current_node){
    console.log(current_node);
    if ( $.isEmptyObject(current_node) === false ) {
      // never executes, because set doesn't work
    }
    else {
      console.log("set: "+url);
      nodes.set({url:[edge]}, function(){
        if ( chrome.runtime.lastError ) {
          console.log(chrome.runtime.lastError);
        }
        else {
          console.log("get: "+url);
          chrome.storage.local.get(url, function(thing) {console.log(thing)});
          console.log("Created new Node for url " + url + " and new edge from " + edge.in_node + " at time " + edge.timestamp);
        }
      });
    }
  });
}
4

1 回答 1

2

如何首先像这样创建对象如何使用变量的值作为键名在 chrome 扩展中使用 chrome.storage?

也可以尝试除了异常而不是使用 if 块?

于 2013-07-15T22:57:09.273 回答