diff --git a/utils/block.js b/utils/block.js new file mode 100644 index 0000000..9b5cbc1 --- /dev/null +++ b/utils/block.js @@ -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; + } +}; diff --git a/utils/common.js b/utils/common.js new file mode 100644 index 0000000..a793328 --- /dev/null +++ b/utils/common.js @@ -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("==="); + } +}; diff --git a/utils/platform.js b/utils/platform.js new file mode 100644 index 0000000..f565ef3 --- /dev/null +++ b/utils/platform.js @@ -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