Chrome webrequest在requestBody中看不到POST数据

时间:2015-10-13 15:29:11

标签: javascript google-chrome-extension

我在从Facebook操作中获取POST数据时遇到问题。当您在FB上提交评论时,它会发布到此网址:https://www.facebook.com/ufi/add/comment/?__pc=EXP1%3ADEFAULT 在请求中,帖子的帖子数据存在: 以下是帖子数据中实际评论的示例:

comment_text:test this is a test

当我尝试通过Chrome扩展程序访问此内容时,我似乎无法获取此数据。我试过解析requestBody,但它是空的。然后我试着看看是否有其他请求方法可以工作,我似乎无法在任何地方找到数据。

的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"]
    );

以下是每种请求的console.log语句的输出:

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"
}

上面未列出的任何其他听众都没有输出。

有人有任何想法吗?

2 个答案:

答案 0 :(得分:12)

您的console.log显示requestBody 为空。

您只需要解析raw个字节:

  

如果请求方法是PUT或POST,并且尚未在formData中解析正文,则未解析的请求正文元素包含在此数组中。

例如,如果发布的数据是字符串,那么您可以在onBeforeRequest listener中获取其值:

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

根据站点脚本使用的编码,有许多方法可以解码字符串。

答案 1 :(得分:0)

之所以可能不存在请求正文的另一个原因是因为extraInfoSpec必须按如下所示包含“ requestBody”。

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