如果页面已打开,localStorage将无法运行

时间:2015-02-10 15:36:59

标签: google-chrome-extension local-storage

我创建了一个Google Chrome扩展程序,可以从商家那里获取商家信息。详细信息页面,它使用业务'提取城市,州和邮政编码。借助Google的地理编码API解决问题。我使用localStorage存储城市,州和别针的值,如下所示:

var city = "", state = "", pin = "";
var url = "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(laddress[1].replace(/<.*?>/g,'').replace('&amp;','&').trim())+"&sensor=false&key=AIzaSyCAQmrAKh0jzNVcT1VqTUhjB5Oh_dxl1zA";
console.log(url);
var xhr = new XMLHttpRequest();
var resp;
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "*/*");
xhr.onload = function () {
    var json = xhr.responseText;                         // Response
    json = json.replace(/^[^(]*\(([\S\s]+)\);?$/, '$1'); // Turn JSONP in JSON
    json = JSON.parse(json);  
    console.log(json.results[0].address_components);
    for(var i=0;i<json.results[0].address_components.length;i++){
        if(json.results[0].address_components[i].types[0]=="administrative_area_level_2")
            city = json.results[0].address_components[i].long_name;
        if(json.results[0].address_components[i].types[0]=="administrative_area_level_1")
            state = json.results[0].address_components[i].long_name;
        if(json.results[0].address_components[i].types[0]=="postal_code")
            pin = json.results[0].address_components[i].long_name;
    }
    localStorage.setItem("city", city);
    localStorage.setItem("state", state);
    localStorage.setItem("pin", pin);
}
xhr.send();
lcity = localStorage.getItem("city");
lstate = localStorage.getItem("state");
lpin = localStorage.getItem("pin");

时会出现问题
  • 商家页面已经打开,我尝试点击扩展名,localStorage赢了
  • 有时,它没有显示任何值,有时它会显示值。

访问多个商家页面时,我应该如何在localStorage中获得完美的价值?

1 个答案:

答案 0 :(得分:0)

我相信你使用了错误的localStorage。虽然localStorage是按照HTML5定义的,但它需要一个有根的上下文,如wwww.something.com。

尝试使用chrome.storage

修改文档中的示例。我们来使用本地存储。

var city = "", state = "", pin = "";
var url = "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(laddress[1].replace(/<.*?>/g,'').replace('&amp;','&').trim())+"&sensor=false&key=AIzaSyCAQmrAKh0jzNVcT1VqTUhjB5Oh_dxl1zA";
console.log(url);
var xhr = new XMLHttpRequest();
var resp;
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "*/*");
xhr.onload = function () {
    var json = xhr.responseText;     // Response
    json = json.replace(/^[^(]*\(([\S\s]+)\);?$/, '$1'); // Turn JSONP in JSON
    json = JSON.parse(json);  
    console.log(json.results[0].address_components);
    for(var i=0;i<json.results[0].address_components.length;i++){       
        if(json.results[0].address_components[i].types[0]=="administrative_area_level_2")
            city = json.results[0].address_components[i].long_name;
        if(json.results[0].address_components[i].types[0]=="administrative_area_level_1")
            state = json.results[0].address_components[i].long_name;
        if(json.results[0].address_components[i].types[0]=="postal_code")
            pin = json.results[0].address_components[i].long_name;
    }
    chrome.storage.local.set({“city”: city,
                    "state”: state,
                    "pin”: pin}, function(){
                    // Do stuff here after set.
                    chrome.storage.local.get(["city", "state", "pin",
                        function(items) {
                            lcity = items.city;
                            lstate = items.state;
                            lpin = items.pin;
                        }
                });
}
xhr.send();

请记住修改清单中的权限以使用存储空间。

快速点: xhr.send()是异步调用。紧接着设置l *变量是数据有时出现的问题。处理卸载或local.set回调。上面的例子在回调中有设置。