上传的文件内容在chrome.webRequest中不可用

时间:2016-11-06 12:57:49

标签: google-chrome google-chrome-extension

在Chrome扩展程序中,我通过检查requestBody.raw内的chrome.webRequest.onBeforeRequest来尝试获取每个上传文件的数据。但是,通过测试文件上传到各个站点,似乎文件内容不存在。例如,在将PDF文件上传到Gmail时,我的扩展程序会在POST请求中被调用3次,但在所有这些请求中,requestBody.raw数组大小为0.在其他网站中,有时requestBody.raw已填充,但不是上传文件的内容。

编辑:以下是完整的脚本。我试图检查所有可能的数据:

的manifest.json:

 {
     "name": "Example",
     "version": "1.0",
     "manifest_version": 2,
     "description": "Example",
     "permissions": ["webRequest","webRequestBlocking","nativeMessaging","*://*/*"],
     "background": {"scripts": ["background.js"]}
 }

background.js:

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
        if(details.method != "GET"){
            console.log(JSON.stringify(details));

            if(details.requestBody && details.requestBody.raw){
                for(var i = 0; i < details.requestBody.raw.length; ++i){
                    if(details.requestBody.raw[i].file){
                        console.log("FILE");
                        console.log(details.requestBody.raw[i].file);
                    }
                    if(details.requestBody.raw[i].bytes){
                        console.log("RAW");
                        console.log(details.requestBody.raw[i].bytes.byteLength);
                        var dv = new DataView(details.requestBody.raw[i].bytes);
                        var result = "";
                        for(var j = 0; j < dv.byteLength; ++j){
                            result += (String.fromCharCode( dv.getInt8(j) ));
                        }
                        console.log(result);
                    }
                }
            }    
        }
        return {cancel: false};
    },
    {urls: ["<all_urls>"]},
    ["blocking", "requestBody"]
); 

这是使用文本&#34; hello&#34;上传test.txt时的输出。到dropbox:

background.js:5 {"frameId":0,"method":"POST","parentFrameId":-1,"requestBody":{"formData":{"_subject_uid":["144107656"],"fq_dir":[""],"is_xhr":["true"],"ns_map":["231317524_1599"],"show_deleted":["false"],"t":["TUN_gKxY2VWUVyTKF6-L551T"]}},"requestId":"53563","tabId":1207,"timeStamp":1478512776308.165,"type":"xmlhttprequest","url":"https://www.dropbox.com/update/list_dir"}
background.js:5 {"frameId":0,"method":"POST","parentFrameId":-1,"requestBody":{"formData":{"_subject_uid":["144107656"],"fq_path_prefix":["/"],"include_inherited":["false"],"is_xhr":["true"],"max_results":["5"],"sort_by_name":["false"],"t":["TUN_gKxY2VWUVyTKF6-L551T"]}},"requestId":"53564","tabId":1207,"timeStamp":1478512776682.595,"type":"xmlhttprequest","url":"https://www.dropbox.com/share_ajax/shared_with"}
background.js:5 {"frameId":0,"method":"POST","parentFrameId":-1,"requestBody":{"formData":{"_subject_uid":["144107656"],"is_xhr":["true"],"paths":["[\"/test.txt\"]"],"t":["TUN_gKxY2VWUVyTKF6-L551T"]}},"requestId":"53565","tabId":1207,"timeStamp":1478512776685.152,"type":"xmlhttprequest","url":"https://www.dropbox.com/sm/get_token_info"}
background.js:5 {"frameId":0,"method":"POST","parentFrameId":-1,"requestBody":{"raw":[{"bytes":{}}]},"requestId":"53566","tabId":1207,"timeStamp":1478512776688.215,"type":"xmlhttprequest","url":"https://www.dropbox.com/2/sharing/list_file_members/batch"}
background.js:14 RAW
background.js:15 49
background.js:21 {"files":["id:eIK7fjIt9xgAAAAAAAAAdw"],"limit":5}
background.js:5 {"frameId":0,"method":"POST","parentFrameId":-1,"requestBody":{"formData":{"_subject_uid":["144107656"],"activity_context":["3"],"activity_context_data_batch":["[\"/test.txt\"]"],"is_xhr":["true"],"t":["TUN_gKxY2VWUVyTKF6-L551T"]}},"requestId":"53567","tabId":1207,"timeStamp":1478512776690.839,"type":"xmlhttprequest","url":"https://www.dropbox.com/file_activity/comment_status_batch"} 

0 个答案:

没有答案