在 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屋!