1/** 2 * JavaScript-hooks 3 * 4 * Examples: 5 * 6 * - inject function call/code at top of function body 7 * ls.hook.inject([ls.lang,'get'], function(){ls.msg.notice('lang debug');})}); 8 * ls.hook.inject([ls,'ajax'], 'alert(url)'); 9 * 10 * - add and call hooks 11 * ls.hook.add('somefunc_hook1_name', function(param1, param2){ ... }); 12 * 13 * function someFunc(..params..){ 14 * //code 15 * ls.hook.run('somefunc_hook1_name', [param1,param2], thisArg); 16 * //code 17 * } 18 * 19 * @author Sergey S Yaglov 20 * @link http://livestreet.ru/profile/1d10t 21 */ 22ls.hook = (function ($) { 23 this.hooks = {}; 24 25 this.cloneFunc = function(func,as_text,no_def) { 26 var f; 27 if($.type(func)=='string'){ 28 eval('f = '+func+';'); 29 }else if($.type(func)=='array'){ 30 f = func[0][func[1]]; 31 }else{ 32 f = func; 33 } 34 if($.type(f)=='function'){ 35 var fbody = f.toString().replace(/^(function)([^\(]*)\(/gi, '$1 ('); 36 if(typeof as_text!='undefined' && as_text){ 37 if(typeof no_def!='undefined' && no_def){ 38 return fbody.replace(/^[^\{]*\{/gi, '').replace(/\}$/gi, ''); 39 }else{ 40 return fbody; 41 } 42 } 43 return eval('('+fbody+')'); 44 } 45 return function(){}; 46 }; 47 48 /** 49 * @param func functionName|object[parentObject,functionName] Name of function that will be modified 50 * @param funcInj function|string Function or code to be injected 51 * @param marker string 52 */ 53 this.inject = function(func,funcInj,marker) { 54 var funcBody = ls.hook.cloneFunc(func, 1); 55 var funcDefinition = ($.type(func)=='string'?func:($.type(func)=='array'?'func[0][func[1]]':'func'))+' = '; 56 var replaceFrom = /\{/m; 57 var replaceTo = '{ '; 58 if($.type(marker) == 'string'){ 59 //replaceFrom = new RegExp('(\'\\*'+marker+'\\*\'[\r\n\t ]*;?)', 'm'); 60 replaceFrom = new RegExp('(ls\\.hook\\.marker\\(([\'"])'+marker+'(\\2)\\)[\\r\\n\\t ]*;?)','m'); 61 replaceTo = '$1'; 62 } 63 if($.type(funcInj)=='function'){ 64 var funcInjName = 'funcInj'+Math.floor(Math.random()*1000000); 65 eval('window["'+funcInjName+'"] = funcInj;'); 66 eval(funcDefinition + funcBody.replace(replaceFrom,replaceTo+funcInjName+'.apply(this, arguments); ')); 67 }else{ 68 eval(funcDefinition + funcBody.replace(replaceFrom,replaceTo+funcInj+'; ')); 69 } 70 }; 71 72 this.add = function(name,callback,priority) { 73 var priority = priority || 0; 74 if(typeof ls.hook.hooks[name] == 'undefined'){ 75 ls.hook.hooks[name] = []; 76 } 77 ls.hook.hooks[name].push({ 78 'callback': callback, 79 'priority': priority 80 }); 81 }; 82 83 this.run = function(name,params,o) { 84 var params = params || []; 85 var hooks = ls.hook.hooks; 86 if(typeof hooks[name] != 'undefined'){ 87 hooks[name].sort(function(a,b){ 88 return a.priority > b.priority ? 89 1 90 : (a.priority < b.priority ? -1 : 0) 91 ; 92 }); 93 $.each(hooks[name], function(i){ 94 var callback = hooks[name][i].callback; 95 if($.type(callback) == 'function'){ 96 callback.apply(o, params); 97 }else if($.type(callback) == 'array'){ 98 //console.log(callback); 99 callback[0][callback[1]].apply(o, params); 100 }else if($.type(callback) == 'string'){ 101 eval('(function(){'+callback+'}).apply(o, params);'); 102 }else{ 103 ls.debug('cant call hook "'+name+'"['+i+']'); 104 } 105 }); 106 } 107 }; 108 109 this.marker = function(name){ 110 // noop 111 }; 112 113 return this; 114}).call(ls.hook || {},jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.