An interpretation of the WebRequest module of Chrome Extension

Source: Internet
Author: User

Documentation here: http://developer.chrome.com/trunk/extensions/webRequest.html

1, in order to use WebRequest, you first need to add a similar content to the configuration file Manifest.json:

{    "name": "My extension",    ...    " Permissions ": {        " webRequest "," *://*.google.com "    },    ....}

This means that only allow this extension to *.google.com domain name use webrequest, if the string is replaced by other formats, such as *://*, you can support all Web site access.

The core of 2,webrequest is to forge a variety of request, so it's not just about writing an object's data, but choosing the right time to forge it before sending some kind of request, or intercept it halfway after the real request arrives, Replace it with fake and then send it out again. Life cycle of request is a description of the matter.

Onbeforesendheaders This callback is useful, as described in the document "This event will allow extensions to add, modify, or delete the request headers. The simple usage is as follows:

Chrome.webRequest.onBeforeRequest.addListener (    callback, filter, OPT_EXTRAINFOSPEC);

When A,callback is called, it is given a parameter that contains the request information.

The B,filter parameter is an object that has these keys available:

URLs: Strings Similar to this format: *://www.google.co/foo*bar

Types: A type like main_frame or Sub_frame,image

identifier of the Tabid:tab

identifier of the Windowid:window

3, because there may be multiple extension to play WebRequest, so a set of conflict handling mechanism is required. If a new request is set, whether the settings continue to work after the page is refreshed, and when they are set, are all questions about the cache. The other is the problem of using the timestamp attribute, where the attribute values obtained from timestamp can be compared to each other, but if and new Date (). GetTime () The comparison of the values obtained in this way is not necessarily true and needs to be noted.

4, example

A, block all request to www.evile.com

Chrome.webRequest.onBeforeRequest.addListener (  function (details) {    return {cancel:details.url.indexOf (" ://www.evil.com/")! =-1};  },  {urls: [" <all_urls> "]},  [" blocking "]);

Another way to use filter:

Chrome.webRequest.onBeforeRequest.addListener (  function (details) {return {cancel:true};},  {urls: ["*:// www.evil.com/* "]},  [" blocking "])

The filter has been cancel out.

b, remove the header of user-agent from all request

Chrome.webRequest.onBeforeSendHeaders.addListener (  function (details) {for    (var i = 0; i < Details.requestHeaders.length; ++i) {      if (details.requestheaders[i].name = = = ' User-agent ') {        details.requestHeaders.splice (i, 1);        break;      }    }    return {requestHeaders:details.requestHeaders};  },  {urls: ["<all_urls>"]},  ["Blocking", " Requestheaders "]);

5, various API documentation

A,requestfilter:

Requestfilter = {    Tabid:interger,    An array of//optional//url, or a pattern urls:array_of_string that matches a URL    ,    // The optional values are: "Main_frame", "Sub_frame", "stylesheet", "script", "image", "Object", "XMLHttpRequest", "other"    Types:array _of_enumerated_string,//optional    windowid:integer//optional};// The blocking keyword is set to use this object as the rule of block Bolockingresponse = {    //is true if request is cancel, use it in Onbeforerequest!    Cancel:boolean,//optional    //Only used in Onbeforerequest event, used to swap the key properties!!!    redirecturl:string,//option    //Only in the Onheadersreceived event, the header is switched responseheaders when the browser returns to the server    : Httpheaders//optional    //Only used in onbeforesendheaders events. is another key attribute used to swap!!!    requestheaders:httpheaders//optional    //Only used in onauthrequred events, of course, is also used to swap the    authcredentials:object// Optional}; An array of httpheaders:http headers, with each element having its own key-value pair, is an object. This has to be actually printed out before we know it.

B,onbeforesendheaders the event that is called after the TCP connection is established and before the HTTP data is sent. Call Method:

Chrome.webRequest.onBeforeSendHeaders.addListener (function (object details) {...});

The details structure of the parameters passed to callback are as follows:

Details = {    Tabid:integer,//If not associated with tab return-1    parentframeid:integer,    url:string,    timestamp:double,    //0 means that request is    Frameid:integer,    requestid:string,    requestheaders:httpheaders,//in the main frame. Optional    type:enumerated_string,//value in:  ["Main_frame", "Sub_frame", "stylesheet", "script", "image", " Object "," XMLHttpRequest "," other "]    method:string//Standard HTTP method};

Other events are similar, and the parameters passed in when calling callback are the details structure, except that some fields have different values.

6, Summary: WebRequest API is very easy to use, basically is the four part of the three examples of style. Of course, if you want to use it better, you need some basic knowledge of HTTP request, and if you want to forge it, you should know what it's supposed to be.

Ext.: http://www.zengguanglik.com.cn/2014/1/20/00080.html

The WebRequest module of Chrome Extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.