gpt4 book ai didi

javascript - 在 http-on-examine-response 之前返回响应

转载 作者:数据小太阳 更新时间:2023-10-29 04:10:40 26 4
gpt4 key购买 nike

我正在开发一个 Firefox 插件,它需要拦截 HTTP 请求过程并为某些 URL 返回虚假响应。这是我正在使用的代码:

function TracingListener() {
this.originalListener = null;
}

TracingListener.prototype = {
onDataAvailable: function(request, context, inputStream, offset, count) {
LOG("onDataAvailable");
this.originalListener.onDataAvailable(request, context, inputStream, offset, count);
},

onStartRequest: function(request, context) {
LOG("onStartRequest");
this.originalListener.onStartRequest(request, context);
},


onStopRequest: function(request, context, statusCode) {
LOG("onStopRequest");
this.originalListener.onStopRequest(request, context, statusCode);
var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
stream.setData("Test", -1);
this.originalListener.onStopRequest(request, context, Components.results.NS_OK);

},

QueryInterface: function (aIID) {
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsISupports)) {
return this;
}
throw Components.results.NS_NOINTERFACE;
}
};

var observer = {
observe: function(subject, topic, data) {
LOG(topic);
var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
var url = httpChannel.URI.spec;
LOG("Test:" + url);
if (!pattern.test(url)) return;

if (topic == 'http-on-modify-request') {
var newListener = new TracingListener();
newListener.originalListener = httpChannel.setNewListener(newListener);

} else if (topic == "http-on-examine-response" || topic == "http-on-examine-cached-response" || topic == "http-on-examine-merged-response") {
var newListener = new TracingListener();
newListener.originalListener = httpChannel.setNewListener(newListener);
}
}
};


var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver(observer, "http-on-modify-request", false);
observerService.addObserver(observer, "http-on-opening-request", false);

当有 URL 响应时它工作正常:我可以将响应更改为“测试”。

但是,对于某些无法访问或非常慢的 URL,永远不会调用 onStopRequest

我需要在发送任何实际请求之前返回一个请求,即“http-on-open-request”或“http-on-modify-request”。但在那一点上,Firefox 拒绝接受任何响应,因为他认为(这是真的)响应不应该可用。

我该怎么做?

最佳答案

http-on-modify-request 上,您应该可以调用 redirectTo或修改nsIChannel.URI将请求发送到数据 URI,例如 data:text/plain,Test

关于javascript - 在 http-on-examine-response 之前返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20069103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com