zoukankan      html  css  js  c++  java
  • chorme requestBody

    https://stackoverflow.com/questions/18534771/chrome-extension-how-to-get-http-response-body

    Chrome webrequest doesn't see POST data in requestBody

    I am having a problem getting the POST data from a Facebook action. When you submit a comment on FB it posts to this url: https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT In the request, the post data for the post exists: Here is an example of the actual comment from the post data:

    comment_text:test this is a test
    

    When I try to access this through a Chrome Extension I can't seem to get this data. I've tried parsing the requestBody, but it is empty. I then tried to see if any of the other requestmethods would work, and I can't seem to find the data anywhere.

    manifest.json:

    {
    "background": {
        "scripts": [ "background.js" ]
     },
    "manifest_version": 2,
    
    "name": "Interaction Tracker",
    "description": "Track social interactions by social site and customizable categories.",
    "version": "1.0",
    
    "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html"
    },
    "permissions": [ "webRequest", "webRequestBlocking", "webNavigation", "tabs", "<all_urls>", "debugger" ]
    }
    

    background.js

    chrome.webRequest.onBeforeRequest.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["blocking", "requestBody"]
        );
    
    chrome.webRequest.onBeforeSendHeaders.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["blocking", "requestHeaders"]
        );
    
    
    chrome.webRequest.onSendHeaders.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["requestHeaders"]
        );
    
    chrome.webRequest.onHeadersReceived.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["blocking", "responseHeaders"]
        );
    
    chrome.webRequest.onAuthRequired.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["blocking", "responseHeaders"]
        );
    
    chrome.webRequest.onResponseStarted.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["responseHeaders"]
        );
    
    chrome.webRequest.onBeforeRedirect.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["responseHeaders"]
        );
    
    chrome.webRequest.onCompleted.addListener(
      function(details) {
        if (details.method == "POST") {
            var fb_add_comment_regex = new RegExp(".*facebook.com/ufi/add/comment.*");
            if ( fb_add_comment_regex.test(details.url) ) {
                console.log(JSON.stringify(details));
                }
            }
      },
        {urls: ["<all_urls>"]},
        ["responseHeaders"]
        );
    

    Below is the output from the console.log statements for each type of request:

    onBeforeRequest

    {
      "frameId": 0,
      "method": "POST",
      "parentFrameId": -1,
      "requestBody": {
        "raw": [
          {
            "bytes": {
    
            }
          }
        ]
      },
      "requestId": "6724",
      "tabId": 93,
      "timeStamp": 1444749653166.1,
      "type": "xmlhttprequest",
      "url": "https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT"
    }
    

    onBeforeSendHeaders

    {
      "frameId": 0,
      "method": "POST",
      "parentFrameId": -1,
      "requestHeaders": [
        {
          "name": "Origin",
          "value": "https://www.facebook.com"
        },
        {
          "name": "X-DevTools-Emulate-Network-Conditions-Client-Id",
          "value": "AB63796C-002A-4670-8A56-547F8D13CA8C"
        },
        {
          "name": "User-Agent",
          "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
        },
        {
          "name": "Content-Type",
          "value": "application/x-www-form-urlencoded"
        },
        {
          "name": "Accept",
          "value": "*/*"
        },
        {
          "name": "Referer",
          "value": "https://www.facebook.com/GrauZug?ref=br_rs"
        },
        {
          "name": "Accept-Encoding",
          "value": "gzip, deflate"
        },
        {
          "name": "Accept-Language",
          "value": "en-US,en;q=0.8"
        },
        {
          "name": "Cookie",
          "value": "datr=pvA2VDnTeMjGlWhVYRrnhBtO; lu=gh2TS-IuZkO-Ku-YhAzkiFIw; p=-2; c_user=100000720140344; fr=07LqqXcCamvBIa9Ww.AWU6e_qoHRglPj51gS-CF6uF-r8.BVY1Qk.DT.FYB.0.AWVvgvJA; xs=79%3A403i2b7V6bYSIA%3A2%3A1439311770%3A8344; csm=2; s=Aa4bsJIf94u-JaGr.BVyieb; presence=EDvF3EtimeF1444749507EuserFA21B00720140344A2EstateFDsb2F1444656454966Et2F_5b_5dElm2FnullEuct2F1444743344BEtrFnullEtwF2707054892EatF1444748467285G444749507665CEchFDp_5f1B00720140344F101CC; act=1444749649564%2F26"
        }
      ],
      "requestId": "6724",
      "tabId": 93,
      "timeStamp": 1444749653173.2,
      "type": "xmlhttprequest",
      "url": "https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT"
    }
    

    onSendHeaders

    {
      "frameId": 0,
      "method": "POST",
      "parentFrameId": -1,
      "requestHeaders": [
        {
          "name": "Origin",
          "value": "https://www.facebook.com"
        },
        {
          "name": "X-DevTools-Emulate-Network-Conditions-Client-Id",
          "value": "AB63796C-002A-4670-8A56-547F8D13CA8C"
        },
        {
          "name": "User-Agent",
          "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
        },
        {
          "name": "Content-Type",
          "value": "application/x-www-form-urlencoded"
        },
        {
          "name": "Accept",
          "value": "*/*"
        },
        {
          "name": "Referer",
          "value": "https://www.facebook.com/GrauZug?ref=br_rs"
        },
        {
          "name": "Accept-Encoding",
          "value": "gzip, deflate"
        },
        {
          "name": "Accept-Language",
          "value": "en-US,en;q=0.8"
        },
        {
          "name": "Cookie",
          "value": "datr=pvA2VDnTeMjGlWhVYRrnhBtO; lu=gh2TS-IuZkO-Ku-YhAzkiFIw; p=-2; c_user=100000720140344; fr=07LqqXcCamvBIa9Ww.AWU6e_qoHRglPj51gS-CF6uF-r8.BVY1Qk.DT.FYB.0.AWVvgvJA; xs=79%3A403i2b7V6bYSIA%3A2%3A1439311770%3A8344; csm=2; s=Aa4bsJIf94u-JaGr.BVyieb; presence=EDvF3EtimeF1444749507EuserFA21B00720140344A2EstateFDsb2F1444656454966Et2F_5b_5dElm2FnullEuct2F1444743344BEtrFnullEtwF2707054892EatF1444748467285G444749507665CEchFDp_5f1B00720140344F101CC; act=1444749649564%2F26"
        }
      ],
      "requestId": "6724",
      "tabId": 93,
      "timeStamp": 1444749653175.2,
      "type": "xmlhttprequest",
      "url": "https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT"
    }
    

    onHeadersReceived

    {
      "frameId": 0,
      "method": "POST",
      "parentFrameId": -1,
      "requestId": "6724",
      "responseHeaders": [
        {
          "name": "status",
          "value": "200"
        },
        {
          "name": "cache-control",
          "value": "private, no-cache, no-store, must-revalidate"
        },
        {
          "name": "content-encoding",
          "value": "gzip"
        },
        {
          "name": "content-security-policy",
          "value": "default-src * data: blob:;script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl;style-src * 'unsafe-inline';connect-src *.facebook.com *.fbcdn.net *.facebook.net *.spotilocal.com:* *.akamaihd.net wss://*.facebook.com:* https://fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com 127.0.0.1:*;"
        },
        {
          "name": "content-type",
          "value": "application/x-javascript; charset=utf-8"
        },
        {
          "name": "date",
          "value": "Tue, 13 Oct 2015 15:20:55 GMT"
        },
        {
          "name": "expires",
          "value": "Sat, 01 Jan 2000 00:00:00 GMT"
        },
        {
          "name": "pragma",
          "value": "no-cache"
        },
        {
          "name": "public-key-pins-report-only",
          "value": "max-age=500; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E="; pin-sha256="q4PO2G2cbkZhZ82+JgmRUyGMoAeozA+BSXVXQWB8XWQ="; report-uri="http://reports.fb.com/hpkp/""
        },
        {
          "name": "strict-transport-security",
          "value": "max-age=15552000; preload"
        },
        {
          "name": "vary",
          "value": "Accept-Encoding"
        },
        {
          "name": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "name": "x-fb-debug",
          "value": "fd7Bt/uIX2rLmngndhprmXlX3mTkZboQqcPSaw9kQt0aQUEfX3ikBMT1016i1c2RPbts9Jhbb0+bVGWPqk/j7Q=="
        },
        {
          "name": "x-frame-options",
          "value": "DENY"
        },
        {
          "name": "x-xss-protection",
          "value": "0"
        }
      ],
      "statusCode": 200,
      "statusLine": "HTTP/1.1 200 OK",
      "tabId": 93,
      "timeStamp": 1444749655679.6,
      "type": "xmlhttprequest",
      "url": "https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT"
    }
    

    onResponseStarted

    {
      "frameId": 0,
      "fromCache": false,
      "ip": "31.13.93.3",
      "method": "POST",
      "parentFrameId": -1,
      "requestId": "6724",
      "responseHeaders": [
        {
          "name": "status",
          "value": "200"
        },
        {
          "name": "cache-control",
          "value": "private, no-cache, no-store, must-revalidate"
        },
        {
          "name": "content-encoding",
          "value": "gzip"
        },
        {
          "name": "content-security-policy",
          "value": "default-src * data: blob:;script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl;style-src * 'unsafe-inline';connect-src *.facebook.com *.fbcdn.net *.facebook.net *.spotilocal.com:* *.akamaihd.net wss://*.facebook.com:* https://fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com 127.0.0.1:*;"
        },
        {
          "name": "content-type",
          "value": "application/x-javascript; charset=utf-8"
        },
        {
          "name": "date",
          "value": "Tue, 13 Oct 2015 15:20:55 GMT"
        },
        {
          "name": "expires",
          "value": "Sat, 01 Jan 2000 00:00:00 GMT"
        },
        {
          "name": "pragma",
          "value": "no-cache"
        },
        {
          "name": "public-key-pins-report-only",
          "value": "max-age=500; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E="; pin-sha256="q4PO2G2cbkZhZ82+JgmRUyGMoAeozA+BSXVXQWB8XWQ="; report-uri="http://reports.fb.com/hpkp/""
        },
        {
          "name": "strict-transport-security",
          "value": "max-age=15552000; preload"
        },
        {
          "name": "vary",
          "value": "Accept-Encoding"
        },
        {
          "name": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "name": "x-fb-debug",
          "value": "fd7Bt/uIX2rLmngndhprmXlX3mTkZboQqcPSaw9kQt0aQUEfX3ikBMT1016i1c2RPbts9Jhbb0+bVGWPqk/j7Q=="
        },
        {
          "name": "x-frame-options",
          "value": "DENY"
        },
        {
          "name": "x-xss-protection",
          "value": "0"
        }
      ],
      "statusCode": 200,
      "statusLine": "HTTP/1.1 200 OK",
      "tabId": 93,
      "timeStamp": 1444749655683.3,
      "type": "xmlhttprequest",
      "url": "https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT"
    }
    

    onCompleted

    {
      "frameId": 0,
      "fromCache": false,
      "ip": "31.13.93.3",
      "method": "POST",
      "parentFrameId": -1,
      "requestId": "6724",
      "responseHeaders": [
        {
          "name": "status",
          "value": "200"
        },
        {
          "name": "cache-control",
          "value": "private, no-cache, no-store, must-revalidate"
        },
        {
          "name": "content-encoding",
          "value": "gzip"
        },
        {
          "name": "content-security-policy",
          "value": "default-src * data: blob:;script-src *.facebook.com *.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net *.google.com 127.0.0.1:* *.spotilocal.com:* 'unsafe-inline' 'unsafe-eval' *.akamaihd.net *.atlassolutions.com blob: chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl;style-src * 'unsafe-inline';connect-src *.facebook.com *.fbcdn.net *.facebook.net *.spotilocal.com:* *.akamaihd.net wss://*.facebook.com:* https://fb.scanandcleanlocal.com:* *.atlassolutions.com attachment.fbsbx.com 127.0.0.1:*;"
        },
        {
          "name": "content-type",
          "value": "application/x-javascript; charset=utf-8"
        },
        {
          "name": "date",
          "value": "Tue, 13 Oct 2015 15:20:55 GMT"
        },
        {
          "name": "expires",
          "value": "Sat, 01 Jan 2000 00:00:00 GMT"
        },
        {
          "name": "pragma",
          "value": "no-cache"
        },
        {
          "name": "public-key-pins-report-only",
          "value": "max-age=500; pin-sha256="WoiWRyIOVNa9ihaBciRSC7XHjliYS9VwUGOIud4PB18="; pin-sha256="r/mIkG3eEpVdm+u/ko/cwxzOMo1bk4TyHIlByibiA5E="; pin-sha256="q4PO2G2cbkZhZ82+JgmRUyGMoAeozA+BSXVXQWB8XWQ="; report-uri="http://reports.fb.com/hpkp/""
        },
        {
          "name": "strict-transport-security",
          "value": "max-age=15552000; preload"
        },
        {
          "name": "vary",
          "value": "Accept-Encoding"
        },
        {
          "name": "x-content-type-options",
          "value": "nosniff"
        },
        {
          "name": "x-fb-debug",
          "value": "fd7Bt/uIX2rLmngndhprmXlX3mTkZboQqcPSaw9kQt0aQUEfX3ikBMT1016i1c2RPbts9Jhbb0+bVGWPqk/j7Q=="
        },
        {
          "name": "x-frame-options",
          "value": "DENY"
        },
        {
          "name": "x-xss-protection",
          "value": "0"
        }
      ],
      "statusCode": 200,
      "statusLine": "HTTP/1.1 200 OK",
      "tabId": 93,
      "timeStamp": 1444749655684.2,
      "type": "xmlhttprequest",
      "url": "https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT"
    }
    

    Any other listener not listed above didn't have output.

    Anyone have any idea here?

    shareimprove this question
     
        
    Is requestBody really empty, i.e. are all keys missing...? – Rob W Oct 13 '15 at 16:11
        
    "I've tried parsing the requestBody, but it is empty". Where's your code for that? How do you know it's empty? – rsanchez Oct 13 '15 at 16:12
    1  
    I'm not sure if it is empty or if they keys are missing. All I do know is that I am dumping the entire details, and this is what I get: "requestBody": { "raw": [ { "bytes": { } } ] } – Artagel Oct 13 '15 at 17:35 

    Your console.log shows that requestBody is not empty.

    You'll just have to parse the raw bytes:

    If the request method is PUT or POST, and the body is not already parsed in formData, then the unparsed request body elements are contained in this array.

    For example if the posted data is a string then you can get its value in onBeforeRequest listener:

    var postedString = decodeURIComponent(String.fromCharCode.apply(null,
                                          new Uint8Array(details.requestBody.raw[0].bytes)));
    

    There are many methods to decode the string depending on the encoding used by the site script.

    shareimprove this answer
     
        
    Thanks. This is the correct answer. I didn't realize that there was data in the raw area. I had tried to print that data to the console but didn't realize the method. – Artagel Oct 13 '15 at 17:40
        
    FYI, you seem to be missing a closing parenthesis at the end, +1 though – DelightedD0D Apr 7 at 10:42
        
    Fixed, thank you. – wOxxOm Apr 7 at 10:52
  • 相关阅读:
    linux----安装jdk
    docker安装gitlab
    Centos7 安装docker
    容易混淆的字符
    nodejs 简单从淘宝的有好货根据关键词获取列表以及详情
    bash设置终端提示符
    C# NuGet打包和离线安装
    Shell 遍历文件夹中所有文件并进行相关操作
    css中“~”和“>”
    双击编辑表格字段
  • 原文地址:https://www.cnblogs.com/developer-ios/p/7348821.html
Copyright © 2011-2022 走看看