javascript 收到响应后使用 Chrome webRequest 进行 URL 转发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16928912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:29:31  来源:igfitidea点击:

URL forwarding using Chrome webRequest after response is received

javascriptgoogle-chromegoogle-chrome-extensionwebrequest

提问by Mat Kelly

I am attempting to create a Chrome extension that utilizes Chrome's webRequestmodule 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:

我正在尝试创建一个 Chrome 扩展程序,该扩展程序利用 Chrome 的webRequest模块执行重定向到从最初访问的 URL 获得的 URL。为此,我只想使用 Chrome 的 webRequest 函数(例如,onBeforeSendHeaders、onHeadersReceived),而不是简单地使用获得的 URL 调用 $.ajax()。所需的功能是:

  1. User enters a URL in the address bar
  2. A request is made and the secondary URL is extracted from the HTTP response
  3. The chrome.webRequest.onHeadersReceivedhandler redirects the user to this secondary URL using the redirectUrl attribute of a blocking response.
  1. 用户在地址栏中输入 URL
  2. 发出请求并从 HTTP 响应中提取辅助 URL
  3. 所述chrome.webRequest.onHeadersReceived处理程序将用户重定向到使用阻挡响应的redirectUrl属性该二次URL。

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 redirectUrlattribute, which is the likely culprit.

...但页面永远不会转发。webRequest 文档说,“仅用作对 onBeforeRequest 事件的响应。” 关于redirectUrl属性,这可能是罪魁祸首。

How does one perform this sort of forwarding using data received from the response headers and the Chrome webRequest module?

如何使用从响应头和 Chrome webRequest 模块接收到的数据执行这种转发?

回答by Sudarshan

web request APIMethods

Web 请求 API方法

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.

因此,想法是存储来自 onHeadersReceived 事件的辅助 URL 并触发一个 chrome.tabs.reload()事件,该事件再次触发 onBeforeRequest 事件,这有助于重定向。

Sample Demonstration

示例演示

The following untested :) demonstration blocks all FacebookURL's and redirects them to Googleupon receiving secondary URL, you can customize it further.

以下未经测试:) 演示阻止所有FacebookURL 并Google在收到辅助 URL 后将它们重定向到,您可以进一步自定义它。

References

参考

manifest.json

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

background.js

This code blocks all URL request to Facebookand redirects them to Google.

此代码阻止所有 URL 请求Facebook并将它们重定向到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

Output

All request(s) to Facebookare redirected to Google.

所有请求Facebook都被重定向到Google