gpt4 book ai didi

javascript - 更改 Firefox 扩展中的 HTTP 响应

转载 作者:行者123 更新时间:2023-12-03 09:32:02 24 4
gpt4 key购买 nike

如何更改 Firefox 扩展中的 HTTP 响应正文?我已经使用下面的代码设置了一个 http-on-examine-response 观察者和一个 nsIStreamListener 对象。在我获取数据、解析并更改数据后,如何将更改后的响应推送回 Firefox 浏览器?例如,假设我在启用扩展程序的情况下访问 Google.com,扩展程序应拦截响应并将每次出现的“google”更改为“goggle”。所以当页面被加载时,用户会到处看到“护目镜”。

function TmSteroidsObserver()
{
this.register();
}


TmSteroidsObserver.prototype = {
observe: function(subject, topic, data) {

if (topic == "http-on-examine-response") {

}
else if (topic == "http-on-modify-request") {
var channel = subject.QueryInterface(Components.interfaces.nsIChannel);
var listener = new StreamListener(channel);
}

},

register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(listener, "http-on-modify-request", false);
observerService.addObserver(listener, "http-on-examine-response", false);
},

unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
observerService.removeObserver(this, "http-on-examine-response");
},

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

}

function StreamListener(channel) {

channel.notificationCallbacks = listener;
channel.asyncOpen(listener, null);

}

StreamListener.prototype = {
mData: "",
mChannel: null,

// nsIStreamListener
onStartRequest: function (aRequest, aContext) {
this.mData = "";
},

onDataAvailable: function (aRequest, aContext, aStream, aSourceOffset, aLength) {
var scriptableInputStream =
Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
scriptableInputStream.init(aStream);

this.mData += scriptableInputStream.read(aLength);
},

onStopRequest: function (aRequest, aContext, aStatus) {
if (Components.isSuccessCode(aStatus)) {
// request was successfull
this.mCallbackFunc(this.mData);
} else {
// request failed
this.mCallbackFunc(null);
}

this.mChannel = null;
},

// nsIChannelEventSink
onChannelRedirect: function (aOldChannel, aNewChannel, aFlags) {
// if redirecting, store the new channel
this.mChannel = aNewChannel;
},

// nsIInterfaceRequestor
getInterface: function (aIID) {
try {
return this.QueryInterface(aIID);
} catch (e) {
throw Components.results.NS_NOINTERFACE;
}
},

// nsIProgressEventSink (not implementing will cause annoying exceptions)
onProgress : function (aRequest, aContext, aProgress, aProgressMax) { },
onStatus : function (aRequest, aContext, aStatus, aStatusArg) { },

// nsIHttpEventSink (not implementing will cause annoying exceptions)
onRedirect : function (aOldChannel, aNewChannel) { },

// we are faking an XPCOM interface, so we need to implement QI
QueryInterface : function(aIID) {
if (aIID.equals(Components.interfaces.nsISupports) ||
aIID.equals(Components.interfaces.nsIInterfaceRequestor) ||
aIID.equals(Components.interfaces.nsIChannelEventSink) ||
aIID.equals(Components.interfaces.nsIProgressEventSink) ||
aIID.equals(Components.interfaces.nsIHttpEventSink) ||
aIID.equals(Components.interfaces.nsIStreamListener))
return this;

throw Components.results.NS_NOINTERFACE;
}
};

最佳答案

您可以使用 nsITraceableChannel 来拦截响应。

您应该修改可用于您需要的数据并将其传递给 innerListener 的 OnDataAvailable

下面的链接将帮助您更好地理解这一点。

http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/

http://www.ashita.org/howto-xhr-listening-by-a-firefox-addon/

关于javascript - 更改 Firefox 扩展中的 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1695440/

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