天天看點

在EditPlus中使用JSLint校驗JavaScript代碼

  本文以Editplus為例,安裝此功能。

  此插件包含兩個檔案:JSLint.wsf和JSLint.js,可以從http://www.jslint.com/fulljslint.js下載下傳最新版本來替換解壓後的JSLint.js。Editplus通過WScript.exe執行JSLint.wsf,将目前檔案的内容給JSLint.js檢測,并輸出檢測結果。

  但這個插件存在一個問題:僅支援ASCII編碼的校驗,對于包含中文的UTF-8編碼的檔案會停止校驗。

  打開JSLint.wsf,裡面讀取檔案的時候,預設都采用了ASCII的編碼讀取的,是以對非ASCII編碼的檔案不會正常校驗。

  在JSLint.wsf的最後的部分可以找到如下代碼:

1

2

3

4

5

6

7

8

9

<code>if</code><code>(args.length&gt;0) {</code>

<code>    </code><code>var</code> <code>fso =</code><code>new</code> <code>ActiveXObject(</code><code>"Scripting.FileSystemObject"</code><code>),</code>

<code>        </code><code>file = fso.OpenTextFile(filepath),</code>

<code>        </code><code>cot = file.ReadAll();</code>

<code>    </code><code>JSLINT(cot);</code>

<code>    </code><code>var</code> <code>report = JSLINT.reportTXT();</code>

<code>    </code><code>WScript.StdOut.Write(report);</code>

<code> </code><code>}</code>

<code> </code><code>WScript.Quit();</code>

  這裡采用FSO的OpenTextFile擷取檔案内容。OpenTextFile的格式為:

object.OpenTextFile(filename[, iomode[, create[, format]]])

  format指定了讀取檔案的編碼格式,其值可以為:

-2:采用系統預設編碼

-1:采用Unicode

0:采用ASCII,預設值

  其預設值為0,也就是讀取ASCII編碼的檔案,不支援指定編碼為UTF-8,是以針對UTF-8編碼的檔案會報錯。

  為了支援對UTF-8檔案的檢測,改用ADODB.Stream讀取檔案,将這段代碼改寫如下:

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<code>/**</code>

<code>    </code><code>var fso = new ActiveXObject("Scripting.FileSystemObject"),</code>

<code>/**/</code>

<code>    </code><code>var</code> <code>stream =</code><code>new</code> <code>ActiveXObject(</code><code>"ADODB.Stream"</code><code>),</code>

<code>    </code><code>cot =</code><code>''</code><code>;</code>

<code>    </code><code>stream.Mode = 3; </code><code>// 常用值 1:讀,2:寫,3:讀寫,目前環境之允許用3</code>

<code>    </code><code>stream.Type = 2; </code><code>// 1:二進制,2:文本(預設)</code>

<code>    </code><code>stream.Charset =</code><code>'UTF-8'</code><code>; </code><code>// 指定編碼</code>

<code>    </code><code>stream.Open();</code>

<code>    </code><code>stream.LoadFromFile(filepath);</code>

<code>    </code><code>cot = stream.ReadText(-1); </code><code>// 讀取全部内容</code>

<code>    </code><code>stream.Close();</code>

<code>}</code>

<code>WScript.Quit();</code>

  這樣,就可以對UTF-8進行JSLint的檢測了。

  目前是根據個人常用的檔案編碼方式(如UTF-8),将其寫死到JSLint.wsf中的,最好是能夠自動識别檔案編碼并讀取和校驗。這将涉及編碼方式的檢測,尤其是到對無BOM的UTF-8檔案檢測。

<a href="http://support.microsoft.com/kb/232211">在 Windows 中的 Windows 腳本宿主的概述</a>

<a href="http://code.google.com/p/vfresh/downloads/detail?name=EditPlus.JSLint%20v0.1.zip">原版本EditPlus.JSLint</a>

<a href="http://www.codeproject.com/KB/scripting/Exsead7.aspx">Reading and Writing Binary Files Using JScript</a>

<a href="http://msdn.microsoft.com/en-us/library/314cz14s(VS.85).aspx">OpenTextFile Method</a>

<a href="http://msdn.microsoft.com/en-us/library/ms675032(v=VS.85).aspx">Stream Object (ADO)</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-12.html">Web 前端工程師和設計師必讀精華文章推薦</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/07/28/html5-awesome-single-page-sites-inspiration.html" target="_blank">酷!15個精美的 HTML5 單頁網站作品欣賞</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/11/22/best-awesome-css3-animation-demos.html">炫!35個讓人驚訝的 CSS3 動畫效果示範</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/03/02/30-mind-blowing-parallax-scrolling-effect-websites.html" target="_blank">贊!30個與衆不同的優秀視差滾動效果網站</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/01/13/25-outstanding-single-page-website-designs.html">靓å!25個優秀的國外單頁網站設計作品欣賞</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/09/awesome-html5-and-javascript-effects.html">帥!8個驚豔的 HTML5 和 JavaScript 特效</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/06/27/35-exclusive-rainbow-colored-flash-websites.html" target="_blank">頂!35個很漂亮的國外 Flash 網站作品欣賞</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/24/outstanding-admin-panels-part-one.html" target="_blank">哇!34個漂亮網站和應用程式背景管理界面</a>

在EditPlus中使用JSLint校驗JavaScript代碼

<a href="http://www.yyyweb.com/go/web" target="_blank">本部落格新站點 ◆ 前端裡 ◆ 歡迎圍觀:)</a>

歡迎任何形式的轉載,但請務必注明出處。

繼續閱讀