在javascript中扩展ActiveXObject [英] Extending an ActiveXObject in javascript
问题描述
我想添加一些功能跟踪javascript中对ActiveX对象方法的某些调用。
I want to add some functionality track certain calls to ActiveX object methods in javascript.
我通常会像这样创建我的activeX对象:
var tconn = new ActiveXObject(Tconnector);
I usually create my activeX object like this: var tconn = new ActiveXObject("Tconnector");
每次在tconn和该activeX控件的所有其他实例上调用 open 方法时,我都需要记录。
I need to log every time the open method is called on tconn and all other instances of that activeX control.
我不能修改tconn的原型,因为它没有!
I cant modify tconn's prototype because it does not have one!
我认为我可以创建一个伪ActiveXObject函数来创建一个代理对象来代理对真实代理的调用。你可以帮我这么做吗?
I think that i can create a dummy ActiveXObject function that creates a proxy object to proxy calls to the real one. Can you help me do that?
注意:写一个直接包装器是不可能的,因为在应用程序中已经有1000次调用这个activeX。
Note: writing a direct wrapper is out of question, because there are already 1000s of calls to this activeX within the application.
推荐答案
您实际上可以覆盖 ActiveXObject()
。
这意味着您可以尝试围绕实际对象构建透明代理对象并挂钩方法调用。这意味着您必须围绕ActiveX对象的每个方法和属性构建代理,除非您绝对确定没有任何代码调用特定方法或属性。
This means you can try to build a transparent proxy object around the actual object and hook on method calls. This would mean you'd have to build a proxy around every method and property your ActiveX object has, unless you are absolutely sure there is no code whatsoever calling a particular method or property.
我为MSXML2.XMLHTTP
对象构建了一个小包装器。你可能会遇到各种各样的问题,所以要考虑到这一点:
I've built a small wrapper for the "MSXML2.XMLHTTP"
object. There are probably all kinds of problems you can run into, so take that with a grain of salt:
var ActualActiveXObject = ActiveXObject;
var ActiveXObject = function(progid) {
var ax = new ActualActiveXObject(progid);
if (progid.toLowerCase() == "msxml2.xmlhttp") {
var o = {
_ax: ax,
_status: "fake",
responseText: "",
responseXml: null,
readyState: 0,
status: 0,
statusText: 0,
onReadyStateChange: null
// add the other properties...
};
o._onReadyStateChange = function() {
var self = o;
return function() {
self.readyState = self._ax.readyState;
self.responseText = self._ax.responseText;
self.responseXml = self._ax.responseXml;
self.status = self._ax.status;
self.statusText = self._ax.statusText;
if (self.onReadyStateChange) self.onReadyStateChange();
}
}();
o.open = function(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword) {
varAsync = (varAsync !== false);
this._ax.onReadyStateChange = this._onReadyStateChange
return this._ax.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
};
o.send = function(varBody) {
return this._ax.send(varBody);
};
// add the other methods...
}
else {
var o = ax;
}
return o;
}
function Test() {
var r = new ActiveXObject('Msxml2.XMLHTTP');
alert(r._status); // "fake"
r.onReadyStateChange = function() { alert(this.readyState); };
r.open("GET", "z.xml");
r.send();
alert(r.responseText);
}
免责声明:特别是async / onReadyStateChange处理可能不对,而且代码也可能有其他问题。正如我所说,这只是一个想法。请小心处理。
Disclaimer: Especially the async/onReadyStateChange handling probably isn't right, and the code may have other issues as well. As I said, it's just an idea. Handle with care.
P.S。:对于方法和属性名称,COM对象不区分大小写。这个包装器(就像所有JavaScript一样)区分大小写。例如,如果您的代码恰好同时调用Send()
和send()
,您将在包装器中也需要一个骨架Send()方法:
P.S.: A COM object is case-insensitive when it comes to method- and property names. This wrapper is (as all JavaScript) case-sensitive. For example, if your code happens to call both "Send()"
and "send()"
, you will need a skeleton "Send()" method in the wrapper as well:
o.Send = function() { return this.send.apply(this, arguments); };
这篇关于在javascript中扩展ActiveXObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!