1

只是无法编辑响应标头

chrome.webRequest.onHeadersReceived.addListener(function (details) {
for(i=0;i<details.responseHeaders.length;i++)
{
if(details.responseHeaders[i].name.toLocaleLowerCase()=='content-type')
{
alert(details.responseHeaders[i].name); //it works
details.responseHeaders[i].value='text/plain; charset=utf-8';
}
}
return {responseHeaders: details.responseHeaders};
}, {urls: ["<all_urls>"]}, ['responseHeaders']);

它会提醒 Content-Type 但无法编辑值?

4

2 回答 2

2

刚做了

chrome.webRequest.onHeadersReceived.addListener(function (details) {
    var yeniheaderlar =
        [{
            name : "Content-Security-Policy",
            value : "sil"
        }, {
            name : "Content-Security-Policy-Report-Only",
            value : "sil"
        }, {
            name : "X-Content-Security-Policy",
            value : "sil"
        }, {
            name : "X-WebKit-CSP",
            value : "sil"
        }, {
            name : "X-Frame-Options",
            value : "sil"
        }, {
            name : "X-XSS-Protection",
            value : "sil"
        }, {
            name : "Access-Control-Allow-Origin",
            value : "*"
        }, {
            name : "Access-Control-Allow-Methods",
            value : "POST, GET, OPTIONS, DELETE, PUT"
        }, {
            name : "Access-Control-Allow-Headers",
            value : "*"
        }
    ];
    for (z = 0; z < yeniheaderlar.length; z++) {
        var zamazingo = false;
        for (i = 0; i < details.responseHeaders.length; i++) {
            if (details.responseHeaders[i].name.toLowerCase() == yeniheaderlar[z].name.toLowerCase()) {
                if (yeniheaderlar[z].value == "sil") {
                    details.responseHeaders.splice(i, 1);
                } else {
                    details.responseHeaders[i].value = yeniheaderlar[z].value; ;
                }
                zamazingo = true;
            }
        }
        if (!zamazingo && (yeniheaderlar[z].value != 'sil')) {
            details.responseHeaders.push(yeniheaderlar[z]);
        }
    }
    return {
        responseHeaders : details.responseHeaders
    };
}, {
    urls : ["<all_urls>"],
    types : ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
    ["blocking", "responseHeaders"]);
于 2012-09-29T19:34:52.990 回答
0

你试过details.responseHeaders[i].name = 'text/plain; charset=utf-8';吗?

于 2012-09-28T21:33:26.210 回答