Recently, the company's products have cooperation with other sites, the trouble is that each site has personalized needs, and some need their own logo, and some need to customize the ads.
For example, by default, we're displaying Google ad http://www.wangchao.net.cn/bbsdetail_1950471.html.
However, this station needs to be dedicated to display tourism advertising http://www.wangchao.net.cn/bbsdetail_1950471.html
The UI of the product is rather complex, and hard code is certainly not acceptable. At this time I miss the Windsor I used in. NET, so I immediately got a dynamic agent out (for the dynamic language is a piece of cake), so I can give a specific host site by adding additional JS files to inject their own custom code
(function(){
var interceptors = {};
$.intercept = function(name, func){
var parts = name.split('.');
var owner = window;
for (var i = 0; i < parts.length - 1; i++){
owner = owner[parts[i]];
if (!owner) break;
}
if (owner){
var funcName = parts[parts.length - 1];
var target = owner[funcName];
}
if (!(owner && target)){
interceptors[name] = func;
return;
}
owner[funcName] = function(){
this.__invocation__ = target;
func.apply(this, arguments)
}
}
$._ = function(name, func, scope, args){
var interceptor = interceptors[name];
if (interceptor){
scope = scope || window;
scope.__invocation__ = func;
return interceptor.apply(scope, args || []);
}
else {
return func.apply(scope, args || []);
}
}
})()
To intercept a function is simple, with the name of the function being intercepted and the function used to intercept as arguments
$.intercept = function('objectName.methodName', function(){
//do something
//and invoke the original method if needed
this.__invocation();
})