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?

6
  • 2
    Do you want this to be runtime or build time? Commented Jan 4, 2013 at 17:17
  • Is this for an ide? Kind of like a smart find/replace Commented Jan 4, 2013 at 17:19
  • Maybe a shell script using sed or grep? Commented Jan 4, 2013 at 17:20
  • 'Find & Replace' command in Notepad++ would work wonders :) Commented Jan 4, 2013 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. Commented Jan 4, 2013 at 17:22

3 Answers 3

1

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

How can I obfuscate (protect) JavaScript?

Sign up to request clarification or add additional context in comments.

2 Comments

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.
In this case i would prefer to open code in netbeans.org Then right click on function / variable definition refactor -> rename
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.

Comments

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#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.