2009-02-06 17 views
16

Sto cercando di utilizzare un obfuscator javascript. Quali sono alcuni dei più popolari e quale impatto hanno sulle prestazioni?Qual è il miglior objugatore di javascript?

+1

Anche se le questioni sollevate da Robert Harvey possono essere più o meno vero, le risposte qui forniscono un elenco di obfuscators e è molto utile Inoltre, Harvey sottintende che questo sito esperto può solo rispondere a domande semplicistiche e "opinioni" esperte e la discussione estesa è in qualche modo negativa. La domanda sarebbe accettabile se fosse "Posso ottenere una lista di obsfuscator?" Cordiali saluti, Dave H. – DHorse

+0

** DUPLICATO DI ** - >>> ** http: //stackoverflow.com/questions/194397/how-can-i-obfuscateprotect-javascript** –

+0

è possibile utilizzare http: // jsobfuscator .byethost7.com/ –

risposta

12

Yahoo ha uno piuttosto buono. Tecnicamente è un minificatore, ma fa un buon lavoro di offuscare nel processo.

YUI Compressor

+0

Compresso e offuscato tramite Compressore YUI online gratuitamente qui: http://refresh-sf.com/yui/ – NexusRex

+0

il collegamento non funziona più –

3

Ebbene, Google ha portato a questo come il primo anello:

http://www.javascriptobfuscator.com

ma mi chiedo a cosa offuscamento di javascript fa. Qualunque cosa tu stia facendo in javascript che ha bisogno di confusione dovrebbe probabilmente essere fatta dal lato server, giusto?

+0

Di tutto ciò che ho visto, questo è forse il miglior obfuscator. Anche se non ho provato JScrambler. – AStackOverflowUser

2

non ho mai usato Obfuscator in produzione, ma ho provato JavaScript Utility e sembra piuttosto buona.

Per quanto riguarda le prestazioni, il codice offuscato deve essere decompresso al volo ogni volta che viene caricata la pagina. Potrebbe non essere un problema per i piccoli script, ma il tempo di decompressione sarà significativo con i file più grandi. D'altra parte, il codice minificato è direttamente eseguibile dal browser.

Alcuni offuscatori possono produrre output che non funzionano nei browser più vecchi o meno comuni. Dovresti testare molto attentamente con i browser che intendi supportare.

+1

Gli Obfuscator non richiedono il disimballaggio del codice. Altri schemi per minimizzare il codice potrebbero richiedere questo, ma l'obsolescenza di per sé non lo richiede. –

+0

Sono d'accordo sul fatto che mi sono confuso e imballato qui. L'imballaggio è un approccio comune per offuscare JS, ma il suo scopo principale è quello di ridurre la dimensione del file di script. –

6

Testato 8 obfuscators differenti (ad eccezione www.javascriptobfuscator.com), e sono rimasto stupito da quanto tutti schifo. Finito per scrivere il mio stesso offuscatore usando le espressioni regolari. Godetevi:

static Dictionary<string, string> names = new Dictionary<string, string>(); 
static bool testing = false; 
static string[] files1 = 
    @"a.js,b.js,c.js" 
    .Split(new string[] { Environment.NewLine, " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); 
static string[] ignore_names = 
    @"sin,cos,order,min,max,join,round,pow,abs,PI,floor,random,index,http, 
    __defineGetter__,__defineSetter__,indexOf,isPrototypeOf,length,clone,toString,split,clear,erase 
    RECT,SIZE,Vect,VectInt,vectint,vect,int,double,canvasElement,text1,text2,text3,textSizeTester,target,Number 
    number,TimeStep,images,solid,white,default,cursive,fantasy,". 
    Split(new string[] { Environment.NewLine, " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); 
string[] extra_names = @"a,b,c".Split(new string[] { Environment.NewLine, " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); 
string src = @"C:\temp"; 
string dest1 = src + "\\all1.js"; 
string dest2 = src + "\\all2.js"; 

static void Main() 
{ 
    File.Delete(dest1); 
    File.Delete(dest2); 
    foreach (string s in files1) 
    File.AppendAllText(dest1, File.ReadAllText(src + "\\" + s) + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine, Encoding.UTF8); 

    string all = File.ReadAllText(dest1); 
    int free_index = 0; 

    foreach (string s in extra_names) 
    { 
    free_index++; 
    string free_name = "" + (char)('A' + (free_index % 25)) + (char)('A' + ((free_index/25) % 25)); 
    Debug.Assert(free_name != "AA"); 
    names.Add(s, free_name); 
    } 

    Regex reg1 = new Regex("(var |function |\\.prototype\\.)([a-zA-Z0-9_]+)"); 

    int startat = 0; 
    while (startat < all.Length) 
    { 
    Match match = reg1.Match(all, startat); 
    if (!match.Success) 
     break; 

    string key = all.Substring(match.Groups[2].Index, match.Groups[2].Length); 
    if (!ignore_names.Contains(key)) 
    { 
     free_index++; 
     string free_name = "" + (char)('A' + (free_index % 25)) + (char)('A' + ((free_index/25) % 25)); 
     Debug.Assert(free_name != "AA"); 
     if (!names.ContainsKey(key)) 
     names.Add(key, testing ? key + free_name : free_name); 
    } 
    startat = match.Groups[0].Index + match.Groups[0].Length; 
    } 

    Regex reg2 = new Regex(@"/\*.*\*/", RegexOptions.Multiline); 
    Regex reg3 = new Regex("([^:\\\\])//.*\r\n"); 
    Regex reg4 = new Regex("([a-zA-Z0-9_]+)"); 
    Regex reg5 = new Regex("(\r\n)*[ \t]+"); 
    Regex reg6 = new Regex("(\r\n)+"); 
    all = reg2.Replace(all, eval2); 
    all = reg3.Replace(all, eval3); 
    all = reg4.Replace(all, eval4); 
    all = reg5.Replace(all, eval5); 
    all = reg6.Replace(all, eval6); 
    File.WriteAllText(dest2, all); 
} 

public static string eval4(Match match) 
{ 
    return names.ContainsKey(match.Groups[1].Value) ? names[match.Groups[1].Value] : match.Groups[0].Value; 
} 
public static string eval5(Match match) 
{ 
    return string.IsNullOrEmpty(match.Groups[1].Value) ? " " : Environment.NewLine; 
} 
public static string eval6(Match match) 
{ 
    return Environment.NewLine; 
} 
public static string eval2(Match match) 
{ 
    return " "; 
} 
public static string eval3(Match match) 
{ 
    return match.Groups[1].Value + Environment.NewLine; 
} 
+12

Perché "fanno tutti schifo"? Quale problema risolve il tuo codice? – frenchie

+2

@frenchie Bene, per prima cosa, incolla il javascript offuscato generato con quegli offuscatori "sucky" nell'abilitazione per JavaScript nell'http: //jsbeautifier.org/ e guardalo subito non-offuscato. – trusktr

+1

@AareP, puoi fornire alcune trasformazioni di esempio? – trusktr

Problemi correlati