ajaxのhttp headerを書き換える。webrequest API | chrome extension

普通にxhrを弄る場合は

http://www.w3.org/TR/XMLHttpRequest/

http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method

mimetypeなどは

beforeSend: function(xhr){
xhr.overrideMimeType(“text/plain;charset=Shift_JIS”); // A
xhr.setRequestHeader(“Content-type”,”text/plain;charset=Shift_JIS”); // B
},

はAかBで更新できたのですが、この辺はダメで

Accept-Charset
Accept-Encoding
Access-Control-Request-Headers
Access-Control-Request-Method
Connection
Content-Length
Cookie
Cookie2
Content-Transfer-Encoding
Date
Expect
Host
Keep-Alive
Origin
Referer
TE
Trailer
Transfer-Encoding
Upgrade
User-Agent
Via

さてどうしようかと webRequest API の登場です。

http://code.google.com/chrome/extensions/trunk/webRequest.html#event-onBeforeSendHeaders

* onBeforeSendHeaders (optionally synchronous)
Fires when a request is about to occur and the initial headers have been prepared. The event is intended to allow extensions to add, modify, and delete request headers (*). The onBeforeSendHeaders event is passed to all subscribers, so different subscribers may attempt to modify the request; see the Implementation details section for how this is handled. This event can be used to cancel the request.
* onSendHeaders
Fires after all extensions have had a chance to modify the request headers, and presents the final (*) version. The event is triggered before the headers are sent to the network. This event is informational and handled asynchronously. It does not allow modifying or cancelling the request.

この2つは、header書き換えできるらしい?

chrome.webRequest.onBeforeSendHeaders.addListener(function(object details) {…}, RequestFilter filter, array of string extraInfoSpec);

manifest.json permissionsにwebRequestを追加

  "permissions": [
    "tabs",
    "webRequest",
    "http://*/*",
chrome.webRequest.onBeforeSendHeaders.addListener(
    function(details) {
      // console.log( details.requestHeaders );
      for (var i = 0; i < details.requestHeaders.length; ++i) {
        if (details.requestHeaders[i].name === 'User-Agent') {
         details.requestHeaders[i].value = 'MY REQUEST';
       }
    }
    return {requestHeaders: details.requestHeaders};
    },
    {urls: ["http://*.bbtune.com/*"],types:["xmlhttprequest"]},
    ["requestHeaders"]
    );

eventは発生していますが書き変わらず?

onBeforeSendHeadersで書き換えた後に onSendHeadersで取得できるdetailsは

のようにちゃんとmy requestに変わってるのに・・・サーバーに送られるヘッダーが変わってない・・・

xmlhttprequestのときは、変更出来ないっぽいです。
裏にtabを開いてみたりして、ajaxじゃない方法でデータを取得してみたりします。

This entry was posted on 金曜日, 4月 13th, 2012 at 1:23 PM and is filed under Chrome Extention, Javascript. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Unityによるゲームを公開しております iOS/Android
https://itunes.apple.com/jp/app/lost-knight-3d-action/id900917032
https://play.google.com/store/apps/details?id=com.groundroad.runknight

各種開発支援・機能開発等小さいお仕事などもお請けしております。
unity開発支援, PHPシステム開発, javascript/html5 フロント開発, titanium mobileアプリ開発
お気軽にお問い合わせください
大崎・五反田近郊での対面でのお打ち合わせはいつでも可能です。