修改Chrome扩展程序中的HTTP响应 [英] Modify HTTP responses from a Chrome extension

查看:841
本文介绍了修改Chrome扩展程序中的HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个修改HTTP响应实体的Chrome扩展?



我已经查看了 Chrome扩展API ,但我还没有发现任何可以做到这一点的内容。

解决方案

通常,不能使用标准的Chrome扩展API更改HTTP请求的响应正文。



此功能正在 104058:WebRequest API:允许扩展程序编辑响应主体<一>。

如果您想编辑已知 XMLHttpRequest 的响应正文, 注入代码通过内容脚本来重写默认的 XMLHttpRequest 构造函数,并使用自定义(全功能)构造函数来重写响应,然后触发真实事件。确保您的XMLHttpRequest对象完全符合Chrome内置的 XMLHttpRequest 对象,否则AJAX网站会崩溃。



在其他情况下,您可以使用 chrome.webRequest chrome.declarativeWebRequest API将请求重定向到 data: -URI。与XHR方法不同,您不会得到请求的原始内容。实际上,请求永远不会到达服务器,因为重定向只能在发送实际请求之前完成。如果您重定向 main_frame 请求,用户将看到 data: -URI,而不是请求的URL。 / p>

Is it possible to create a Chrome extension that modifies HTTP response bodies?

I have looked in the Chrome Extension APIs, but I haven't found anything to do this.

解决方案

In general, you cannot change the response body of a HTTP request using the standard Chrome extension APIs.

This feature is being requested at 104058: WebRequest API: allow extension to edit response body. Star the issue to get notified of updates.

If you want to edit the response body for a known XMLHttpRequest, inject code via a content script to override the default XMLHttpRequest constructor with a custom (full-featured) one that rewrites the response before triggering the real event. Make sure that your XMLHttpRequest object is fully compliant with Chrome's built-in XMLHttpRequest object, or AJAX-heavy sites will break.

In other cases, you can use the chrome.webRequest or chrome.declarativeWebRequest APIs to redirect the request to a data:-URI. Unlike the XHR-approach, you won't get the original contents of the request. Actually, the request will never hit the server because redirection can only be done before the actual request is sent. And if you redirect a main_frame request, the user will see the data:-URI instead of the requested URL.

这篇关于修改Chrome扩展程序中的HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆