📜 ⬆️ ⬇️

New look at javascript injection

Recently I ran into the following task - there is a JavaScript object actions with a bunch of methods, in each of which the following variables should be defined:

 var persik = this,     
       actions = persik.actions,      
       next = (persik.dom. possibleOther whatTo.name of thisMethod.next)? 
           persik.dom. possible Else what else.This MethodName.next: function () {return false}; 


But I really didn’t want to define them, because firstly, the code is noticeably weary, and secondly, the actions object was created for future users of my Persik framework, and as it is supposed, everyone will be able not only to use ready-made actions methods, but also define their own. You can of course write in the documentation “guys, please insert these lines at the beginning of each method you create,” or not create any variables at all and write this instead (although there may be problems with this in some cases), write instead .actions, and instead of calling next () blurt out something like:
if (this.dom. possibleOtherThatTo.name of ThisMethod.next) this.dom.possibleOtherThatTo.name of ThisMethod.next ();
But all this is terribly not beautiful and wrong, and I decided to take a different path. The idea is simple - when initializing the framework, go through all the methods of the actions object, use toString () to get each method as a string, impudently insert the necessary lines of variable definitions there, make a function again with eval () and replace the existing method with a modified one. I will not give the source code of the search methods within the actions in my framework so as not to bore the public, but the modification of the methods looks like this:
')
 _rewriteFunc: function (func, funcName, variablesStr) {     
       var funcArr = func.toString (). split ('{');     
       variablesStr = variablesStr.replace (/ \ [funcName \] / g, funcName);     
       funcArr [1] = variablesStr + funcArr [1];               
       var resultFunc = funcArr.join ('{');     
       eval ('var a =' + resultFunc);         
       return a; 
 },


Here, func is the actually modified method, the name of this method is passed to funcName, and all parents are considered, i.e. the same this.dom. is possible Else what else.ThisMethod name, and variablesStr contains a string with variables that we define. In my case, this is' var persik = this; var actions = persik.actions; var next = ([funcName] .next)? [funcName] .next: function () {return false}; '. The function described above replaces all '[funcName]' with funcName in variablesStr, inserts variablesStr into resultFunc after the first '{' character, collects the function from the string again and returns it. The object of actions, on which all the above-mentioned perversions are made, can be found here: nikitaeremin.com/js/action.js

This technique has a number of disadvantages:
- the variables persik, actions and next are created in all methods of the actions object, although in some methods they may remain unclaimed;
- the source code of JavaScript changes, and if errors occur during the runtime period, FireBug will not swear at all on the lines that actually caused the error, although the cause of the error is correct;
- other minuses to me now will tell habraskobschestvennost :)

I do not think that the trick described above is applicable to a wide range of tasks, this article is more likely for general development, but if someone wants to share ideas about other areas of application of such injections, write to soap: nikitaeremin [at] gmail.com

Source: https://habr.com/ru/post/52256/


All Articles