webRequest和webNavigation

 chrome有2套可以拦截URL的API:

webRequest和webNavigation


试验中:
http://developer.chrome.com/extensions/declarativeWebRequest.html
据说速度更快,而且可以 RedirectToEmptyDocument [但是在试验阶段]

两者关系:

There is no defined ordering between events of the webRequest API and the events of the webNavigation API. It is possible that webRequest events are still received for frames that already started a new navigation, or that a navigation only proceeds after the network resources are already fully loaded.

In general, the webNavigation events are closely related to the navigation state that is displayed in the UI, while the webRequest events correspond to the state of the network stack which is generally opaque to the user.


webNavigation只会发出事件。不能拦截。

webRequest可以拦截请求并改变内容。
http://developer.chrome.com/extensions/webRequest.html#type-RequestFilter

BlockingResponse

object )
Returns value for event handlers that have the 'blocking' extraInfoSpec applied. Allows the event handler to modify network requests.

用cancel方式拦截会导致页面无法继续
chrome.webRequest.onBeforeRequest.addListener(
  function(info) {
    console.log("onBeforeRequest:"+info.type );
   return {cancel:true};
  },
  // filters
  {
    urls: [
      "<all_urls>"
    ],
    types: ["main_frame"]
 },
  // extraInfoSpec
  ["blocking"]
  );

我写了一个有输出的content script. 从console中看到content script在这种情况下也没有被调用

If the optional  opt_extraInfoSpec  array contains the string  'blocking'  (only allowed for specific events), the callback function is handled synchronously. That means that the request is blocked until the callback function returns. In this case, the callback can return a BlockingResponse  that determines the further life cycle of the request. Depending on the context, this response allows cancelling or redirecting a request ( onBeforeRequest ), cancelling a request or modifying headers ( onBeforeSendHeaders onHeadersReceived ), or providing authentication credentials ( onAuthRequired ).



所以能够cancel请求的有2处:onBeforeRequest和onBeforeSendHeaders
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值