Joh*_*985 5 google-chrome google-chrome-extension
是否可以将“ArrayBuffer”转换为某种“可读”格式,例如 JSON?
\n\n测试脚本:
\n\n<script>\ntry {\n http = new ActiveXObject("Microsoft.XMLHTTP"); // Trying IE\n}\ncatch(e) // Failed, use standard object \n{\n http = new XMLHttpRequest(); \n}\n\nvar url = "http://localhost/test.htm";\nvar params = "param=abc¶m2=62";\nhttp.open("POST", url, true);\n\nhttp.onreadystatechange = function() {//Call a function when the state changes.\n if(http.readyState == 4 && http.status == 200) {\n alert(\'send..\');\n }\n}\nhttp.send(params);\n</script>\nRun Code Online (Sandbox Code Playgroud)\n\n在后台(chrome 扩展)请求监听器: \n (从 test.htm 接收 xhr)
\n\nchrome.webRequest.onBeforeRequest.addListener(\n function(details) {\n console.log(details); \n},\n{\nurls: ["*://localhost/*"]\n},\n[\'requestBody\']);\nRun Code Online (Sandbox Code Playgroud)\n\n控制台.日志结果:
\n\nObject {frameId: 0, method: "POST", parentFrameId: -1, requestBody: Object, requestId: "12981"\xe2\x80\xa6}\nframeId: 0\nmethod: "POST"\nparentFrameId: -1\nrequestBody: Object\nraw: Array[1]\n0: Object\nbytes: ArrayBuffer\nbyteLength: 32\n__proto__: ArrayBuffer\nconstructor: function ArrayBuffer() { [native code] }\nslice: function slice() { [native code] }\n__proto__: Object\n__proto__: Object\nlength: 1\n__proto__: Array[0]\n__proto__: Object\nrequestId: "12981"\ntabId: 180\ntimeStamp: 1367604574726.125\ntype: "xmlhttprequest"\nurl: "http://localhost/test.htm"\n__proto__: Object\nRun Code Online (Sandbox Code Playgroud)\n\n我需要将details.requestBody.raw转换回param=abc¶m2=62或JSON。\n谢谢
\n\n\n我使用该库 - 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"]);
Run Code Online (Sandbox Code Playgroud)