-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsh2.txt
67 lines (56 loc) · 1.43 KB
/
wsh2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#WSH #2
## Полезные COM-объекты
### File System Object
Операции с файлами (чтение-запись)
```
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.OpenTextFile("hello.txt");
var lines = file.ReadAll().split("\r\n");
for (var i = 0; i < lines.length; ++)
WScript.Echo(i + " " + lines[i]);
```
### XMLHttpRequest
Запрос данных по HTTP
```
xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
xmlRequest.open("GET", "http://www.e1.ru");
xmlRequest.send(null);
while (xmlRequest.readyState != 4) {
//WScript.Echo(xmlRequest.readyState);
WScript.Sleep(100);
}
//if (xmlRequest.readyState == 4){
if (xmlRequest.status == 200){
WScript.Echo(xmlRequest.responseText);
}
//}
```
Если вдруг хотим реальной асинхронности:
```
xmlRequest.onreadystatechange = function(){
if(xmlRequest.readyState == 4){
...
}
}
## XMLDocument - работа с XML
```xml
<files>
<file os = "win" name = "autoexec.bat" />
<file os = "win" name = "config.sys" />
<file os = "linux" name = "/etc/passwd" />
</files>
```
```javascript
var xmlDocument = WScript.CreateObject("Msxml12.DOMDocument.3.0");
if(xmlDocument.load("files.xml") != 0)
{
var list = xmlDocument.selectNodes("//*/file[@os='win']/@name");
var e = new Enumerator(list);
for(;!e.atEnd(); e.moveNext())
{
WScript.Echo(e.item().text);
}
}
else
WScript.Echo("No file found");
```