JS Dynamic Agent

Source: Internet
Author: User

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();
})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.