如何实现onHeadersReceived,onBeforeSendHeaders和onAuthRequired之间的沟通

时间:2018-02-26 09:23:25

标签: javascript google-chrome-extension

我需要实现一个允许用户在同一站点上的扩展 在Chrome浏览器的同一副本中的两个不同标签下的两个不同帐户下。

现在我的代码工作正常,首先调用onBeforeSendHeaders,然后才调用onHeadersReceived,我的代码现在工作不正常。如何使用onAuthRequired处理呼叫顺序?我需要首先调用onHeadersReceived然后onBeforeSendHeaders。

chrome.webRequest.onHeadersReceived.addListener(function(details) {

var detResp = details.responseHeaders;
    tabId = localStorage.getItem('tabName'),
    userVal = localStorage.getItem('userVal'),
    state = localStorage.getItem('currentState');

    console.log("1");//it's started second

if(typeof localStorage[userVal] === 'undefined') { 
    user = {};
    user["tabs"] = {};
}else{
    user = JSON.parse(localStorage[userVal]);
}

if (state) {
    localStorage.setItem('currentState', false);

    detResp.forEach(function(item, i, detResp) {

        if (item.name === 'set-cookie') {
            var cook = getCookieFromSetCookie(item.value);
            if (cook && userVal){
                user["tabs"]["tab"] = tabId;
                user[userVal] = {"name": cook["name"], "value": cook["value"]};
            }
        } 
    });
}
localStorage[userVal] = JSON.stringify(user);

},{urls:[“”]},[“阻止”,“responseHeaders”]);

chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
var detReq = details.requestHeaders,
    tabId = details.tabId,
    userVal = localStorage.getItem('userVal');

   console.log("2");//it's started first

if (typeof localStorage[userVal] != 'undefined'){

    JSON.parse(localStorage[userVal]);
    var userTab = user["tabs"]["tab"];

    if(userTab && userVal) {

        detReq.forEach(function(item, i, detReq) {
            if (item.name === 'Cookie') {
                item.value = concatCookie(user[userVal]);       
            }
        });
    }
}

return {requestHeaders: details.requestHeaders};

},{urls:[“”]},[“阻止”,“requestHeaders”]);

0 个答案:

没有答案