简体   繁体   English

Chrome扩展程序:使用chrome.webRequest修改与cURL请求的`--data` arg等效的HTTP请求

[英]Chrome Extensions: use chrome.webRequest to modify HTTP request equivalent of the `--data` arg of a cURL request

Diving into my first chrome extension, and trying to figure out how to modify some data in http requests. 深入研究我的第一个chrome扩展,并尝试找出如何修改http请求中的某些数据。

I'm using the documentation here: https://developer.chrome.com/extensions/webRequest 我在这里使用文档: https : //developer.chrome.com/extensions/webRequest

I was able to setup the extension to listen for requests, but am not able to access the data I want. 我能够将扩展程序设置为侦听请求,但无法访问所需的数据。

When I'm in the chrome dev tools, on the Network tab, I right click the particular request I'm trying to modify and copy as cURL. 当我进入chrome dev工具时,在“网络”标签上,右键单击要修改并复制为cURL的特定请求。 The data I want to modify shows up after --data . 我要修改的数据显示在--data之后。 I want to access this and change an integer value one of the parameters is set to. 我想访问它并更改参数之一设置为的整数值。

I'm not sure what the equivalent is with these http requests, but I've tried the following: 我不确定这些http请求的含义是什么,但是我尝试了以下方法:

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    var bkg = chrome.extension.getBackgroundPage();
    bkg.console.log("onBeforeRequest");
    bkg.console.log(JSON.stringify(details));
    blockingResponse = {};

    return blockingResponse;
  },
  {urls: [/*URL*/]},
  ['requestBody','blocking']
);

I can find the request with the url that I am looking at in the Network tab of the dev tools, so I'll be able to parse that and make sure I'm only modifying the requests that I want to, but printing the details doesn't show the data that I actually want to modify. 我可以在开发工具的“网络”标签中找到带有所查看网址的请求,因此可以解析该请求并确保仅修改所需的请求,但可以打印详细信息不会显示我实际要修改的数据。 Any idea how to obtain the HTTP request equivalent of the --data argument of a cURL request? 任何想法如何获取等效于cURL请求的--data参数的HTTP请求? And, well, modify it. 并且,修改它。

Edit: Here's the progress I've made. 编辑:这是我取得的进步。

When I log those details, I get ..."requestBody":{"raw":[{"bytes":{}}]},... 当我记录这些详细信息时,我得到..."requestBody":{"raw":[{"bytes":{}}]},...

However, if I change onBeforeRequest to: 但是,如果我将onBeforeRequest更改为:

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    var bkg = chrome.extension.getBackgroundPage();
    bkg.console.log("onBeforeRequest");
    bkg.console.log(JSON.stringify(details));
    var encodedData = getBase64FromArrayBuffer(details.requestBody.raw[0].bytes);
    bkg.console.log("unencoded data: " + details.requestBody.raw[0].bytes);
    bkg.console.log("encodedData: " + encodedData);
    blockingResponse = {};

    return blockingResponse;
  },
  {urls: ["*://*.facebook.com/*"], types: ["xmlhttprequest"]},
  ['requestBody','blocking']
);

function getBase64FromArrayBuffer(responseData) {
    var uInt8Array = new Uint8Array(responseData);
    var i = uInt8Array.length;
    var binaryString = new Array(i);
    while (i--)
    {
      binaryString[i] = String.fromCharCode(uInt8Array[i]);
    }
    var data = binaryString.join('');

    var base64 = window.btoa(data);

    return base64;
}

The encoded data exists, showing a long string of chars, though it's gibberish. 编码后的数据存在,显示出一长串字符,尽管有点乱码。 Does this mean that I won't be able to access this data and modify it? 这是否意味着我将无法访问此数据并进行修改? Or is there a way to decode this data? 还是有一种方法可以解码此数据?

The chrome.webRequest API does allow you to access POST data. chrome.webRequest API确实允许您访问POST数据。 It does not, however, allow you to modify the POST data. 但是,它不允许您修改POST数据。

You are able to modify some of the header info, but not the POST data. 您可以修改某些标头信息,但不能修改POST数据。

It appears the ability to modify POST data was intended, but a dev at Google who was working on it got moved to something else, and sat on the bug/feature request for two years afterwards, and just released it so someone else could pick it up a few months ago. 似乎可以修改POST数据是有意的,但是Google的一名工作人员将其转移到其他地方,并在两年后接受了bug /功能请求,并刚刚发布了它,以便其他人可以选择它几个月前 If this is a feature that interests you, head to https://bugs.chromium.org/p/chromium/issues/detail?id=91191# and star this bug (requires gmail account), and perhaps some renewed interest will lead to someone completing the functionality. 如果这是您感兴趣的功能,请转至https://bugs.chromium.org/p/chromium/issues/detail?id=91191#并为该错误加注星标(需要gmail帐户),也许会引起一些新的兴趣给完成功能的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM