From d6f7bbe4835d03d565b1e276dfea10efbec855a0 Mon Sep 17 00:00:00 2001 From: tunc kiral Date: Tue, 22 Oct 2019 05:45:19 +0600 Subject: [PATCH] getting utils.js --- helper.js | 26 +++++++- querystring.js | 28 --------- utils.js | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 29 deletions(-) delete mode 100644 querystring.js create mode 100644 utils.js diff --git a/helper.js b/helper.js index 411952d..8c5cd29 100644 --- a/helper.js +++ b/helper.js @@ -14,6 +14,31 @@ var helper = { return document.getElementsByClassName(cName); } }, + QueryString: { + build: function (data) { + if (typeof (data) === 'string') return data; + var query = []; + for (var key in data) { + if (data.hasOwnProperty(key)) { + query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); + } + } + return query.join('&'); + }, + getParams: function (url) { + var params = {}; + var parser = document.createElement('a'); + parser.href = url || window.location.href; + var query = parser.search.substring(1); + var vars = query.split('&'); + if (vars.length < 2) return params; + for (var i = 0; i < vars.length; i++) { + var pair = vars[i].split('='); + params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); + } + return params; + } + }, Ajax: { isScriptLoaded: function (src) { var scripts = document.getElementsByTagName("script"); @@ -113,7 +138,6 @@ var helper = { return xhr; } } - }; String.prototype.trim = function () { return this.replace(/^s+|s+$/g, ""); diff --git a/querystring.js b/querystring.js deleted file mode 100644 index faeb5b4..0000000 --- a/querystring.js +++ /dev/null @@ -1,28 +0,0 @@ -helper={ - QueryString: { - build: function (data) { - if (typeof (data) === 'string') return data; - var query = []; - for (var key in data) { - if (data.hasOwnProperty(key)) { - query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); - } - } - return query.join('&'); - }, - getParams: function (url) { - var params = {}; - var parser = document.createElement('a'); - parser.href = url || window.location.href; - var query = parser.search.substring(1); - var vars = query.split('&'); - if (vars.length < 2) return params; - for (var i = 0; i < vars.length; i++) { - var pair = vars[i].split('='); - params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]); - } - return params; - } - } - -}; \ No newline at end of file diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..9c241c2 --- /dev/null +++ b/utils.js @@ -0,0 +1,158 @@ +(function () { + function isScriptLoaded(src) { + var scripts = document.getElementsByTagName("script"); + for (var i = 0; i < scripts.length; i++) + if (scripts[i].getAttribute('src') == src) return true; + return false; + } + + function loadScriptAsync(url, cb, errcb) { + try { + if (dyg.isScriptLoaded(url)) { + cb && cb(); + } else { + var script = document.createElement("script"); + script.type = "text/javascript"; + script.async = false; + if (script.readyState) { + script.onreadystatechange = function () { + if (script.readyState == "loaded" || script.readyState == "complete") { + script.onreadystatechange = null; + cb && cb(); + } + }; + } else { + script.onload = function () { + cb && cb(); + }; + } + script.onerror = function () { + errcb && errcb(); + }; + script.src = url; + (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script); + } + } catch (e) { + console.log(e); + } + } + + function loadCssAsync(url, cb) { + try { + if (isCssLoaded(url)) { + cb && cb(); + } else { + var script = document.createElement("link"); + script.type = "text/css"; + script.rel = "stylesheet"; + + if (script.readyState) { + script.onreadystatechange = function () { + if (script.readyState == "loaded" || script.readyState == "complete") { + script.onreadystatechange = null; + cb && cb(); + } + }; + } else { + script.onload = function () { + cb && cb(); + }; + } + script.href = url; + (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script); + } + } catch (e) { + console.log(e); + } + + } + + function isCssLoaded(src) { + var scripts = document.getElementsByTagName("link"); + for (var i = 0; i < scripts.length; i++) + if (scripts[i].getAttribute('href') == src) return true; + return false; + } + + function checkIsEmpty(value) { + if (value !== undefined && value !== null && value != '' && (typeof value !== 'undefined')) + return false; + return true; + } + + function generateGuid() { + var guid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(o) { + var n = Math.random() * 16 | 0, + g = o == "x" ? n : (n & 3 | 8); + return g.toString(16) + }); + return guid; + } + + function callMethods(objs , i, cb) { + if(objs.length == 0){ cb && cb(); return;} + if (i === objs.length - 1) { + objs[i].meth(objs[i].src, function () { + cb && cb(); + }); + } else { + objs[i].meth(objs[i].src, function () { + callMethods(objs, i + 1, cb); + }); + } + } + + function md5(d){result = M(V(Y(X(d),8*d.length)));return result.toLowerCase()};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n>16)+(_>>16)+(m>>16)<<16|65535&m}function bit_rol(d,_){return d<<_|d>>>32-_} + + function browserNameVersion() { + var n = navigator.appName, ua = navigator.userAgent, temp; + var b = ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i); + if(b && (temp = ua.match(/version\/([\.\d]+)/i)) != null) { + b[2] = temp[1]; + } + b = b ? [b[1], b[2]]: [n, navigator.appVersion,'-?']; + return b; + } + + function getEnviroment(){ + return document.querySelector("meta[property='og:type']").getAttribute('content'); + } + + function fireEvent(eventName, eventData) { + if (!checkIsEmpty(eventName) && !checkIsEmpty(eventData)) { + var event; + if (document.createEvent) { + event = document.createEvent("HTMLEvents"); + event.initEvent(eventName, true, true); + } else { + event = document.createEventObject(); + event.eventType = eventName; + } + event.eventName = eventName; + if (document.createEvent) { + document.dispatchEvent(event); + } else { + var eventType = ''; + if (ieIE) + eventType = "on" + event.eventType + else + eventType = event.eventType; + document.fireEvent(eventType, event); + } + } + } + + window.dyg ={ + isScriptLoaded: isScriptLoaded, + loadScriptAsync:loadScriptAsync, + loadCssAsync:loadCssAsync, + checkIsEmpty:checkIsEmpty, + generateGuid:generateGuid, + callMethods:callMethods, + md5:md5, + browserNameVersion:browserNameVersion, + getEnviroment:getEnviroment, + fireEvent:fireEvent, + version:1.2 + }; +})(); \ No newline at end of file