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

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

问题描述

是否可以创建修改 HTTP 响应正文的 Chrome 扩展程序?

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

我查看了 Chrome 扩展 API,但没有找到任何可做的这个.

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

推荐答案

一般来说,您不能使用标准 Chrome 扩展 API 更改 HTTP 请求的响应正文.

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

104058:WebRequest API:允许扩展到编辑响应正文.为问题加星标以获得更新通知.

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

如果您想编辑已知 XMLHttpRequest 的响应正文,通过内容脚本注入代码 以使用自定义(完整-特色)在触发真实事件之前重写响应.确保您的 XMLHttpRequest 对象与 Chrome 的内置 XMLHttpRequest 对象完全兼容,否则 AJAX 密集型网站将会中断.

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.

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

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天全站免登陆