REY Sébastien - 2016-03-21
  • change encodeStrings=1 to encodeStrings=0 to desactivate string encoding
  • function renamed
  • parameters renamed
  • variables renamed
  • strings encoded and moved
import re
import string
import random

encodeStrings=1
protected=["abstract","arguments","boolean","break","byte","case","catch","char","class*","const","continue","debugger","default","delete","do","double","else","enum*","eval","export*","extends*","false","final","finally","float","for","function","goto","if","implements","import*","in","instanceof","int","interface","let","long","native","new","null","package","private","protected","public","return","short","static","super*","switch","synchronized","this","throw","throws","transient","true","try","typeof","var","void","volatile","while","with","yield","Array","Date","eval","function","hasOwnProperty","Infinity","isFinite","isNaN","isPrototypeOf","length","Math","NaN","name","Number","Object","prototype","String","toString","undefined","valueOf"]
caracters = ['{','}','!','=','+','-','*','%','/','<','>','(',')',';','&','|',',',']','[','.',':']
caracters2 = list(caracters)
mywords=[]
unique=[]
tabstrings=[]

def generate():
    code = ''.join([random.choice(string.ascii_letters) for n in xrange(5)])
    if code not in unique:
        unique.append(code)
    else:
        return generate()
    return code

def encode(str):
    return '"'+''.join(('\\x%02x' % ord(c)) if ord(c) <= 255 else ('\\u%04x' % ord(c)) for c in str)+'"'

#Get text
donnees=editor.getText().decode("utf8")

#remove comments
donnees=re.sub('(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)','',donnees)

#encode strings to protect them
strings=re.finditer("(\".*?\")|(\'.*?\')",donnees)
for s in strings:
    if (encodeStrings==1):
        codedStr=encode(s.group(0)[1:-1])
    else:
        codedStr=s.group(0)
    if codedStr not in tabstrings:
        tabstrings.append(codedStr)
        console.write(s.group(0)+"\n")
        donnees=re.sub("(\"|\')"+re.escape(s.group(0)[1:-1])+"(\"|\')","t["+str(len(tabstrings)-1)+"]",donnees)

#separate words
for c in caracters:
    donnees=re.sub("(\\"+c+")"," "+c+" ",donnees)

#new file
notepad.new()
editor.setCodePage(65001)
notepad.menuCommand(MENUCOMMAND.LANG_JS)

#variables
strings=re.finditer("(var\s|const\s)\s*([a-zA-Z][a-zA-Z\d_]*(?:\s*,\s*[a-zA-Z][a-zA-Z\d_]*)*)\s*",donnees)
for s in strings:
    if ((s.group(2) not in protected) and (s.group(2) not in mywords)):
        if s.group(2) != '':
            mywords.append(s.group(2))
#functions
strings=re.finditer("function\s+(.*)\(",donnees)
for s in strings:
    if ((s.group(0).split(' ')[1] not in protected) and (s.group(0).split(' ')[1] not in mywords)):
        if s.group(0).split(' ')[1] != '':
            mywords.append(s.group(0).split(' ')[1])

#parametres
strings=re.finditer("function\s+.*\((.*)\)",donnees)
for s in strings:
    for p in s.group(1).replace(" ","").split(","):
        if ((p not in protected) and (p not in mywords)):
            if p != '':
                mywords.append(p)
for s in mywords:
    donnees=re.sub("\s"+s+"\s"," "+generate()+" ",donnees)

#minimize
for c in caracters2:
    donnees=re.sub("\s*\\"+c+"\s*",c,donnees)
donnees=re.sub("\s+"," ",donnees)
res='var t=['
for s in tabstrings:
    res=res+s+","
res=res+"'']"
print(res)
print(donnees.encode("utf8"))
 

Last edit: REY Sébastien 2016-03-22