Chrome扩展程序:从本地存储中检索数据

时间:2014-06-23 19:30:38

标签: javascript google-chrome-extension google-chrome-storage

我正在尝试构建一个扩展,在弹出窗口中对测试服务器进行身份验证,并从测试服务器检索数据并通过后台脚本将其存储到本地存储中,并在popup.html上显示相同的内容。问题是,我能够存储来自服务器的数据,但是无法在第一次身份验证中显示它。我必须再次登录,然后我才能显示从测试服务器检索到的数据? 任何提示或建议将不胜感激。

Popup.js:

chrome.runtime.sendMessage({from: "login", user: username, pass: password, subject: "page_details"},function(response) 
{   

    //Sends message to background to start content script.
  var login_response = response.type1;

    if(login_response == "DONE")
    {
        //window.location = "index.html";
    }
  if(login_response == "INVALID")
  {

  }
});

Backgroundscript.js

 if (msg.from && (msg.from === "login")
            && msg.subject && (msg.subject = "page_details"))
      {   
chrome.tabs.executeScript( { file: 'contentscript.js' });
          sendResponse({'type1':"DONE"});
        }

Contentscript.js:

var xhr = new XMLHttpRequest();

    xhr.open(method, url, true);
    xhr.setRequestHeader("Content-Type","application/json");
    xhr.send(JSON.stringify(data));
    var raw_data = xhr.responseText;
    var project_details = JSON.parse(xhr.responseText);


chrome.runtime.sendMessage({from: "content2", subject: "page_details", data: raw_data},function(response) 
    {


    });

backgroundscript.js:

if (msg.from && (msg.from === "content2")
            && msg.subject && (msg.subject = "page_details"))
      {
        var project_name = [];
        chrome.pageAction.show(sender.tab.id);
        var raw_details = msg.data;
        var project_details = JSON.parse(msg.data);

        localStorage["Project"] = JSON.stringify(project_name);
}

并在index.html(扩展的另一个弹出页面)中填充localStorage [" Project"],但我无法一次性填充项目详细信息我必须重新加载页面,然后只有我能够在index.html上填充它

0 个答案:

没有答案