HTA JSMin (Omitted modifier) based on javascript

Source: Internet
Author: User
Tags control characters

When I used JSMin.

Over time, I found it too troublesome! Since we are programmers, why not make things easier?

Therefore, I started to "friendly" JSMin.

In the process of "friendliness", I encountered unexpected problems. I will immediately discuss what problems I encountered and how to solve them.

However, I am sorry because I thought about writing text only after all the problems are solved. This is "no picture or truth ".

Before you begin, let me explain some technical knowledge involved in this issue-if you have the programming experience of the Windows Script Host, you can skip this part; if you have ASP experience, you may already know about this part.

Windows Script host(wsh.pdf is a Windows Script, which represents a few executable files (wscript.exeand cscript.exe). Their functions are to execute task Files written in JScript or VBScript without being restricted by Internet security policies, so as to perform some system management work. It is worth noting that, by default, Windows uses. all files with js extension are considered as WSH files. If you try to double-click. js files. You may find some error messages or be intercepted by anti-virus software.
HTML Application is short for HTA. as its name implies, it is an "HTML Application"-it executes scripts in the same loose security conditions as the Windows Script Host, unlike Windows Script hosts, HTML applications often have a good GUI. Essentially, an HTA file is actually an HTM file with a special extension. If you don't want to spend too much effort on creating the interface, and have both HTML and WSH experience, it is a good choice to use HTML Application to solve the problem. It should be noted that HTML Application is also often considered as the predecessor of WPF technology. If your target environment has WPF support (that is. NET Framework 3.0 +), so the use of WPF may be more suitable. Because the HTA file is easy to create, it was used for Trojan download. Some anti-virus software may still roughly regard the HTA file as a Trojan download tool. Like a binary executable file like .exe, whether an HTA file is harmful is determined by its own design, rather than by the file type.
FileSystemObject (FSO) is a component object provided by Windows Script technology to facilitate script operations on the file system. It is also common in ASP programming.
WshShell is a component object provided by the WSH runtime environment to facilitate script operations on Shell-related objects. Many WSH programs use this object.
ADODB. stream, also known as "ADO Stream", is a component object provided by ADO to facilitate the operation of binary data streams, but is often used outside the database, such as file operations.

"You only need to click the file icon." Such a feature is called "shell Association, most of the time, you know the shell association as "associate to double-click the mouse" or something. However, a few people may find that, in addition to the default commands, there are some other commands such as "edit" and "print" in the shortcut menu displayed by right-clicking.

What we need to do this time is to add a "Minimize" command for the. js file. When we click this command, JSMin will be started to lose weight for our ECMAScript code.

Here, I encountered the first problem:

I have compiled a. js file (actually a WSH task file) and used it to "automatically" Install ".

In Windows. add File Associations to js files, which must be in HKCR \. add subkeys to the Shell item under the "default" value corresponding to the "JSFile" -- HKCR \ JSFile, and the subordinate subitem named "Command.

After the Minimize item is added under HKCR \ JSFile \ Shell and the "My hta file path" % 1 "value is set for the Command subitem, I found that the Minimize command will generate an error like "invalid Executable File". If the start command is added before, the "open mode" dialog box appears ......

It seems that the Shell item is not so opportunistic, so I had to read the file type setting of htafile first, and then set it to the newly added Minimize command ,. the js file is written as follows:

Copy codeThe Code is as follows: view sourceprint? 1 var asocCommand = wshShell. regRead ("HKEY_CLASSES_ROOT \ htafile \ Shell \ Open \ Command \\"). replace ("% 1", instPath + "\" + appExec ). replace ("% *", '"% 1 "');

The shell association problem is solved, followed by the parsing of the command line parameters:

At first, I planned to use the WSH task file as the tool carrier, however, I immediately discovered that WSH files lack UI support by default-InputBox In VBScript and prompt in HTML do not exist in WSH task files compiled using JScript, if you insist on using WSH, you can only obtain user input through the Windows console. If the user input is obtained through the console, the standard usage process of this tool is changed to "click the file icon-shortcut menu-Minimize-enter a character on the keyboard ", after a series of mouse operations (of course, it is also possible to use a keyboard operation), you can suddenly switch to a keyboard, which seems to be quite difficult.

Therefore, I chose the file type that provides rich interfaces such as HTA as a way to implement this tool.

When HTA is selected, some features of WSH "do not pass out" are also lost. For example, the "Arguments" object used to parse command line parameters is missing.

In my conception, the-level parameter can be used to specify the code reduction level of JSMin, and the-silent parameter can be used to close the prompt information. If these parameters cannot be interpreted from the command line, these functions cannot be implemented.

Therefore, I have compiled two functions:

Copy codeThe Code is as follows: // ==========/// ==========
// Parse command-line info
// Interpret the actual path and additional parameters of the HTA from the command line in the HTA Environment
// This part of the code is original to NanaLich and you can use it directly without prior written consent. You shall not arbitrarily claim that you or your institution have created the code or release the modified version in the name of NanaLich.
// ==========/// ==========
Function namedOrNot (args ){
Var named ={}, not = [], c;
For (var I = 0; I <args. length; I ++ ){
C = args [I];
Switch (c. charAt (0 )){
Case "-":
Case "/":
C = c. substring (1 );
If (c. indexOf ("=")> 0 ){
C = c. split ("= ");
Named [c. shift ()] = c. join ("= ");
} Else if (c. indexOf (":")> 0 ){
C = c. split (":");
Named [c. shift ()] = c. join (":");
} Else {
// It cannot be determined whether a named parameter also accepts additional parameters. This is an unsolved problem.
// I ++;
Named [c] = args [I + 1];
}
Break;
Default:
Not. push (c );
Break;
}
}
Args. named = named;
Args. unnamed = not;
}
Function parseArgs (str ){
Var a = [], q = false, c = "", $ = "";
Function mit (){
If (c)
A. push (c );
}
For (var I = 0; I <str. length; I ++ ){
$ = Str. charAt (I );
If ($ = '"'){
Q =! Q;
} Else {
If ($ = ""&&! Q ){
Mit ();
C = "";
} Else {
C ++ =$;
}
}
}
Mit ();
NamedOrNot ();
Return;
}
// ==========/// ==========

In this way, you only need to pass the commandLine attribute of the HTA: Application object to this function, and then you can get the name and non-name parameters after resolution.

After the problem of command line parameters is solved, the next step is the file encoding problem.

In our actual Web development process, we may use other encoding formats other than "non-Unicode region encoding" and "Unicode (UTF-16) encoding" for various reasons, for example, Unicode (UTF-16) Big Endian and UTF-8 encoding methods.

If we use existing text editing tools to copy and paste code, we only need to select the encoding type when saving the file; but now we are designing a tool that removes the hassle of "open-copy-paste-save, in this tool, we need to design the function to automatically adapt to the encoding type.

Unfortunately, in my tests, FSO and ADODB. stream does not have the ability to automatically recognize text encoding. You must try another method. Fortunately, VBScript is also supported by default in HTA. Although VBScript does not directly operate on Byte groups, however, in VBScript, operations on Byte groups as strings can still meet our requirements to a certain extent.

Therefore, I have compiled the following function:Copy codeThe Code is as follows: Function vbDetectFileEncoding (fn)
Dim Stream, B3
Set Stream = CreateObject ("ADODB. Stream ")
Stream. Type = 1
Call Stream. Open ()
Call Stream. LoadFromFile (fn)
B3 = CStr (Stream. Read (3 ))

Call Stream. Close ()
Set Stream = Nothing

Dim L1
L1 = Left (B3, 1)
If (L1 = ChrW (& hFEFF) Then
VbDetectFileEncoding = "unicode"
Exit Function
Elseif (L1 = ChrW (& hFFFE) Then
VbDetectFileEncoding = "unicodeFEFF"
Exit Function
Elseif B3 = (ChrB (& hEF) & ChrB (& hBB) & ChrB (& hBF) Then
VbDetectFileEncoding = "UTF-8"
Exit Function
End If

VbDetectFileEncoding = defEncoding
End Function

This function infers the encoding method of a text file based on the BOM that may exist in the text file.

Note the following:

ADODB. stream related documentation wrote Allowed values are typical strings passed over the interface as Internet character set names (for example, "iso-8859-1", "Windows-1252", and so on ). for a list of the character set names that are known by a system, see the subkeys of HKEY_CLASSES_ROOT \ MIME \ Database \ Charset in the Windows Registry ., the project corresponding to "Unicode Big Endian" (Encoding 1201) in the registry is "unicodeFFFE ", it can be inferred from this information that the Charset attribute should be specified as "unicodeFFFE" when "Unicode Big Endian" encoding is used in ADO Stream ";

Here is a obfuscated implementation: the Unicode Number of BOM characters is U + FEFF, while the "General" and "Unicode encoding" are actually "Unicode Little Endian"-BOM characters will be written as "ff fe" two bytes, in Unicode Big Endian, it is written as "fe ff ".

Here, the problem arises-if "unicodeFEFF" is the opposite of "unicodeFFFE", we can understand that "FEFF" is the Unicode Number of BOM characters; but at the same time, what does "FFFE" in "unicodeFFFE" representing "Unicode Big Endian" mean? Obviously, this cannot be the meaning of "a Unicode Character numbered U + FFFE.

In practice, I found that no matter whether the Charset attribute is set to "unicode" or "unicodeFFFE", the output files are all encoded using "Unicode (Little Endian, this is clearly not in line with the meaning of the document.

When I try again, I find that if I want to output a "Unicode Big Endian" encoded file, the Charset attribute should be set to "unicodeFEFF"-we can easily find that, "FEFF" exactly matches the byte sequence of BOM characters in the file; we can also find that setting "unicodeFXFX" for the Charset attribute is actually equivalent to "encoding in byte order like FXFX", which is specially treated, it does not fully match what is written in the registry.

So far, the gadgets have been able to read and write files that are encoded in Unicode, Unicode Big Endian, UTF-8, and non-Unicode region encoding, but I still don't want to understand why the document and registry contain information that is incorrect or completely useless ......

After these problems are solved one by one, it seems that there are no problems worth studying.

However, in the process of using JSMin, another problem is found:

Some people (for example, I) work mainly in Windows. In Windows, the default line Separator of text files is CR LF.

And JSMin will replace all CR with LF, so that cr lf pairs will become the "LF" of two LF pairs.

According to the original JSMin design, the control character LF is usually reduced, so two consecutive LF is not a problem; but jsmin. one of the new functions of js is to retain certain features (/*! ... */), And if there is a cr lf pair in this "Important comment", it will eventually become two LF control characters that cannot be removed, which is not good.

To solve this problem, I made some modifications to jsmin. js ...... However, because JSMin is a little beyond my understanding, I can only turn CR into spaces. However, this method also has some advantages in Windows (in most Windows versions, the single LF control character is hardly observed in the "Notepad" program, and the two lines of text separated by the LF control character seem to be stuck together ...... I will not list these modifications separately.

All the Code mentioned in this article can be found in this compressed package.

The package contains three files: install. js is the "installation" script and jsmin associated with the registration file. hta is the "application", jsmin. js is the jsmin after I modified it. js.

After extracting the three files to the same folder, double-click install. js to install the tool. If you reinstall the operating system, you may find that the tool remains in your personal folder; if you double-click the install. js, you can use this tool again.

Note: Since Windows XP, a newer version of Windows will set a flag for the files downloaded from the network. This flag may make the HTA file unable to run normally, if you encounter this problem during usage, click "unlock" in the file Properties dialog box to remove this mark.

Update: Modified install. js. Now the 64-bit Windows 7 can also be correctly installed. After installation, you do not need to manually "unlock" it.

The secret is here:Copy codeThe Code is as follows: var appsPath = wshShell. ExpandEnvironmentStrings (wshShell. RegRead (regUSF + "Personal") + "\ Scriptlet ";

Try {
Fso. OpenTextFile (instPath + "\" + appExec + ": Zone. Identifier", 1). Close ();
Fso. OpenTextFile (instPath + "\" + appExec + ": Zone. Identifier", 2). Close ();
} Catch (ex ){}

Package and download files with a renamed jse. This makes it easy for users who often develop JavaScript files to avoid confusion.
Because many of my friends use win2003 for development and the. js file is opened in plain text. It is impossible to run it in Javascript later. simply change it to install. jse to run it.
Thanks to the author for publishing such a good thing. Author's blog address
Http://www.cnblogs.com/NanaLich

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.