4

「ArrayBuffer」を JSON のような「読み取り可能な」形式に変換することは可能ですか?

テスト スクリプト:

<script>
try {
   http = new ActiveXObject("Microsoft.XMLHTTP");   // Trying IE
}
catch(e)    // Failed, use standard object 
{
  http = new XMLHttpRequest(); 
}

var url = "http://localhost/test.htm";
var params = "param=abc&param2=62";
http.open("POST", url, true);

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert('send..');
    }
}
http.send(params);
</script>

Background (chrome 拡張機能) Request LISTENER: (test.htm から xhr を受信)

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        console.log(details); 
},
{
urls: ["*://localhost/*"]
},
['requestBody']);

console.log の結果:

Object {frameId: 0, method: "POST", parentFrameId: -1, requestBody: Object, requestId: "12981"…}
frameId: 0
method: "POST"
parentFrameId: -1
requestBody: Object
raw: Array[1]
0: Object
bytes: ArrayBuffer
byteLength: 32
__proto__: ArrayBuffer
constructor: function ArrayBuffer() { [native code] }
slice: function slice() { [native code] }
__proto__: Object
__proto__: Object
length: 1
__proto__: Array[0]
__proto__: Object
requestId: "12981"
tabId: 180
timeStamp: 1367604574726.125
type: "xmlhttprequest"
url: "http://localhost/test.htm"
__proto__: Object

details.requestBody.raw を param=abc¶m2=62 または JSON に戻す必要があります。ありがとうございました

http://developer.chrome.com/dev/extensions/webRequest.html

4

1 に答える 1

2

私はそのライブラリを使用します - https://github.com/vicetjs/array-buffer-to-data

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        try {
            if (details && details.type === "xmlhttprequest" && 
                var buffer = details.requestBody.raw[0].bytes;
                console.log(arrayBufferToData.toJSON(buffer));//JSON payload body
            }
            return {requestHeaders: details.requestHeaders};
        } catch(e) {
            console.log(e.stack);
        } 
    },
    {urls: ["<all_urls>"]},
    ["requestBody"]);
于 2016-06-02T20:15:44.373 に答える