在Chrome扩展程序中,更改发送到某个域的ajax请求的引荐来源? [英] In Chrome Extension, change referrer for ajax requests sent to certain domain?

查看:192
本文介绍了在Chrome扩展程序中,更改发送到某个域的ajax请求的引荐来源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些研究,现在我知道不可能在谷歌浏览器中发送带有更改的推荐人的请求,因为浏览器会覆盖更改,但无论如何/谷歌Chrome扩展程序中的任何权限都将禁用这个,或者使它可以使用不同的推荐人向某个域发送请求?

I've done some research and I now know it's not possible to send a request with a changed referrer in Google Chrome because the browser will overwrite the change, but is there anyway/any permissions in a Google Chrome Extension that would disable this, or make it so that you could send a request to a certain domain with a different referrer?

推荐答案

chrome.webRequest 正是您所需要的,特别是 onBeforeSendHeaders 事件。它允许您在发送请求之前更改任何标头(甚至是不安全的标头),但只能在后台脚本中使用。

chrome.webRequest is what you're looking for, specifically thee onBeforeSendHeaders event. It will allow you to change any headers (even unsafe ones) before sending the request, but can only be used in a background script.

您需要添加 webRequest webRequestBlocking 到清单中的权限列表。

You'll need to add webRequest and webRequestBlocking to your permissions list in the manifest.

chrome.webRequest.onBeforeSendHeaders.addEventListener(handle(details), filterObject, extraInfoArray);

以下是一个例子:

chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
    var newRef = "http://referer.domain/helloworld.example";
    var gotRef = false;
    for(var n in details.requestHeaders){
        gotRef = details.requestHeaders[n].name.toLowerCase()=="referer";
        if(gotRef){
            details.requestHeaders[n].value = newRef;
            break;
        }
    }
    if(!gotRef){
        details.requestHeaders.push({name:"Referer",value:newRef});
    }
    return {requestHeaders:details.requestHeaders};
},{
    urls:["http://target.domain/*"]
},[
    "requestHeaders",
    "blocking"
]);

filterObject 告诉它只触发处理任何与列表中的匹配的URL。

The filterObject tells it to only fire the handle for any with the urls matching ones in the list.

extraInfoArray 告诉它你想得到 requestHeaders 阻止告诉它暂停请求,直到句柄结束。

The extraInfoArray tells it you want to get requestHeaders, and blocking tells it to pause the request until the handle is finished.

这篇关于在Chrome扩展程序中,更改发送到某个域的ajax请求的引荐来源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆