Monday, January 28, 2013

chrome.webRequest API

chrome.webRequest API

원본 : https://developer.chrome.com/extensions/webRequest.html

사용법이 애매하다. 문서를 봐도 모르겠다. 차라리 예제가 빠르다

1. 아래와 같이 적절히 manifest.json파일을 설정한다


"permissions": ["webRequest", "webRequestBlocking",
                  "http://*.limsungguk.com/"],
  "background": {
    "scripts": ["loldogs.js", "background.js"]
  },


2. background.js 에 이벤트 등록

chrome.webRequest.onBeforeRequest.addListener(foo,filter,extra);
처럼 등록을 한다.

여기서 filter는 문서상 RequestFilter라고 나오는 부분이다.

onBeforeRequest 뿐만아니라 onBeforeSendHeaders , onSendHeaders, onHeadersReceived 등등 원하는 이벤트를 등록해준다.

콜백으로 오는 함수가 리턴하는 값을 설정해주는 값에 따라 적절한 기능을 하는데

아래와 같은 것들 BlockingResponse 할수있다.. network request를 수정가능하게 해준다는 의미다.. 어떤 이벤트에 어떤 값을 리턴할수있는지 확인하자..



BlockingResponse

cancel optional boolean )
If true, the request is cancelled. Used in onBeforeRequest, this prevents the request from being sent.
redirectUrl optional string )
Only used as a response to the onBeforeRequest event. If set, the original request is prevented from being sent and is instead redirected to the given URL.
requestHeaders optional HttpHeaders )
Only used as a response to the onBeforeSendHeaders event. If set, the request is made with these request headers instead.
responseHeaders optional HttpHeaders )
Only used as a response to the onHeadersReceived event. If set, the server is assumed to have responded with these response headers instead. Only return responseHeaders if you really want to modify the headers in order to limit the number of conflicts (only one extension may modifyresponseHeaders for each request).
authCredentials optional object )
Only used as a response to the onAuthRequired event. If set, the request is made using the supplied credentials.
username string )
password string )















No comments:

Post a Comment