0

I want to write something that will automatically (using a rule system and a list of words to use as replacement) should replace all variables and function names with something else:

eg:

var foot = 0;
function cat(state) {
    return state ? "running" : "sleep";
}
cat(foot);

To:

var house = 0;
function bear(country) {
    return country ? "running" : "sleep";
}
bear(house);

I have searched the net but haven't found any project that can easily be modified to do this.

Do any of you know how to do this or know of a project that I can use as a starting point?

Alex
  • 37,502
  • 51
  • 204
  • 332
  • 2
    Do you want this to be runtime or build time? – Roonaan Jan 04 '13 at 17:17
  • Is this for an ide? Kind of like a smart find/replace – Alex Jan 04 '13 at 17:19
  • Maybe a shell script using `sed` or `grep`? – bfavaretto Jan 04 '13 at 17:20
  • 'Find & Replace' command in Notepad++ would work wonders :) – Shaz Jan 04 '13 at 17:22
  • It's not as simple as replacing words. You will need to write a complex process that can read Javascript and distinguish between user defined function names and variable names. – MrCode Jan 04 '13 at 17:22
  • @Roonaan Not sure i understand. How i want it to work: Point it at a directory or a list of js files, parse the files. Tell it to replace the names with names in a specific list file. Done. Do you call that build time? – user1768203 Jan 04 '13 at 18:21

3 Answers3

1

Are you lloking for an Obfuscator or what you like to do?

How can I obfuscate (protect) JavaScript?

Community
  • 1
  • 1
GreenRover
  • 1,486
  • 1
  • 14
  • 33
  • I was considering using an obfuscator for this purpose but none that i found allowed me to control what the names would be replaced with. – user1768203 Jan 04 '13 at 17:35
  • In this case i would prefer to open code in http://netbeans.org/ Then right click on function / variable definition refactor -> rename – GreenRover Jan 04 '13 at 19:55
0

You could write a short Shell script (e.g. bash). You need sed and for loops and three variables that contain the word lists. If you have your first code with the foot and the cat and the state in a file called my_first_code.txt, then it would look like this:

foot_words="house ba be bi bo bu"
cat_words="bear da de di do du"
state_words="country ka ke ki ko ku"

count=1
for word in $foot_words; do
    sed 's%foot%'$word'%g' my_first_code.txt > new_code_$count.txt
    ((count++))
done
count=1
for word in $cat_words; do
    sed -i 's%cat%'$word'%g' new_code_$count.txt
    ((count++))
done
count=1
for word in $state_words; do
    sed -i 's%state%'$word'%g' new_code_$count.txt
    ((count++))
done

In this example you will get 6 new files new_code_1.txt, new_code_2.txt, new_code_3.txt and so on.

Explanation: The first for loop copies the code in my_first_code.txt and replaces the word foot by the new word. The two other for loops just replace the words in the new file.

erik
  • 2,278
  • 1
  • 23
  • 30
0

The Google Closure Compiler (https://developers.google.com/closure/compiler/) has the ability to recognize function and variable names, but no built in option to replace them with a name of your choosing.

Because a straight find and replace would have no context, if you wanted to roll you're own you'd need to use regular expressions to "parse" the JavaScript, which is difficult but manageable if you use recursive regular expressions like in .NET with balancing groups

http://msdn.microsoft.com/en-us/library/bs2twtah.aspx

get inner patterns recursively using regex c#

Community
  • 1
  • 1
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62