chrome 插件 页面请求转发_收到响应后使用Chrome webRequest进行网址转发

本文介绍如何创建一个Chrome扩展,利用webRequest模块根据初始请求的响应提取二级URL并进行重定向。详细描述了在onHeadersReceived事件中尝试使用redirectUrl属性进行重定向的问题,并提出了解决方案,即在onHeadersReceived事件中存储二级URL,然后通过chrome.tabs.reload()触发onBeforeRequest事件来实现重定向。
摘要由CSDN通过智能技术生成

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?

解决方案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.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值