收到响应后使用Chrome webRequest进行网址转发 [英] URL forwarding using Chrome webRequest after response is received
问题描述
我试图创建一个Chrome扩展程序,利用Chrome的 webRequest 模块执行重定向到从最初访问的URL获取的URL。为此,我只想使用Chrome的webRequest函数(例如,onBeforeSendHeaders,onHeadersReceived),而不是简单地使用获取的URL执行对$ .ajax()的调用。所需功能如下:
chrome.webRequest.onHeadersReceived.addListener(
function(details){
var secondaryURL = extractSecondaryURL(details);
return {redirectUrl:secondaryURL}; //这不起作用
},
{url:[http:// * / *,https:// * / *]},
[blocking,responseHeaders]
);
...但页面永远不会被转发。 webRequest文档说:仅用于响应onBeforeRequest事件。
如何使用从响应头接收到的数据执行这种转发? Chrome webRequest模块?
web请求API 方法
- onBeforeRequest 允许取消或重定向请求
- onBeforeSendHeaders , onHeadersReceived 允许取消请求或修改标题 >
- onAuthRequired 允许提供\修改认证凭证。
所以,想法是存储来自onHeadersReceived Event的辅助URL并激发 chrome.tabs.reload()
事件再次触发onBeforeRequest事件,这有助于重定向。
示例演示
以下未经测试的演示模块全部 Facebook
网址并将其重定向至 Google
在收到辅助网址后,您可以进一步自定义。
manifest.json
确保所有权限都可用并注册带有扩展名的后台页面。
{
name:Hanlder for Navigation,
description:http://stackoverflow.com/questions/16928912/url-forwarding-using-chrome-webrequest-after-response-is-已收到,
版本:1,
manifest_version:2,
background:{
scripts:[background.js]
},
权限:[https://www.facebook.com/*,webRequest,webRequestBlocking,标签]
}
background.js
此代码将所有URL请求阻止到 Facebook
并将其重定向至
var _redirectURL =;
//注册一个事件监听器
//在被触发之前跟踪所有的请求
chrome.webRequest.onBeforeRequest.addListener(function(details){
if(_redirectURL!= ){
return {
redirectUrl:http://www.google.co.in// *重定向网址* /
};
}
},{
urls:[*://www.facebook.com/*] / * URL列表* / * *
},[blocking]); //阻止截获的请求,直到此处理程序完成
chrome.webRequest.onHeadersReceived.addListener(function(details){
if(_redirectURL ==){
var secondaryURL = extractSecondaryURL );
_redirectUrl = secondaryURL;
chrome.tabs.reload();
}
},{
url:[http:// * / * ,https:// * / *]
},[blocking,responseHeaders]);
输出
I am attempting to create a Chrome extension that utilizes Chrome's webRequest module to perform a redirect to a URL obtained from an initially accessed URL. For this I would like to utilize only Chrome's webRequest functions (e.g., onBeforeSendHeaders, onHeadersReceived) and not simply perform a call to $.ajax() with the obtained URL. The desired functionality is:
- User enters a URL in the address bar
- A request is made and the secondary URL is extracted from the HTTP response
- The chrome.webRequest.onHeadersReceived handler redirects the user to this secondary URL using the redirectUrl attribute of a blocking response.
My attempt at accomplishing this is:
chrome.webRequest.onHeadersReceived.addListener(
function(details){
var secondaryURL = extractSecondaryURL(details);
return {redirectUrl: secondaryURL}; //this doesn't work
},
{urls:["http://*/*", "https://*/*"]},
["blocking","responseHeaders"]
);
...but the page is never forwarded. The webRequest documentation says, "Only used as a response to the onBeforeRequest event." about the redirectUrl attribute, which is the likely culprit.
How does one perform this sort of forwarding using data received from the response headers and the Chrome webRequest module?
web request API Methods
- onBeforeRequest allows cancelling or redirecting a request
- onBeforeSendHeaders, onHeadersReceived allows cancelling a request or modifying headers
- onAuthRequired allows providing\modifying authentication credentials.
So, idea is to store the secondary URL from onHeadersReceived Event and fire a chrome.tabs.reload()
event which fires onBeforeRequest event again which helps in redirecting.
Sample Demonstration
The following untested :) demonstration blocks all Facebook
URL's and redirects them to Google
upon receiving secondary URL, you can customize it further.
References
manifest.json
Ensure all permissions are available and register background page with extension.
{
"name": "Hanlder for Navigation",
"description": "http://stackoverflow.com/questions/16928912/url-forwarding-using-chrome-webrequest-after-response-is-received",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions":["https://www.facebook.com/*","webRequest","webRequestBlocking","tabs"]
}
background.js
This code blocks all URL request to Facebook
and redirects them to Google
.
var _redirectURL = "";
// Register an event listener which
//traces all requests before being fired
chrome.webRequest.onBeforeRequest.addListener(function (details) {
if (_redirectURL != "") {
return {
redirectUrl: "http://www.google.co.in/" /*Redirection URL*/
};
}
}, {
urls: ["*://www.facebook.com/*"] /* List of URL's */ * *
}, ["blocking"]); // Block intercepted requests until this handler has finished
chrome.webRequest.onHeadersReceived.addListener(function (details) {
if (_redirectURL == "") {
var secondaryURL = extractSecondaryURL(details);
_redirectUrl = secondaryURL;
chrome.tabs.reload();
}
}, {
urls: ["http://*/*", "https://*/*"]
}, ["blocking", "responseHeaders"]);
Output
All request(s) to Facebook
are redirected to Google
.
这篇关于收到响应后使用Chrome webRequest进行网址转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!