Add utils
This commit is contained in:
parent
539443ede2
commit
eaa4f436b1
4 changed files with 305 additions and 0 deletions
36
utils/block.js
Normal file
36
utils/block.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
utils.block = {
|
||||||
|
block: function(list, optional) {
|
||||||
|
if (optional == true) {
|
||||||
|
if (random.chance(6)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function go_deeper(item) {
|
||||||
|
if (item == null || item === undefined) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (typeof(item) == "function") {
|
||||||
|
return item();
|
||||||
|
}
|
||||||
|
if (typeof(item) == "string") {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
if (item instanceof(Array)) {
|
||||||
|
var s = "";
|
||||||
|
for (var i = 0; i < item.length; i++) {
|
||||||
|
s += go_deeper(item[i]);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
var asString = "";
|
||||||
|
for (var i = 0; i < list.length; i++) {
|
||||||
|
asString += go_deeper(list[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return asString;
|
||||||
|
}
|
||||||
|
};
|
72
utils/common.js
Normal file
72
utils/common.js
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
utils.common = {
|
||||||
|
objToString: function (obj) {
|
||||||
|
try {
|
||||||
|
return "" + obj
|
||||||
|
} catch (e) {
|
||||||
|
return "[" + e + "]"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getAllProperties: function (obj) {
|
||||||
|
let list = [];
|
||||||
|
while (obj) {
|
||||||
|
list = list.concat(Object.getOwnPropertyNames(obj));
|
||||||
|
obj = Object.getPrototypeOf(obj);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
},
|
||||||
|
getKeysFromHash: function (obj) {
|
||||||
|
let list = [];
|
||||||
|
for (var p in obj) {
|
||||||
|
list.push(p);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
},
|
||||||
|
quote: function (obj) {
|
||||||
|
return JSON.stringify(obj);
|
||||||
|
},
|
||||||
|
shuffle: function (list) {
|
||||||
|
let newArray = list.slice();
|
||||||
|
let len = newArray.length;
|
||||||
|
let i = len;
|
||||||
|
while (i--) {
|
||||||
|
let p = parseInt(Math.random() * len);
|
||||||
|
let t = newArray[i];
|
||||||
|
newArray[i] = newArray[p];
|
||||||
|
newArray[p] = t;
|
||||||
|
}
|
||||||
|
return newArray;
|
||||||
|
},
|
||||||
|
uniqueList: function (list) {
|
||||||
|
let tmp = {}, r = [];
|
||||||
|
for (let i = 0; i < list.length; i++) {
|
||||||
|
tmp[list[i]] = list[i];
|
||||||
|
}
|
||||||
|
for (let i in tmp) {
|
||||||
|
r.push(tmp[i]);
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
},
|
||||||
|
mergeHash: function (obj1, obj2) {
|
||||||
|
for (let p in obj2) {
|
||||||
|
try {
|
||||||
|
if (obj2[p].constructor == Object) {
|
||||||
|
obj1[p] = Utils.mergeHash(obj1[p], obj2[p]);
|
||||||
|
} else {
|
||||||
|
obj1[p] = obj2[p];
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
obj1[p] = obj2[p];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj1;
|
||||||
|
},
|
||||||
|
traceback: function () {
|
||||||
|
Logger.error("===[ Traceback ]");
|
||||||
|
try {
|
||||||
|
throw new Error();
|
||||||
|
} catch (e) {
|
||||||
|
Logger.dump(e.stack || e.stacktrace || "");
|
||||||
|
}
|
||||||
|
Logger.error("===");
|
||||||
|
}
|
||||||
|
};
|
98
utils/platform.js
Normal file
98
utils/platform.js
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
utils.platform = {
|
||||||
|
platform: function () {
|
||||||
|
var version, webkitVersion, platform = {};
|
||||||
|
|
||||||
|
var userAgent = (navigator.userAgent).toLowerCase();
|
||||||
|
var language = navigator.language || navigator.browserLanguage;
|
||||||
|
|
||||||
|
version = platform.version = (userAgent.match(/.*(?:rv|chrome|webkit|opera|ie)[\/: ](.+?)([ \);]|$)/) || [])[1];
|
||||||
|
webkitVersion = (userAgent.match(/webkit\/(.+?) /) || [])[1];
|
||||||
|
platform.windows = platform.isWindows = !!/windows/.test(userAgent);
|
||||||
|
platform.mac = platform.isMac = !!/macintosh/.test(userAgent) || (/mac os x/.test(userAgent) && !/like mac os x/.test(userAgent));
|
||||||
|
platform.lion = platform.isLion = !!(/mac os x 10_7/.test(userAgent) && !/like mac os x 10_7/.test(userAgent));
|
||||||
|
platform.iPhone = platform.isiPhone = !!/iphone/.test(userAgent);
|
||||||
|
platform.iPod = platform.isiPod = !!/ipod/.test(userAgent);
|
||||||
|
platform.iPad = platform.isiPad = !!/ipad/.test(userAgent);
|
||||||
|
platform.iOS = platform.isiOS = platform.iPhone || platform.iPod || platform.iPad;
|
||||||
|
platform.android = platform.isAndroid = !!/android/.test(userAgent);
|
||||||
|
platform.opera = /opera/.test(userAgent) ? version : 0;
|
||||||
|
platform.isOpera = !!platform.opera;
|
||||||
|
platform.msie = /msie/.test(userAgent) && !platform.opera ? version : 0;
|
||||||
|
platform.isIE = !!platform.msie;
|
||||||
|
platform.isIE8OrLower = !!(platform.msie && parseInt(platform.msie, 10) <= 8);
|
||||||
|
platform.mozilla = /mozilla/.test(userAgent) && !/(compatible|webkit|msie)/.test(userAgent) ? version : 0;
|
||||||
|
platform.isMozilla = !!platform.mozilla;
|
||||||
|
platform.webkit = /webkit/.test(userAgent) ? webkitVersion : 0;
|
||||||
|
platform.isWebkit = !!platform.webkit;
|
||||||
|
platform.chrome = /chrome/.test(userAgent) ? version : 0;
|
||||||
|
platform.isChrome = !!platform.chrome;
|
||||||
|
platform.mobileSafari = /apple.*mobile/.test(userAgent) && platform.iOS ? webkitVersion : 0;
|
||||||
|
platform.isMobileSafari = !!platform.mobileSafari;
|
||||||
|
platform.iPadSafari = platform.iPad && platform.isMobileSafari ? webkitVersion : 0;
|
||||||
|
platform.isiPadSafari = !!platform.iPadSafari;
|
||||||
|
platform.iPhoneSafari = platform.iPhone && platform.isMobileSafari ? webkitVersion : 0;
|
||||||
|
platform.isiPhoneSafari = !!platform.iphoneSafari;
|
||||||
|
platform.iPodSafari = platform.iPod && platform.isMobileSafari ? webkitVersion : 0;
|
||||||
|
platform.isiPodSafari = !!platform.iPodSafari;
|
||||||
|
platform.isiOSHomeScreen = platform.isMobileSafari && !/apple.*mobile.*safari/.test(userAgent);
|
||||||
|
platform.safari = platform.webkit && !platform.chrome && !platform.iOS && !platform.android ? webkitVersion : 0;
|
||||||
|
platform.isSafari = !!platform.safari;
|
||||||
|
platform.language = language.split("-", 1)[0];
|
||||||
|
platform.current =
|
||||||
|
platform.msie ? "msie" :
|
||||||
|
platform.mozilla ? "mozilla" :
|
||||||
|
platform.chrome ? "chrome" :
|
||||||
|
platform.safari ? "safari" :
|
||||||
|
platform.opera ? "opera" :
|
||||||
|
platform.mobileSafari ? "mobile-safari" :
|
||||||
|
platform.android ? "android" : "unknown";
|
||||||
|
|
||||||
|
function platformName(candidates) {
|
||||||
|
for (var i = 0; i < candidates.length; i++) {
|
||||||
|
if (candidates[i] in window) {
|
||||||
|
return "window." + candidates[i];
|
||||||
|
}
|
||||||
|
if (candidates[i] in navigator) {
|
||||||
|
return "navigator." + candidates[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
platform.GUM = platformName(['getUserMedia', 'webkitGetUserMedia', 'mozGetUserMedia', 'msGetUserMedia', 'getGUM']);
|
||||||
|
platform.PeerConnection = platformName(['webkitRTCPeerConnection', 'mozRTCPeerConnection', 'msPeerConnection']);
|
||||||
|
platform.IceCandidate = platformName(['mozRTCIceCandidate', 'RTCIceCandidate']);
|
||||||
|
platform.SessionDescription = platformName(['mozRTCSessionDescription', 'RTCSessionDescription']);
|
||||||
|
platform.URL = platformName(['URL', 'webkitURL']);
|
||||||
|
platform.AudioContext = platformName(['AudioContext', 'webkitAudioContext']);
|
||||||
|
platform.OfflineAudioContext = platformName(['OfflineAudioContext', 'webkitOfflineAudioContext']);
|
||||||
|
platform.MediaSource = platformName(["MediaSource", "WebKitMediaSource"]);
|
||||||
|
|
||||||
|
platform.SpeechRecognition = platformName(["SpeechRecognition", "webkitSpeechRecognition"]);
|
||||||
|
platform.SpeechGrammarList = platformName(["SpeechGrammarList", "webkitSpeechGrammarList"]);
|
||||||
|
|
||||||
|
function findWebGLContextName(candidates) {
|
||||||
|
var canvas = document.createElement("canvas");
|
||||||
|
for (var i=0; i<candidates.length; i++) {
|
||||||
|
var name = candidates[i];
|
||||||
|
try {
|
||||||
|
if (canvas.getContext(name)) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
platform.WebGL = "webgl";//findWebGLContextName(["webgl", "experimental-webgl", "webkit-3d"]);
|
||||||
|
platform.WebGL2 = "webgl2";//findWebGLContextName(["webgl2", "experimental-webgl2"]);
|
||||||
|
|
||||||
|
platform.captureStreamUntilEnded = "captureStreamUntilEnded";
|
||||||
|
if (platform.isMozilla) { platform.captureStreamUntilEnded = "mozCaptureStreamUntilEnded"; }
|
||||||
|
|
||||||
|
platform.srcObject = "srcObject";
|
||||||
|
if (platform.isMozilla) { platform.srcObject = "mozSrcObject"; }
|
||||||
|
|
||||||
|
return platform;
|
||||||
|
}
|
||||||
|
};
|
99
utils/script.js
Normal file
99
utils/script.js
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
utils.script = {
|
||||||
|
methodHead: function (list, numOptional) {
|
||||||
|
if (isNaN(numOptional)) {
|
||||||
|
numOptional = 0;
|
||||||
|
}
|
||||||
|
var arity = list.length - Random.number(numOptional);
|
||||||
|
var params = [];
|
||||||
|
for (var i = 0; i < arity; i++) {
|
||||||
|
params.push(Random.pick([list[i]]));
|
||||||
|
}
|
||||||
|
return "(" + params.join(", ") + ")";
|
||||||
|
},
|
||||||
|
methodCall: function (objectName, methodHash) {
|
||||||
|
if(!Utils.getKeysFromHash(methodHash).length || !objectName) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
var methodName = Random.key(methodHash);
|
||||||
|
var methodArgs = methodHash[methodName];
|
||||||
|
if (typeof(methodArgs) == "function") { // Todo: Hmmmm..
|
||||||
|
return methodArgs();
|
||||||
|
}
|
||||||
|
return objectName + "." + methodName + JS.methodHead(methodArgs);
|
||||||
|
},
|
||||||
|
setAttribute: function (objectName, attributeHash) {
|
||||||
|
if(!Utils.getKeysFromHash(attributeHash).length || !objectName) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
var attributeName = Random.key(attributeHash);
|
||||||
|
var attributeValue = Random.pick(attributeHash[attributeName]);
|
||||||
|
var operator = " = ";
|
||||||
|
/*
|
||||||
|
if (typeof(attributeValue) == "number" && Random.chance(8)) {
|
||||||
|
operator = " " + Make.randomAssignmentOperator() + " ";
|
||||||
|
}
|
||||||
|
if (typeof(attributeValue) == "string") {
|
||||||
|
attributeValue = "'" + attributeValue + "'";
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
return objectName + "." + attributeName + operator + attributeValue + ";";
|
||||||
|
},
|
||||||
|
makeConstraint: function (keys, values) {
|
||||||
|
var o = {};
|
||||||
|
var n = Random.range(0, keys.length);
|
||||||
|
while (n--) {
|
||||||
|
o[Random.pick(keys)] = Random.pick(values);
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
},
|
||||||
|
makeRandomOptions: function (base_o) {
|
||||||
|
var o = {}, unique = Random.some(Object.keys(base_o));
|
||||||
|
for (var i = 0; i < unique.length; i++) {
|
||||||
|
o[unique[i]] = Random.pick(base_o[unique[i]]);
|
||||||
|
}
|
||||||
|
return JSON.stringify(o);
|
||||||
|
},
|
||||||
|
safely: function (s) {
|
||||||
|
if (window.debug) {
|
||||||
|
return "try { " + s + " } catch(e) { Logger.JSError(e); }";
|
||||||
|
}
|
||||||
|
return "try { " + s + " } catch(e) { }";
|
||||||
|
},
|
||||||
|
makeLoop: function (s, max) {
|
||||||
|
return "for (var i = 0; i < " + (max || Make.rangeNumber()) + "; i++) {" + s + "}";
|
||||||
|
},
|
||||||
|
makeArray: function (type, arrayLength, cb) {
|
||||||
|
if (type == null || type === undefined) {
|
||||||
|
type = Random.index(["Uint8", "Float32"]);
|
||||||
|
}
|
||||||
|
switch (Random.number(8)) {
|
||||||
|
case 0:
|
||||||
|
var src = "function() { var buffer = new " + type + "Array(" + arrayLength + ");";
|
||||||
|
src += JS.makeLoop("buffer[i] = " + cb() + ";", arrayLength);
|
||||||
|
src += "return buffer;}()";
|
||||||
|
return src;
|
||||||
|
case 1:
|
||||||
|
return "new " + type + "Array([" + Make.filledArray(cb, arrayLength) + "])";
|
||||||
|
default:
|
||||||
|
return "new " + type + "Array(" + arrayLength + ")";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
randListIndex: function (objName) {
|
||||||
|
return Random.number() + ' % ' + o.pick(objName) + '.length';
|
||||||
|
},
|
||||||
|
addElementToBody: function (name) {
|
||||||
|
return "(document.body || document.documentElement).appendChild" + JS.methodHead([name]);
|
||||||
|
},
|
||||||
|
forceGC: function () {
|
||||||
|
if (Platform.isMozilla) {}
|
||||||
|
if (Platform.isChrome) {
|
||||||
|
if (window.GCController)
|
||||||
|
return GCController.collect();
|
||||||
|
}
|
||||||
|
if (Platform.isSafari) {}
|
||||||
|
if (Platform.isIE) {}
|
||||||
|
},
|
||||||
|
getRandomElement: function() {
|
||||||
|
return "document.getElementsByTagName('*')[" + Random.number(document.getElementsByTagName("*").length) + "]";
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in a new issue