简体   繁体   English

如何通过Chrome扩展程序修改请求正文

[英]How to modify request bodies by chrome extension

I've tried use chrome.webRequest API and finally found out that it looks like google don't allow us to modify requestBodies of POST requests? 我试过使用chrome.webRequest API,最后发现Google不允许我们修改POST请求的requestBodies吗? I could only cancel it or modify its headers. 我只能取消它或修改其标题。 So is there any other way to modify the raw (not form) body of a post request? 那么,还有其他方法可以修改发布请求的原始(非表单)正文吗? I know that a proxy server could do that but I want to deal with it using extension. 我知道代理服务器可以做到这一点,但是我想使用扩展名来处理它。

This works for some cases: first, save the body of the request in a variable in the onBeforeRequest listener. 这在某些情况下onBeforeRequest :首先,将请求的正文保存在onBeforeRequest侦听器中的变量中。 Then, in onBeforeSendHeaders you can either cancel or redirect the original request (sorry, Chrome gives you only two options to deal with the original). 然后,在onBeforeSendHeaders您可以取消或重定向原始请求(对不起,Chrome仅为您提供两个选项来处理原始请求)。 Also in onBeforeSendHeaders , you issue a new request (say, jquery ajax) to which you attach the old body from the variable, and the old headers - both can be modified/rewritten as needed. 同样在onBeforeSendHeaders ,您发出一个新请求(例如jquery ajax),将变量的旧主体和旧标头附加到该请求(可以根据需要进行修改/重写)。 (Minor catch: it won't let you set all of the headers for "security reasons", so you may add one more onBeforeSendHeaders listener to add the sensitive headers to the new request as well). (次要问题:出于“安全原因”,它不允许您设置所有标头,因此您可以再添加一个onBeforeSendHeaders侦听器,以将敏感标头也添加到新请求中)。

Works for cases when the request issuer is happy with a redirect or cancel as a response. 适用于请求发出者满意重定向或取消作为响应的情况。 If the request issuer expects the full actual response, intact, with no redirects, then it gets harder. 如果请求发布者期望完整的完整响应,没有重定向,那么它将变得更加困难。

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

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