-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq4a.js
387 lines (355 loc) · 11.1 KB
/
jq4a.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*!
* jQuery for Administrators Core
* http://code.google.com/p/jquery-for-admins/
*
* Copyright 2011, 2012 Michael Snead
* Dual licensed under the MIT or GPL Version 2 licenses (just like jQuery).
*
* 06/26/2012
*
* This code is pre-alpha.
*
* Compatible with:
* - Mozilla Rhino
* - Microsoft Windows Scripting Host (JScript)
*/
//Msxml2.XMLHTTP
//http://msdn.microsoft.com/en-us/library/ms535874(v=vs.85).aspx
(function(context) {
var $ = function(selector, context) {
};
/* environment */
$.environment = {};
$.environment.activeX = context.ActiveXObject ? true : false;
$.environment.java = context.java ? context.java == "[JavaPackage java]" : false;
$.environment.android = false;
if($.environment.activeX) {
$.environment.windows = true;
$.environment.mac = false;
$.environment.unix = false;
}
if($.environment.java) {
var osname = java.lang.System.getProperty("os.name").toLowerCase();
$.environment.windows = osname.indexOf("win") >= 0;
$.environment.mac = osname.indexOf("mac") >= 0;
$.environment.unix = osname.indexOf("nix") >= 0 || osname.indexOf("nux") >= 0 || osname.indexOf("sunos") >= 0;
}
/* file */
$.file = {};
$.file.exists = function(path) {
var doesExist = false;
if($.environment.activeX) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
doesExist = fso.FileExists(path);
fso = null;
} else if($.environment.java) {
var file = new java.io.File(path);
doesExist = file.exists();
file = null;
}
return doesExist;
};
/* shell */
$.shell = {};
$.shell.execute = function(cmd) {
var execObject = {};
var isFile = $.file.exists(cmd);
if($.environment.activeX) {
var shell, exec, stdout;
shell = new ActiveXObject("WScript.Shell");
if(isFile) {
exec = shell.Exec(cmd);
stdout = exec.StdOut;
} else {
//assume it's a command
exec = shell.Exec('%comspec% /c ' + cmd);
stdout = exec.StdOut;
}
execObject.readLine = function() {
if(stdout.AtEndOfStream) { return null; } //return null if at the end of stream
return stdout.ReadLine();
};
execObject.destroy = function() {
stdout = null;
exec = null;
shell = null;
};
} else if($.environment.java) {
var p;
if(isFile) {
p = java.lang.Runtime.getRuntime().exec(cmd);
p.waitFor();
var reader = java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
execObject.readLine = function() {
return reader.readLine(); //returns 'null' when at the end of stream
};
} else {
if($.environment.windows) {
//if it's Windows, use "cmd" to execute the system command
var comspec = java.lang.System.getenv("comspec");
p = java.lang.Runtime.getRuntime().exec(comspec + ' /c ' + cmd);
} else {
//some other OS, hopefully it will interpret the system command
p = java.lang.Runtime.getRuntime().exec(cmd);
}
var reader = java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
execObject.readLine = function() {
return reader.readLine(); //returns 'null' when at the end of stream
};
}
execObject.destroy = function() {
p.destroy();
};
}
return execObject;
};
var trimLeft = /^\s+/;
var trimRight = /\s+$/;
/* echo */
$.echo = function(echoStr) {
if($.environment.activeX) { WScript.Echo(echoStr); }
if($.environment.java) { print(echoStr); }
}
/* tee */
$.tee = function(cmdStr, itFunc, echoIt) {
var execObj = $.shell.execute(cmdStr);
var buff = execObj.readLine();
while(buff != null) {
if(echoIt === undefined || echoIt === true) {
$.echo(buff);
if(itFunc) { itFunc(buff); }
} else {
if(itFunc) { itFunc(buff); }
}
buff = execObj.readLine();
};
}
/* each */
$.each = function(axObj, itFunc) {
var itemCount = 0;
if(Object.prototype.toString.call(axObj)==='[object Array]'){
for(var i=0;i<axObj.length;i++){ //It's an array, loop through it
itFunc(axObj[i],i);
}
} else {
if(!(axObj instanceof Object)) { //It's not a JS object, probably ActiveX
var e = new Enumerator(axObj);
e.moveFirst();
while(e.atEnd() == false) {
itFunc(e.item(),itemCount);
e.moveNext();
itemCount++;
}
} else {
for(var x in axObj) {
itFunc(x,itemCount);
itemCount++;
}
}
}
};
/* stringWriter */
var StringWriter = function() {};
StringWriter.prototype.Write = function(wobj) {
if($.environment.activeX) {
this.axObject = this.axObject || new ActiveXObject("System.IO.StringWriter");
this.axObject.Write_13(wobj);
} else if($.environment.java) {
this.javaObject = this.javaObject || new java.lang.StringBuilder();
this.javaObject.append(wobj);
} else {
this.rawString = this.rawString || "";
this.rawString += wobj;
}
}
StringWriter.prototype.WriteLine = function(wobj) {
if($.environment.activeX) {
this.axObject = this.axObject || new ActiveXObject("System.IO.StringWriter");
this.axObject.WriteLine_13(wobj);
} else if($.environment.java) {
this.javaObject = this.javaObject || new java.lang.StringBuilder();
this.javaObject.append(wobj + "\r\n");
} else {
this.rawString = this.rawString || "";
this.rawString += wobj + "\r\n";
}
}
StringWriter.prototype.ToString = function(wargs) {
if($.environment.activeX) {
this.axObject = this.axObject || new ActiveXObject("System.IO.StringWriter");
return wargs ? this.axObject.ToString(wargs) : this.axObject.ToString();
} else if($.environment.java) {
this.javaObject = this.javaObject || new java.lang.StringBuilder();
return this.javaObject.toString();
} else {
this.rawString = this.rawString || "";
return this.rawString;
}
}
StringWriter.prototype.isNative = $.environment.activeX || $.environment.java;
//System.Collections.ArrayList <-- use $.each to enumerate
// Add,Sort,Reverse,Remove
$.stringWriter = StringWriter;
/* echoCmd */
$.echoCmd = function(cmdStr) {
var execObj = $.shell.execute(cmdStr);
var buff = execObj.readLine();
while(buff != null) {
$.echo(buff);
buff = execObj.readLine();
};
};
$.reflectObject = function(obj) {
var retStr = "";
retStr += "prototype: " + Object.prototype.toString.call(obj) + "\r\n";
for(var x in obj) {
retStr += x + ": " + obj[x] + "\r\n";
}
return retStr;
}
/* ajax */
$.ajax = function(aOpts) {
var aType = ((aOpts.type ? aOpts.type : "GET") + '').toUpperCase();
if($.environment.activeX) {
try {
var aXML = new ActiveXObject("Msxml2.XMLHTTP");
if(aOpts.username) {
aXML.Open(aType, aOpts.url, aOpts.async === true, aOpts.username + '', aOpts.password + '');
} else {
aXML.Open(aType, aOpts.url, aOpts.async === true);
}
aXML.Send();
if(aOpts.success) { aOpts.success(aXML.responseText); }
else { return aXML.responseText; }
} catch(e) {
$.echo($.reflectObject(e));
if(aOpts.error) { aOpts.error(e); }
}
} else if($.environment.java) {
if(aOpts.async === true) { throw "Asynchronous AJAX requests are not supported under Rhino."; }
if(aType === "POST") {
var data = "", dataElement, url, conn, wr, rd, line, responseText = "";
if(aOpts.data != undefined && aOpts.data != "") {
for(var x in aOpts.data) {
dataElement = java.net.URLEncoder.encode(x, "UTF-8") + "=" + java.net.URLEncoder.encode(aOpts.data[x], "UTF-8");
data = data === "" ? dataElement : data + "&" + dataElement;
}
}
url = new java.net.URL(aOpts.url);
conn = url.openConnection();
conn.setDoOutput(true);
wr = new java.io.OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
rd = java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
line = rd.readLine();
while(line != null) {
responseText += responseText === "" ? line : "\r\n" + line;
line = rd.readLine();
}
wr.close();
rd.close();
if(aOpts.success) { aOpts.success(responseText); }
else { return responseText; }
} else {
try {
var responseText = readUrl(aOpts.url);
if(aOpts.success) { aOpts.success(responseText); }
else { return responseText; }
} catch(e) {
if(aOpts.error) { aOpts.error(e); }
}
}
}
}
/* reboot */
$.reboot = function() {
if($.environment.activeX) {
var opsys = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true");
for(var enumItems = new Enumerator(opsys); !enumItems.atEnd(); enumItems.moveNext())
{
enumItems.item().Reboot();
}
} else if($.environment.java) {
if($.environment.windows) {
var sysroot = java.lang.System.getenv("%SystemRoot%");
$.shell.execute(sysroot + "\\shutdown.exe -r -t 00 -f");
} else if($.environment.android) {
//seems this requires superuser
//http://stackoverflow.com/questions/5484535/runtime-exec-reboot-in-android
} else if($.environment.unix) {
$.shell.execute('reboot');
}
}
}
/* shutdown */
$.shutdown = function() {
if($.environment.activeX) {
var opsys = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true");
for(var enumItems = new Enumerator(opsys); !enumItems.atEnd(); enumItems.moveNext())
{
enumItems.item().Shutdown();
}
} else if($.environment.java) {
if($.environment.windows) {
var sysroot = java.lang.System.getenv("%SystemRoot%");
$.shell.execute(sysroot + "\\shutdown.exe -r -t 00 -f");
} else if($.environment.android) {
//seems this requires superuser
//http://stackoverflow.com/questions/5484535/runtime-exec-reboot-in-android
} else if($.environment.unix) {
$.shell.execute('poweroff');
}
}
}
/* base64Encode */
$.base64Encode = function(input) {
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
while(i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if(isNaN(chr2)) {
enc3 = enc4 = 64;
} else if(isNaN(chr3)) {
enc4 = 64;
}
output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
};
/* trim */
$.trim = String.prototype.trim ?
function(text) {
return text == null ?
"" :
text.trim();
} :
function(text) {
return text == null ?
"" :
text.toString().replace(trimLeft,"").replace(trimRight,"");
};
/* contains */
$.contains = function(x, it) {
if(Object.prototype.toString.call(it)==='[object Array]'){
for(var i=0;i<it.length;i++){ //It's an array, loop through it
if(x.indexOf(it[i]) != -1)
{ return true; }
}
} else if(Object.prototype.toString.call(it)==='[object String]') {
return x.indexOf(it) !== -1;
} else {
throw "Invalid object (not an array or a string)";
}
return false;
}
context.$ = $;
})(this);