2013-02-11 41 views
0

所以我從EDITAREA得到了這個代碼片段的一個Web熒光筆,我如何重寫它以使其更易讀而不破壞代碼輸出行爲?適當的字符間距和縮進

editAreaLoader.iframe_script= "<script type='text/javascript'> Ã EA(){vart=Á;t.error=Ì;t.inlinePopup=[{popup_id:\"area_search_replace\",icon_id:\"search\"},{popup_id:\"edit_area_help\",icon_id:\"help\"}];t.plugins={};t.line_number=0;È.eAL.set_browser_infos(t);if(t.isIE >=8)t.isIE=7;t.É={};t.last_text_to_highlight=\"\";t.last_hightlighted_text=\"\";t.syntax_list=[];t.allready_used_syntax={};t.check_line_selection_timer=50;t.ÂFocused=Ì;t.highlight_selection_line=null; text=text.replace(sy[\"custom_regexp\"]['after'][i]['regexp'],convert);}}return text;};var editArea= eA;EditArea=EA;</script>".replace(/Á/g,'this').replace(/Â/g,'textarea').replace(/Ã/g,'function').replace(/Ä/g,'prototype').replace(/Å/g,'settings').replace(/Æ/g,'length').replace(/Ç/g,'style').replace(/È/g,'parent').replace(/É/g,'last_selection').replace(/Ê/g,'value').replace(/Ë/g,'true').replace(/Ì/g,'false'); 

如何重寫代碼,使其更具可讀性?謝謝。

+2

我不明白這段代碼是什麼。爲什麼要寫一些帶有特殊字符的關鍵字,然後再替換它們呢? – JJJ 2013-02-11 09:56:09

回答

0

我不知道我真正瞭解這個問題,但我敢說你要麼希望將字符串分割:

editAreaLoader.iframe_script= "<script type='text/javascript'>" + 
    "Ã EA(){" + 
    "vart=Á;" + 
    ... 

...或負載的東西從外部文件:

editAreaLoader.iframe_script= "<script type='text/javascript' src='/path/fo/file.js'>" + 
+0

試過了你的答案,它不起作用 – 2013-02-11 10:18:11

+1

它怎麼能不*工作? – 2013-02-11 10:18:50

1

代碼擴展到以下幾點:

 
function EA() { 
    vart = this; 
    t.error = false; 
    t.inlinePopup = [{ 
     popup_id: "area_search_replace", 
     icon_id: "search" 
    }, { 
     popup_id: "edit_area_help", 
     icon_id: "help" 
    }]; 
    t.plugins = {}; 
    t.line_number = 0; 
    parent.eAL.set_browser_infos(t); 
    if (t.isIE >= 8) t.isIE = 7; 
    t.last_selection = {}; 
    t.last_text_to_highlight = ""; 
    t.last_hightlighted_text = ""; 
    t.syntax_list = []; 
    t.allready_used_syntax = {}; 
    t.check_line_selection_timer = 50; 
    t.textareaFocused = false; 
    t.highlight_selection_line = null; 
    text = text.replace(sy["custom_regexp"]['after'][i]['regexp'], convert); 
} 
} 
return text; 
}; 
var editArea = eA; 
EditArea = EA; 

但它有語法錯誤,因此在人不工作湖

+0

假設它沒有錯誤。我沒有複製整個代碼,因爲它很長。順便說一句,我想這樣的輸出,但只是它被包裝成字符串。 – 2013-02-11 10:14:18

+0

只需在瀏覽器的JavaScript控制檯中評估代碼即可。然後使用一些像http://jsbeautifier.org/這樣的漂亮打印機來設置它的格式。 – 2013-02-11 10:18:48