0

我正在编写一个扩展,我希望我自己的 cookie 存储mycookiesbackground.js其中存储域、路径、密钥和标志。在我的background.js中,当收到 HTTP 响应时,我调用响应chrome.extension.sendMessage()的每个 Set-Cookie 标头。因此,每个响应在几 m 秒内多次调用此函数。响应后,只存储最后一两个cookie,其余的都丢失了。

我做了以下实验。我用功能连接了一个按钮update_mycookies。单击按钮立即调用该函数并显示mycookies2500ms 后的内容。如果我在 2500 毫秒后读取存储,如果我引入延迟(如下所示),所有四个 cookie 都在那里,update_mycookies但如果我删除延迟,只有最后一个或两个 cookie 被更新,其余的都丢失了。正在使用chrome.extension.sendMessage`chrome.storage.local.set()。我希望我的商店必须更新所有 cookie 以作为响应。

function update_mycookies(){
    chrome.extension.sendMessage({"cdomain":".google.com", "path":"/", "key":"PREF"});
    setTimeout(function() {
        chrome.extension.sendMessage({"cdomain":".google.com", "path":"/", "key":"NID"});}, 1000);

    setTimeout(function(){
        chrome.extension.sendMessage({"cdomain":".google.it", "path":"/", "key":"PREF"}); }, 1500);

    setTimeout(function(){
        chrome.extension.sendMessage({"cdomain":".google.it", "path":"/", "key":"NID"}); }, 2000);
}

监听器onMessage如下:

chrome.extension.onMessage.addListener(  
  function(request,sender,sendResponse) {
 if (request["cdomain"] && request["path"] && request["key"]) {
       chrome.storage.local.get("mycookies", function (items) {
         if (items["mycookies"]) {
          var found = false;
           var entries = items["mycookies"].split(";");
           entries.forEach (function (entry) {
            var dk = entry.split(",");
             if (dk[0] == request["cdomain"] && dk[1] == request["path"] && dk[2] == request["key"])
               found = true; 
           });
           if (!found)
             chrome.storage.local.set({"mycookies": items["mycookies"] + ";" + request["cdomain"] + "," + request["path"] + "," 
               + request["key"] });
         }
        else 
     chrome.storage.local.set({"mycookies": request["cdomain"] + "," + request["path"] + "," + request["key"] });
      }); 
   }
 }
4

0 回答 0