JsUtils

JavaScript Utility functions

目前為 2016-04-25 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greatest.deepsurf.us/scripts/19117/121894/JsUtils.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

(function(a){a.fn.extend({sortList:function(){return function(){if(0!==this.length){if(!this.is("ul, ol"))throw"Error: sortList can only be used on a ul or ol tag!";var b=arguments,c=!1;if("undefined"!==typeof b[0]){if("boolean"!==typeof b[0])throw"the first argument must be a boolean";c=b[0]}for(var d=[],e=0;e<this.length;e++){var f=a(this.get(e)),g=f.children("li").not(function(c,e){for(c=1;c<b.length;c++){var f=b[c];if(a.trim(a(this).text())===f)return d.push(a(this)),!0}return!1}).get();g.sort(function(c,
b){return a(c).text().toUpperCase().localeCompare(a(b).text().toUpperCase())});a.each(g,function(c,a){f.append(a)});!0===c&&a.each(d,function(c,b){a(this).parent().append(this)})}return this}}.apply(this,arguments)},sortSelect:function(){if(!this.is("select"))throw"SortSelect can only be used on a select node";var b=[];a.each(this.children(),function(c,d){"string"===typeof a(this).data("sort-ignore")&&b.push(this)});var c=this.children("option").not(function(c){return 0<=a.inArray(this,b)?!0:!1});
c.sort(function(c,b){return a(c).text().toUpperCase().localeCompare(a(b).text().toUpperCase())});for(var d=0;d<c.length;d++)this.append(c[d]);return this},center:function(){this.css("position","absolute");this.css("top",Math.max(0,(a(window).height()-a(this).outerHeight())/2+a(window).scrollTop())+"px");this.css("left",Math.max(0,(a(window).width()-a(this).outerWidth())/2+a(window).scrollLeft())+"px");return this},sortTable:function(b){"undefined"===typeof b&&(b=-1);if(0!==this.length){if("number"!==
typeof b)throw"col must be of type number. Got type"+typeof b+" instead";if(!this.is("table"))throw"Error: sortList can only be used on a table!";var c=a(this).find("tbody").children("tr").get();-1===b?c.sort(function(c,b){return a(c).text().toUpperCase().localeCompare(a(b).text().toUpperCase())}):c.sort(function(c,d){var g=a(c).children("td").eq(b).text().toUpperCase(),h=a(d).children("td").eq(b).text().toUpperCase();return g.localeCompare(h)});for(var d=0;d<c.length;d++)a(this).find("tbody").append(c[d]);
return this}},changetext:function(a){if(0!==this.length)return this.contents().filter(function(){return 3===this.nodeType})[0].nodeValue=a,this},enableButton:function(a){if(0!==this.length){if(!this.is("a, button"))throw"This function may only be used on a button or anchor tag";if("undefined"===typeof a||null===a)throw"The argument passed to this function must be a boolean";!0===a?this.prop("disabled",!1).removeClass("disabled"):this.prop("disabled",!0).addClass("disabled");return this}}});(function(){(function(){var b=
jQuery.fn.hide;a.fn.hide=function(){b.apply(this,arguments);this.is(":hidden")||this.addClass("hide");return this}})();(function(){var b=jQuery.fn.show;a.fn.show=function(){b.apply(this,arguments);var c;c=this.prop("nodeName");var a;c=document.createElement(c);a="getComputedStyle"in window;document.body.appendChild(c);a=(a?window.getComputedStyle(c,""):c.currentStyle).display;document.body.removeChild(c);c=a;this.is(":hidden")&&this.removeClass("hide").css("display",c);return this}})()})();(function(){(function(){Object.defineProperty(jQuery.expr.filters,
"offscreen",{value:function(b){b=a(b);var c=a(window),d=c.scrollTop(),e=c.scrollLeft(),f;f=e+c.width();var c=d+c.height(),g=b.offset();g.right=g.left+b.outerWidth();g.bottom=g.top+b.outerHeight();return f<g.left||e>g.right||c<g.top||d>g.bottom}})})()})();(function(){(function(){var a=jQuery.fn.addClass;jQuery.fn.addClass=function(){var c=a.apply(this,arguments);jQuery(this).trigger("cssClassChanged");return c}})()})()})(jQuery);
var ObjectUtil=function(){var a=function(c){return c instanceof jQuery||"jquery"in Object(c)?!0:!1},b=function(){if(null==arguments||0===arguments.length)return!1;for(var c=0;c<arguments.length;c++){var a=arguments[c];if(void 0===a||null===a||0===a.length||"string"!==typeof a||0===$.trim(a).length)return!1}return!0};return{isjQuery:a,validString:function(){return b.apply(this,arguments)},extendObj:function(c,a){if("function"!==typeof a)throw Error("sub must be a Constructor");if("function"!==typeof c)throw Error("base must be a Constructor");
a.prototype=Object.create(c.prototype);return a.prototype.constructor=a},stringContains:function(c,a){return 0>~c.indexOf(a)},getElementFromJqueryArray:function(c,b){if(!a(c))throw new TypeError("element must be an instance of Jquery");if("number"!==typeof b)throw new TypeError("element must be a number");return c.filter(function(c){return c===b})},deepCompare:function(){function c(a,b){var e;if(isNaN(a)&&isNaN(b)&&"number"===typeof a&&"number"===typeof b||a===b)return!0;if("function"===typeof a&&
"function"===typeof b||a instanceof Date&&b instanceof Date||a instanceof RegExp&&b instanceof RegExp||a instanceof String&&b instanceof String||a instanceof Number&&b instanceof Number)return a.toString()===b.toString();if(!(a instanceof Object&&b instanceof Object)||a.isPrototypeOf(b)||b.isPrototypeOf(a)||a.constructor!==b.constructor||a.prototype!==b.prototype||-1<f.indexOf(a)||-1<g.indexOf(b))return!1;for(e in b)if(b.hasOwnProperty(e)!==a.hasOwnProperty(e)||typeof b[e]!==typeof a[e])return!1;
for(e in a){if(b.hasOwnProperty(e)!==a.hasOwnProperty(e)||typeof b[e]!==typeof a[e])return!1;switch(typeof a[e]){case "object":case "function":f.push(a);g.push(b);if(!c(a[e],b[e]))return!1;f.pop();g.pop();break;default:if(a[e]!==b[e])return!1}}return!0}var a,b,f,g;if(1>arguments.length)return!0;a=1;for(b=arguments.length;a<b;a++)if(f=[],g=[],!c(arguments[0],arguments[a]))return!1;return!0},guid:function(){function a(){return Math.floor(65536*(1+Math.random())).toString(16).substring(1)}return a()+
a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()}}}(),DomUtil=function(){return{injectCss:function(a){(new RegExp(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)).test(a)?$("<link>").prop({type:"text/css",rel:"stylesheet"}).attr("href",a).appendTo("head"):$("<style>").prop("type","text/css").html(a).appendTo("head")}}}(),XmlUtil=function(){return{encodeXml:function(a){if("string"===typeof a)return a.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,
"&apos;")},decodeXml:function(a){if("string"===typeof a)return a.replace(/&apos;/g,"'").replace(/&quot;/g,'"').replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/&amp;/g,"&")}}}(),XssEncoder=function(a){return a}(XmlUtil||{}),fileUtil=function(){var a=function(){return window.File&&window.FileReader&&window.FileList&&window.Blob?!0:!1},b=function(a){ObjectUtil.isjQuery(a)&&(a=a.val());a=a.substr(a.lastIndexOf(".")+1);if(""===a)throw"No file extension";return a.toLowerCase()},c=function(a,c){if(0==
a)return"0 Byte";var b=Math.floor(Math.log(a)/Math.log(1024));return(a/Math.pow(1024,b)).toPrecision(c+1||3)+" "+"Bytes KB MB GB TB PB EB ZB YB".split(" ")[b]},d=function(){return{getImageInfoFromInput:function(e,d){function g(a,b){var e=new FileReader;e.addEventListener("load",function(){var d=new Image;d.addEventListener("load",function(){var f={};Object.defineProperties(f,{fileName:{value:a.name},imageWidth:{value:d.width},imageHeight:{value:d.height},imageType:{value:a.type},fileSize:{value:c(a.size)},
base64:{value:e.result}});Object.freeze(f);b.resolve(f,this)});d.src=h?window.URL.createObjectURL(a):e.result;h&&window.URL.revokeObjectURL(a)});e.readAsDataURL(a);return b.promise()}if(a()){if(ObjectUtil.isjQuery(e)){if(0===e.length)throw"Input does not exist";e=e[0]}BrowserUtils.isChrome()&&(window.URL=window.webkitURL);var h=window.URL,k=e.files;if(!k)throw new TypeError("File upload not supported by your browser.");if(0<k.length)if(k=k[0],/\.(png|jpg|gif)$/i.test(k.name)){var l=$.Deferred();$.when(g(k,
l)).done(function(a,c){d.call(c,a,c)})}else throw new ImageBoundException("Unsupported Image extension: '"+b(k.name)+"' file name: '"+k.name+"'");}}}}();return{getFileExt:b,getFileNameFromInput:function(c){var b=null;if(ObjectUtil.isjQuery(c)){if(0===c.length)throw"Input does not exist";c=c[0]}if(null===c)throw"Input does not exist";if("url"===c.getAttribute("type")||!a()){c=c.value;if("string"!==typeof c)throw"path does not contain a value, this maybe because you did not pass in an object that represents an input";
return c.replace(/^C:\\fakepath\\/i,"").split("\\").pop().split("/").pop()}b=c.files[0];return b.name},supportsFileApi:a,ImageUtils:d,b64toBlob:function(a,c,b){c=c||"";b=b||512;a=atob(a);for(var d=[],k=0;k<a.length;k+=b){for(var l=a.slice(k,k+b),n=Array(l.length),m=0;m<l.length;m++)n[m]=l.charCodeAt(m);l=new Uint8Array(n);d.push(l)}return new Blob(d,{type:c})}}}(),dragDrop=function(a,b){return{fileUtil:a,canSupportDragAndDrop:function(){return a.supportsFileApi()}}}(fileUtil||{},window),ArrayUtils=
function(){return{removeFromArray:function(a,b){console&&console.warn("The use of 'removeFromArray' is depricated, please use 'yourArray._remove_(item)' instead");for(var c=a.length;c--;)ObjectUtil.isjQuery(b)?$(a[c]).is(b)&&a.splice(c,1):a[c]===b&&a.splice(c,1)},arrayCopy:function(a,b){if(!Array.isArray(a))throw new TypeError("array to copy must be of type 'Array'");return $.extend("undefined"===typeof b?!1:b,[],a)}}}(),ColourUtils=function(){var a=Math.random();return{getRandomColour:function(){a+=
.618033988749895;var b=a%=1,c=parseInt(6*b),d=6*b-c,b=.75*(1-.7),e=.75*(1-.7*d),d=.75*(1-.7*(1-d)),f,g,h;0==c?(f=.75,g=d,h=b):1==c?(f=e,g=.75,h=b):2==c?(f=b,g=.75,h=d):3==c?(f=b,g=e,h=.75):4==c?(f=d,g=b,h=.75):5==c&&(f=.75,g=b,h=e);return"rgb("+parseInt(256*f)+","+parseInt(256*g)+","+parseInt(256*h)+")"}}}(),BrowserUtils=function(){var a=function(){var a=function(){var a=window.navigator.userAgent,b=a.indexOf("Edge/");return 0<b?parseInt(a.substring(b+5,a.indexOf(".",b)),10):null};return{getVersion:a,
isEdge:function(){return null!==a()}}}();return{detectMobile:function(){var a=!1,c=navigator.userAgent||navigator.vendor||window.opera;if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(c)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(c.substr(0,
4)))a=!0;return a},isChrome:function(){return null!==window.chrome&&"Google Inc."===window.navigator.vendor?!0:!1},isFireFox:function(){return-1<navigator.userAgent.toLowerCase().indexOf("firefox")?!0:!1},isWebkit:function(){return/webkit/.test(navigator.userAgent.toLowerCase())},isEdgeOrIe:function(){return a.isEdge()||IeUtils.isIe()},Edge:a}}(),IeUtils=function(){var a=function(){return-1!==navigator.appVersion.indexOf("MSIE 10")},b=function(){return!!navigator.userAgent.match(/Trident.*rv[ :]*11\./)},
c=function(){return 0<window.navigator.userAgent.indexOf("MSIE ")|b()||a()?!0:!1};return{isIe:c,getIeVersion:function(){if(!c())return null;var d="unknown",e={"5.5":5.5,"5.6":6,"5.7":7,"5.8":8,9:9},f=(new Function("/*@cc_on return @_jscript_version; @*/"))();"undefined"!==typeof f?(d=e[f],"undefined"===typeof d&&(d=a()?10:b()?11:-1!=navigator.appVersion.indexOf("MSIE 9.")?9:"unknown")):a()?d=10:b()&&(d=11);return d}}}(),VersionUtil=function(){return{isHigherVersion:function(a,b){if("string"!==typeof a&&
"string"!==typeof b)throw"versions must be strings";for(var c=a.split("."),d=b.split("."),e=c.length>d.length?d.length:c.length,f=0;f<e;f++){var g=parseInt(c[f]),h=parseInt(d[f]);if(g>h)return!0;if(h>g)return!1}return c.length>=d.length}}}(),QueryString=function(){for(var a={},b=window.location.search.substring(1).split("&"),c=0;c<b.length;c++){var d=b[c].split("=");"undefined"===typeof a[d[0]]?a[d[0]]=d[1]:"string"===typeof a[d[0]]?a[d[0]]=[a[d[0]],d[1]]:a[d[0]].push(d[1])}return a}();
(function(){function a(a){var b=1;"-"==a[0]&&(b=-1,a=a.substr(1));return function(e,f){if("undefined"===typeof e[a]||"undefined"===typeof f[a])throw"There is no property with the value of "+a.toString()+" inside this object";return e[a].toUpperCase().localeCompare(f[a].toUpperCase())*b}}function b(){if(0==arguments.length)throw"Zero length arguments not allowed for Array.sortBy()";var c=arguments;return function(b,e){for(var f=0,g=0;0==f&&g<c.length;g++)f=a(c[g])(b,e);return f}}Object.defineProperty(Array.prototype,
"sortBy",{enumerable:!1,writable:!0,value:function(){return this.sort(b.apply(null,arguments))}})})();
(function(){Array.prototype._remove_||(Object.defineProperty(Array.prototype,"_remove_",{enumerable:!1,value:function(a){var b=this.length;try{var c=null,c=JSON.stringify(a)}catch(f){}for(;b--;){var d=this[b],e=null;if(null!==c)try{e=JSON.stringify(d)}catch(f){}ObjectUtil.isjQuery(a)?$(d).is(a)&&this.splice(b,1):null!==e&&null!==c&&e===c?this.splice(b,1):ObjectUtil.deepCompare(a,d)&&this.splice(b,1)}}}),Array.prototype.moveItem||Object.defineProperty(Array.prototype,"moveItem",{value:function(a,b){for(;0>
a;)a+=this.length;for(;0>b;)b+=this.length;if(b>=this.length)for(var c=b-this.length;c--+1;)this.push(void 0);this.splice(b,0,this.splice(a,1)[0])}}))})();(function(){Object.defineProperty(Function.prototype,"getName",{value:function(){try{return/^function\s+([\w\$]+)\s*\(/.exec(this.toString())[1]}catch(a){return null}}})})();
var LinkedArray=function(){var a=function c(a){if("undefined"===typeof a)throw new TypeError("a type must be supplied");var e=[];Object.setPrototypeOf(e,c.prototype);Object.defineProperty(e,"_type",{value:a});return e};a.prototype=[];a.prototype.push=function(){for(var a=1===arguments.length?[arguments[0]]:Array.apply(null,arguments),d=0;d<a.length;d++){var e=a[d];if(Array.isArray(this._type)){if(!Array.isArray(e))throw new TypeError("this array only accepts instance of arrays");}else if("object"===
typeof this._type||"function"===typeof this._type){if("undefined"===typeof e)break;if(!(e instanceof this._type)){a="This array only accepts instance of {supplied object}";if(""!==this._type.name)a="This array only accepts instance of "+this._type.name;else try{a="This array only accepts instance of "+this._type.getName()}catch(f){}throw new TypeError(a);}}else{if("number"===typeof this._type&&"number"!==typeof e)throw new TypeError("this array only accepts "+typeof this._type);if("boolean"===typeof this._type&&
"boolean"!==typeof e)throw new TypeError("this array only accepts "+typeof this._type);if("string"===typeof this._type&&"string"!==typeof e)throw new TypeError("this array only accepts "+typeof this._type);}}a.forEach(function(a,c,d){Array.prototype.push.call(this,a)},this)};a.prototype.getType=function(){return"function"===typeof this._type?new this._type:"object"===typeof this._type?this._type:typeof this._type};return a}(),ImageBoundException=function(){function a(a){this.name="ImageBoundException";
this.message=a||"An error occured with this image";this.stack=Error().stack}a.prototype=Object.create(Error.prototype);return a.prototype.constructor=a}();