-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoLogViewer.hta
207 lines (195 loc) · 6.88 KB
/
AutoLogViewer.hta
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<html>
<head>
<meta charset="UTF-8"/>
<meta http-equiv="MSThemeCompatible" content="yes">
<title>Auto Log Viewer</title>
<script type="text/javascript">
var fso = new ActiveXObject("Scripting.FileSystemObject");
var wsh = new ActiveXObject("WScript.Shell");
var files = [];
var prevFiles = { target: '', files: [] };
function init() {
updateFileList();
setInterval('updateFileList()', 60 * 1000);
}
function formatDate(d) {
return (d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate() + ' ' + d.getHours() + ':'
+ d.getMinutes() + ':' + d.getSeconds()).replace(/(\D)(\d)(\b|(?=\D))/g, "$10$2");
}
function figureNumber(value) {
var num = '' + value;
while (num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
return num;
}
function deleteFileByPost(filePath) {
fso.DeleteFile(filePath);
}
function updateFileList() {
var targetDir = "TEMP";
var TemporaryFolder = 2;
var tfolder = fso.GetSpecialFolder(TemporaryFolder);
if (document.getElementById('place_radio_desktop').checked) {
var desktopPath = wsh.SpecialFolders('Desktop');
tfolder = fso.GetFolder(desktopPath);
targetDir = "Desktop"
}
files = [];
for (var enumFiles = new Enumerator(tfolder.Files)
; ! enumFiles.atEnd(); enumFiles.moveNext()) {
var f = enumFiles.item();
var fd = {
name: f.Name,
path: f.Path,
size: f.Size,
dateLastModified: f.DateLastModified
};
files.push(fd);
}
files.sort(function (a, b) { return b.dateLastModified - a.dateLastModified; });
var filelistDiv = document.getElementById('filelist');
for (var i = filelistDiv.childNodes.length - 1; i >= 0; i--) {
filelistDiv.removeChild(filelistDiv.childNodes.item(i));
}
var updateTimeLabelSpan = document.getElementById('update_time_label');
updateTimeLabelSpan.innerHTML = new Date().toString();
var filterRexexText = document.getElementById('filterText').value;
filterRexexText = filterRexexText.replace(/^\s+|\s+$/g, "");
var regexp;
if (filterRexexText === "") {
regexp = /.*/;
} else {
regexp = new RegExp(filterRexexText, "i");
}
for (var i in files) {
var f = files[i];
if (!f.name.match(regexp)) {
continue;
}
filelistDiv.appendChild(document.createTextNode(formatDate(new Date(f.dateLastModified)) + " "));
var a = document.createElement('a');
a.setAttribute('href', '');
a.attachEvent('onclick', new Function('showLogFile(' + i + '); return false;'));
a.appendChild(document.createTextNode(f.name));
filelistDiv.appendChild(a);
filelistDiv.appendChild(document.createTextNode(' (' + figureNumber(f.size) + ')'));
filelistDiv.appendChild(document.createElement('br'));
}
if (targetDir === prevFiles.target) {
var newFileIndexList = [];
for (var i in files) {
var f1 = files[i];
var found = false;
for (var j in prevFiles.files) {
var f2 = prevFiles.files[j];
if (f1.name === f2.name) {
found = true;
break;
}
}
if (!found) {
if (f1.name.match(regexp)) {
newFileIndexList.push(i);
}
}
}
if (document.getElementById('auto_spawn_tailf_checkbox').checked) {
for (var i in newFileIndexList) {
showLogFile(newFileIndexList[i]);
}
}
}
prevFiles = {target: targetDir, files: files};
}
function showLogFile(index) {
var TemporaryFolder = 2;
var tfolder = fso.GetSpecialFolder(TemporaryFolder);
var tailfPath = tfolder.Path + "\\tailf_" + fso.GetTempName() + ".js";
var fout = fso.CreateTextFile(tailfPath);
fout.WriteLine(__tailf_main.toString());
fout.WriteLine('__tailf_main();');
fout.Close();
var f = files[index];
var cmd = 'cmd /c title "' + f.name + '" & cscript //nologo "' + tailfPath + '" "' + f.path + '"';
wsh.Run(cmd);
// Delete "tailf.js" after 10 seconds.
setTimeout("deleteFileByPost('" + tailfPath.replace(/\\/g, "\\\\") + "')", 10 * 1000);
}
window.attachEvent('onload', init);
/// for tailf.js
function __tailf_main() {
var args = WScript.Arguments;
if (args.Length < 1) {
return;
}
var filePath = args(0);
var isUTF16 = false;
var isUTF8 = false;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileSize = fso.GetFile(filePath).Size;
// UTF-8 or UTF-16 or ANSI
var stream = new ActiveXObject("ADODB.Stream");
var adTypeText = 2;
stream.Type = adTypeText;
stream.Charset = 'iso-8859-1';
stream.Open();
stream.LoadFromFile(filePath);
stream.Position = 0;
if (fileSize >= 2) {
var head = stream.ReadText(2);
if (head === "\xFF\xFE") {
isUTF16 = true;
} else if (head === "\xEF\xBB") {
if (fileSize >= 3) {
var head2 = stream.ReadText(1);
if (head2 === "\xBF") {
isUTF8 = true;
}
}
}
}
stream.Close();
var ForReading = 1;
var TristateUseDefault = -2;
var TristateUseTrue = -1;
var TristateUseFalse = 0;
var format = isUTF16 ? TristateUseTrue : TristateUseFalse;
var f = fso.OpenTextFile(filePath, ForReading, false, format);
var stopCounter = 0;
while (true) {
if (f.AtEndOfStream) {
stopCounter++;
var sleepTime = 1000;
if (stopCounter >= 120) {
stopCounter = 120;
sleepTime = 10000;
}
WScript.Sleep(sleepTime);
} else {
var line = f.ReadLine();
WScript.StdOut.WriteLine(line);
}
}
f.Close();
}
///
</script>
</head>
<body>
<form id="form1">
Filter: <input type="text" id="filterText" size="80" value="(\.log$)|(\.txt$)" /><br />
<input type="radio" name="place" id="place_radio_temp" value="temp" checked onclick="updateFileList()">TEMP<br>
<input type="radio" name="place" id="place_radio_desktop" value="desktop" onclick="updateFileList()">Desktop<br>
<!--
<input type="radio" name="place" value="other">: <input type="text" name="p"><br>
-->
<input type="checkbox" id="auto_spawn_tailf_checkbox" checked> Auto<br />
<hr>
<input type="button" id="updateButton" value="Update File List" onclick="updateFileList()">
<span id="update_time_label">Update Time Label</span>
</form>
<div id="filelist">
<a id="f1" href="#" onclick="showLogFile()">file 1</a><br>
<a id="f2" href="#" onclick="showLogFile()">file 2</a><br>
</div>
</body>
</html>