Windows Scripting Host
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; i++)
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.Sleep(100);
}
if (xmlRequest.status == 200)
{
WScript.Echo(xmlRequest.responseText);
}
Если вдруг хотим реальной асинхронности:
xmlRequest.onreadystatechange = function() {
if (xmlRequest.readyState == 4)
{
...
}
}
XMLDocument - работа с xml
<files>
<file os="win" name="autoexec.bat" />
<file os="win" name="config.sys" />
<file os="linux" name="/etc/passwd" />
</files>
var xmlDocument = WScript.CreateObject("Msxml2.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().value);
}
}
else
WScript.Echo("No file found");
Практика
- Вывести в консоль первую новость (заголовок и содержимое) с http://news.yandex.ru/index.rss
Written on April 13, 2016
