2013-07-15 28 views
0

我試圖在Chrome的本地存儲中存儲一些用於Chrome擴展的值的列表。由於每個列表都與一個URL相關,因此我試圖將該URL用作鍵值存儲中的鍵。但是,由於某種原因,當使用URL作爲關鍵字時,set()似乎失敗(儘管typeof(url_variable)顯示它只是一個字符串),但如果我使用一些人造字符串(如"hello"),我可以正常檢索存儲的對象。在Chrome本地存儲詞典中使用URL作爲密鑰

使用URLs作爲密鑰有限制嗎? 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); 
     } 
     }); 
    } 
    }); 
} 
+0

使用uri,'sessionStorage.setItem('http://foo.bar/f.b','hello world')測試了_sessionStorage_接口; sessionStorage.getItem( 'http://foo.bar/f.b'); //「hello world」'你想要存儲什麼? –

+0

我正在存儲'邊緣'對象的列表,這些對象基本上只是包含兩個字符串的對象......可能這些對象沒有正確序列化? –

回答