我正在尝试开发一个 Firefox 扩展,它可以丢弃对某个站点的每个 HTTP 请求并返回一个虚假响应。任何请求都不应到达原始 Web 服务器,但我希望能够创建自定义响应。我尝试拦截“http-on-modify-request”消息,但取消请求似乎不起作用,因为之后我无法模拟真实的响应。同样,使用 nsITraceableStream 实例,我似乎无法真正取消请求。我没有想法,有人可以帮忙吗?
I am trying to develop a Firefox extension that drops every HTTP request to a certain site and returns a fake response. No request should get through to the original web server, but I want to be able to create a custom response. I tried to intercept the "http-on-modify-request" message, but cancelling the request doesn't seem to work, as I cannot simulate a real response afterwards. Similarly, using an nsITraceableStream instance, I don't seem to be able to really cancel the request. I am out of ideas, can somebody help?
发布评论
评论(1)
从 Firefox 21 开始,下面的答案已被取代,现在是 nsIHttpChannel.redirectTo() 方法 很好地完成了这项工作。您可以重定向到 data: URI,类似这样的内容将起作用:
原始答案(已过时)
每个频道都有其关联的流侦听器,在收到数据时收到通知。要伪造响应,您所需要做的就是获取此侦听器并向其提供错误的数据。 nsITraceableChannel 实际上就是这样做的方法。您需要将频道的常用侦听器替换为您自己的侦听器,该侦听器不会执行任何操作,之后您可以取消该频道,而不会通知侦听器。然后触发侦听器并为其提供您自己的数据。像这样的事情:
这段代码的问题仍然是,如果频道被取消,页面显示为空白(所以我评论了该行) - 似乎侦听器仍然查看频道并注意到它被取消。
The answer below has been superseded as of Firefox 21, now the nsIHttpChannel.redirectTo() method does the job nicely. You can redirect to a data: URI, something like this will work:
Original answer (outdated)
Each channel has its associated stream listener that gets notified when data is received. All you need to do to fake a response is to get this listener and feed it with wrong data. And nsITraceableChannel is in fact the way to do it. You need to replace the channel's usual listener by your own that won't do anything, after that you can cancel the channel without the listener being notified about it. And then you trigger the listener and give it your own data. Something like this:
The problem with this code is still that the page shows up blank if the channel is canceled (so I commented that line) - it seems that the listener still looks at the channel and notices that it is canceled.