1

我正在编写一个扩展来消除请求标头“Referer”的内容问题是,即使发送 HTTP 请求时“Referer”值为空,一旦加载新页面,document.referrer 值是正确的. 无论HTTP请求和响应的“Referer”值如何,document.referrer都是内部获取的吗?

扩展和内容脚本的代码如下...

chrome.webRequest.onBeforeSendHeaders.addListener(
    function(details){
        if(details.frameId == 0){
            var referrer = '';

            // traverse the headervalues and delete 'Referer'
            for (var i = details.requestHeaders.length - 1; i >= 0; i--) {
                if (details.requestHeaders[i].name == 'Referer'){
                    // save the value in order to compare it in the content script
                    referrer = details.requestHeaders[i].value;
                    details.requestHeaders[i].value = '';
                };
            }

            chrome.tabs.sendMessage(details.tabId, { event : "onBeforeSendHeaders", referrer : referrer });
            return({ requestHeaders : details.requestHeaders});
        }
    },
   {urls: ["<all_urls>"]},
   ["blocking", "requestHeaders"]
);


chrome.webNavigation.onDOMContentLoaded.addListener(
    function onDOMContentLoaded(details){
        if(details.frameId == 0){
            chrome.tabs.sendMessage(details.tabId, { event : "onDOMContentLoaded" });
        }
    }
);

和内容脚本

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        switch(request.event){
            case "onBeforeSendHeaders" :
                console.log("onBeforeSendHeaders:" + request.referrer);
                break;          
            case "onDOMContentLoaded" :
                // this should be blank but instead, it has the same value 
                // of the request.referrer, which holds the value before being deleted
                console.log("onDOMContentLoaded:" + document.referrer);
                break;
            default : 
                console.log("default");
        }
    }
);

关于我做错了什么的任何建议?

4

0 回答 0