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
上帝王牌,Web Navigation Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this. The following commands need to be supported: BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored. FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored. VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied. QUIT: Quit the browser. Assume that the browser initially loads the web page at the URL http://www.acm.org/ Input Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command. Output For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command. Sample Input VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD QUIT Sample Output http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ Ignored http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ Ignored
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值