Prot.js

Helps neaten code without being a heavy library

目前為 2014-05-28 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/1780/4456/Protjs.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Prot.js
// @namespace   http://mdev.me/
// @description Helps neaten code without being a heavy library
// @include     http://*stackoverflow.com/questions/*
// @version     2
// @grant       none
// ==/UserScript==
Arr=function(b){this.a=b};Arr.prototype=new Array;Arr.prototype._ret=function(a){return this._retWrap&&a instanceof Array?A(a).wrapAll():a};Arr.prototype._retWrap=false;Arr.prototype.concat=function(){return this.a.concat.apply(this.a,arguments)};Arr.prototype.contains=function(a){return this.a.indexOf(a)>-1};Arr.prototype.every=function(d,b){b=b||this.a;if(typeof this.a.every=="function"){return this.a.every.apply(b,arguments)}else{for(var a=0,e=this.a.length;a<e;a++){if(d.call(b,this.a[a],a,this.a)!==true){return false}}return true}};Arr.prototype.filter=function(d,b){b=b||this.a;if(typeof this.a.filter=="function"){return this._ret(this.a.filter.apply(b,arguments))}else{var f=[];for(var a=0,e=this.a.length;a<e;a++){if(d.call(b,this.a[a],a,this.a)===true){f.push(this.a[a])}}return this._ret(f)}};Arr.prototype.forEach=function(b,a){a=a||this.a;if(typeof this.a.forEach=="function"){this.a.forEach.apply(a,arguments)}else{this.loop(b,a)}};Arr.prototype.getUnique=function(){var b=[];this.loop(function(a){if(b.indexOf(a)>-1){return}b.push(a)});return this._ret(b)};Arr.prototype.has=Arr.prototype.contains;Arr.prototype.indexOf=function(c){if(typeof this.a.indexOf=="function"){return this.a.indexOf.apply(this.a,arguments)}else{if(this===null){throw new TypeError()}var e=0,b,d=new Object(this.a),a=d.length>>>0;if(a===0){return -1}if(arguments.length>1){e=Number(arguments[1]);if(e!==e){e=0}else{if(e!==0&&e!==Infinity&&e!==-Infinity){e=(e>0||-1)*Math.floor(Math.abs(e))}}}if(e>=a){return -1}for(b=e>=0?e:Math.max(a-Math.abs(e),0);b<a;b++){if(d[b]&&d[b]===c){return b}}return -1}};Arr.prototype.join=function(){return this.a.join.apply(this.a,arguments)};Arr.prototype.lastIndexOf=function(c){if(typeof this.a.lastIndexOf=="function"){return this.a.lastIndexOf.apply(this.a,arguments)}else{if(this===null){throw new TypeError()}var e,b,d=new Object(this.a),a=d.length>>>0;if(a===0){return -1}e=a;if(arguments.length>1){e=Number(arguments[1]);if(e!==e){e=0}else{if(e!==0&&e!==Infinity&&e!==-Infinity){e=(e>0||-1)*Math.floor(Math.abs(e))}}}for(b=e>=0?Math.min(e,a-1):a-Math.abs(e);b>=0;b--){if(d[b]&&d[b]===c){return b}}return -1}};Arr.prototype.loop=function(e,d){for(var a=0,f=this.a.length;a<f;a++){var b=null;if(d){b=e.apply(d,[this.a[a],a,this.a])}else{b=e(this.a[a],a,this.a)}if(typeof b!="undefined"){if(b=="__continue"){continue}else{return b}}}return};Arr.prototype.map=function(e,d){d=d||this.a;if(typeof this.a.map=="function"){this.a=this.a.map.apply(d,arguments);return this._ret(this.a)}else{var a=this.a;for(var b=0,f=this.a.length;b<f;b++){this.a[b]=e.call(d,this.a[b],b,a)}return this._ret(this.a)}};Arr.prototype.pop=function(){return this.a.pop.apply(this.a,arguments)};Arr.prototype.push=function(){return this.a.push.apply(this.a,arguments)};Arr.prototype.reverse=function(){return this._ret(this.a.reverse.apply(this.a,arguments))};Arr.prototype.shift=function(){return this.a.shift.apply(this.a,arguments)};Arr.prototype.slice=function(){return this._ret(this.a.slice.apply(this.a,arguments))};Arr.prototype.some=function(d,b){b=b||this.a;if(typeof this.a.some=="function"){return this.a.some.apply(b,arguments)}else{for(var a=0,e=this.a.length;a<e;a++){if(d.call(b,this.a[a],a,this.a)===true){return true}}return false}};Arr.prototype.sort=function(b){if(df(b,"function")||!df(b)){return this._ret(this.a.sort.apply(this.a,arguments))}else{if(df(b,"object")){var a={col:b.col||0,dir:b.dir||"asc"};if(a.dir==-1){a.dir="desc"}if(a.dir==1){a.dir="asc"}return this.sort(function(d,c){if((d instanceof Array||d instanceof Object)&&(c instanceof Array||c instanceof Object)&&df(d[a.col])&&df(c[a.col])){d=d[a.col];c=c[a.col]}return a.dir=="desc"?(d<c?1:d>c?-1:0):(d>c?1:d<c?-1:0)})}else{return false}}};Arr.prototype.splice=function(){return this._ret(this.a.splice.apply(this.a,arguments))};Arr.prototype.toSource=function(){return this.a.toSource?this.a.toSource.apply(this.a,arguments):"["+this.a.toString.apply(this.a,arguments)+"]"};Arr.prototype.toString=function(){return this.a.toString.apply(this.a,arguments)};Arr.prototype.unshift=function(){return this.a.unshift.apply(this.a,arguments)};Arr.prototype.vAdjust=function(g,f){f=df(f)?f:false;if(df(g,"number")){this.map(function(a){return a+g})}else{if(df(g,"string")){var d=g.match(/^([*/])(\d+(\.\d+)?)$/);if(g.match(/^\\([*/])(\d+(\.\d+)?)$/)){g=g.substr(1)}if(d!=null){if(d[1]=="*"){this.map(function(a){return a*d[2]})}if(d[1]=="/"){this.map(function(a){return a/d[2]})}}else{var c=f?g:"",e=f?"":g;this.map(function(a){return c+a+e})}}else{if(df(g,"function")){this.map(g)}}}return this._ret(this.a)};Arr.prototype.valueOf=function(){return this.a.valueOf()};Arr.prototype.wrapAll=function(a){this._retWrap=df(a,"boolean",true);return this};function A(b){if(arguments.length>1||!(b instanceof Array)){b=Array.prototype.slice.call(arguments,0)}return new Arr(b)}Obj=function(a){this.o=a;this.length()};Obj.prototype._ret=function(a){return this._retWrap&&a instanceof Object?O(a).wrapAll():a};Obj.prototype._retWrap=false;Obj.prototype.hasProp=function(a){return Object.prototype.hasOwnProperty.call(this.o,a)};Obj.prototype.keys=function(){var a=[];this.loop(function(c,b){a.push(b)});return a};Obj.prototype.length=function(){var a=0;for(var b in this.o){if(this.o.hasOwnProperty(b)){a++}}this.len=a;return a};Obj.prototype.loop=function(e,d,b){b=df(b,"boolean")?b:false;if(!df(e,"function")){return false}for(var a in this.o){if((!b&&!this.hasProp(a)||a=="length")){continue}var c=null;if(d){c=e.apply(d,[this.o[a],a,this.o])}else{c=e(this.o[a],a,this.o)}if(typeof c!="undefined"){if(c=="__continue"){continue}else{return c}}}};Obj.prototype.map=function(a){this.loop(function(b,c){this.o[c]=a(b)},this);return this._ret(this.o)};Obj.prototype.toArray=function(){var b=Array.prototype.slice.call(this.o,0);if(b.length==0&&this.length()>0){var c=[];this.loop(function(d){c.push(d)});return c}var a=A(b);a._retWrap=this._retWrap;return a.filter(function(){return true})};Obj.prototype.vAdjust=function(g,f){f=df(f)?f:false;if(df(g,"number")){this.map(function(a){return a+g})}else{if(df(g,"string")){var d=g.match(/^([*/])(\d+(\.\d+)?)$/);if(g.match(/^\\([*/])(\d+(\.\d+)?)$/)){g=g.substr(1)}if(d!=null){if(d[1]=="*"){this.map(function(a){return a*d[2]})}if(d[1]=="/"){this.map(function(a){return a/d[2]})}}else{var c=f?g:"",e=f?"":g;this.map(function(a){return c+a+e})}}else{if(df(g,"function")){this.map(g)}}}return this._ret(this.o)};Obj.prototype.valueOf=function(){return this.o.valueOf()};Obj.prototype.wrapAll=function(a){this._retWrap=df(a,"boolean",true);return this};function O(a){return new Obj(a)}Str=function(a){this.s=a};Str.prototype=new String;Str.prototype._ret=function(a){return this._retWrap&&typeof a=="string"?S(a).wrapAll():a};Str.prototype._retWrap=false;Str.prototype.contains=function(a,b){return this.s.indexOf(a,b)>-1};Str.prototype.endsWith=function(a,b){if(typeof this.s.endsWith=="function"){return this.s.endsWith.call(this.s,a,b)}else{b=b||this.s.length;b=b-a.length;return this.lastIndexOf(a)==b}};Str.prototype.has=Str.prototype.contains;Str.prototype.is=function(a,d,b){d=typeof d=="boolean"?d:true;if(a instanceof RegExp){return this.matches(a)}else{return d&&!b?this.lower()==S(a).lower():(b?this.s===a:this.s==a)}};Str.prototype.isIn=function(a){return a.indexOf(this.s)>-1};Str.prototype.len=function(b,a){if(!b&&!a){return this.s.length}b=b?this.s.length>=b:true;a=a?this.s.length<=a:true;return b&&a};Str.prototype.log=function(){console.log(this.valueOf())};Str.prototype.lower=function(){return this._ret(this.toLowerCase())};Str.prototype.matches=function(a){return a.test(this)};Str.prototype.quote=function(){if(typeof this.s.quote=="function"){return this._ret(this.s.quote.call(this.s))}else{return this._ret('"'+this.rereplace(/(["\\])/g,"\\$1",/\r?\n/g,"\\n",/\t/g,"\\t")+'"')}};Str.prototype.rereplace=function(){var a=this.s;A(O(arguments).toArray()).loop(function(c,b){if(b%2!=0||typeof this[b+1]=="undefined"){return}a=a.replace(c,this[b+1])},arguments);return this._ret(a)};Str.prototype.startsWith=function(a,b){if(typeof this.s.startsWith=="function"){return this.s.startsWith.call(this.s,a,b)}else{b=b||0;return this.indexOf(a)==b}};Str.prototype.toSource=function(){return this._ret('"'+this.toString()+'"')};Str.prototype.toString=function(){return this._ret(this.s?this.s.toString():"")};Str.prototype.trim=function(a,b,d){d=d||"\\s";a=(typeof a)[0]=="u"||a===true?"^["+d+"]*":"";b=(typeof b)[0]=="u"||b===true?"["+d+"]*$":"";return this._ret(this.s.replace(new RegExp(a+"(.*?)"+b),"$1"))};Str.prototype.trimLeft=function(a){return this._ret(this.trim(true,false,a))};Str.prototype.trimRight=function(a){return this._ret(this.trim(false,true,a))};Str.prototype.valueOf=function(){return this.s.valueOf()};Str.prototype.wrapAll=function(a){this._retWrap=df(a,"boolean",true);return this};function S(a){return new Str(a)}Nod=function(a){if(df(a,"string")){a=document.querySelector(a)}this.n=a};Nod.prototype.addClass=function(b){var a=S(this.n.getAttribute("class")||"").trim();var c=a.replace(/^\s*(.*)\s*$/,"$1").split(" ");c=A(c).filter(function(d){return d!=""});if(!this.hasClass(b)){c.push(b)}this.n.setAttribute("class",c.join(" "));return this.hasClass(b)};Nod.prototype.adj=function(c,d,b){c=c?c.toLowerCase():"";d=d?d:false;var f="",g=false,e=this.n,a=b=="prev"?"previousSibling":"nextSibling";while(!g){e=e[a];if(e==null){e=false;break}f=e?N(e).getTagName():"";if(f==""){continue}if(c==""||f==c){g=true}if(d!==false&&g===true){g=N(e).hasClass(d)}}return e};Nod.prototype.children=function(){var a=this.n.childNodes,b=[];O(a).loop(function(c){if(c.nodeType==1){b.push(c)}});return b};Nod.prototype.find=function(a){if(!this.isElement()){return null}return this.n.querySelector(a)};Nod.prototype.findAll=function(a){if(!this.isElement()){return{}}return this.n.querySelectorAll(a)};Nod.prototype.getCompStyle=function(a){return this.n.currentStyle?this.n.currentStyle[a]:getComputedStyle(this.n,null)[a]};Nod.prototype.getPos=function(){var b=this.n.offsetLeft;var a=this.n.offsetTop;while(this.n.offsetParent){if(this.n==document.getElementsByTagName("body")[0]){break}else{b=b+this.n.offsetParent.offsetLeft;a=a+this.n.offsetParent.offsetTop;this.n=this.n.offsetParent}}return[b,a]};Nod.prototype.getTagName=function(){return(!this.n.tagName?"":this.n.tagName.toLowerCase())};Nod.prototype.hasClass=function(a){if(arguments.length==1){return new RegExp("(^|\\s)"+a+"(\\s|$)").test(this.n.className)}else{return O(arguments).toArray().every(function(b){return this.hasClass(b)},this)}};Nod.prototype.hasParent=function hasParent(b,d){d=df(d,"boolean")?d:false;if(df(b,"string")){var a=b.substr(0,1);if(a!="."&&a!="#"){a="tag"}b=b.replace(/^(\.|#)/,"")}else{var a="node"}var c=d?this.n:this.n.parentNode;while(c!=null){if(a=="#"&&c.id==b){return true}else{if(a=="."&&N(c).hasClass(b)){return true}else{if(a=="tag"&&N(c).getTagName()==b){return true}else{if(a=="node"&&c==b){return true}}}}c=c.parentNode}return false};Nod.prototype.hide=function(){this.n.style.display="none"};Nod.prototype.index=function(){return A(N(this.n.parentNode).children()).indexOf(this.n)};Nod.prototype.isElement=function(){return this.n&&df(this.n.nodeType)&&this.n.nodeType==1&&this.n.tagName!=""};Nod.prototype.next=function(a,b){return this.adj(a,b,"next")};Nod.prototype.on=function(b,a){addEvent(this.n,b,a)};Nod.prototype.parent=function(){return this.n.parentNode||false};Nod.prototype.prev=function(a,b){return this.adj(a,b,"prev")};Nod.prototype.remove=function(){return this.n.parentNode.removeChild(this.n)};Nod.prototype.removeClass=function(c){var b=S(this.n.getAttribute("class")||"").trim(),a=-1;var d=b.replace(/^\s*(.*)\s*$/,"$1").split(" ");d=A(d).filter(function(e){return e!=""});if((a=d.indexOf(c))>-1){d.splice(a,1)}this.n.setAttribute("class",d.join(" "));return !this.hasClass(c)};Nod.prototype.selectContents=function(){if(document.body.createTextRange){var a=document.body.createTextRange();a.moveToElementText(this.n);a.select()}else{if(window.getSelection){var b=window.getSelection(),a=document.createRange();a.selectNodeContents(this.n);b.removeAllRanges();b.addRange(a)}}};Nod.prototype.setStyles=function(a){if(this.n.style&&df(a,"object")&&O(a).len>0){O(a).loop(function(c,b){this.style[b]=c},this.n,true)}};Nod.prototype.show=function(){this.n.style.display=""};Nod.prototype.toString=function(){return this.n?this.n.toString():""};Nod.prototype.toggle=function(){this.n.style.display=(this.n.style.display=="none")?"":"none"};Nod.prototype.value=function(){return this.n.value};Nod.prototype.valueOf=function(){return this.n.valueOf()};function N(a){return new Nod(a)}Evnt=function(a){this.e=a||window.event;this.getTarget()};Evnt.prototype.target=null;Evnt.prototype.getTarget=function(){this.target=this.e.srcElement||this.e.target};Evnt.prototype.kill=function(a){a=df(a,"boolean")?a:true;if(this.e.preventDefault){this.e.preventDefault()}else{this.e.returnValue=false}return a?this.stopBubble():true};Evnt.prototype.stopBubble=function(){if(!this.e.stopPropagation){this.e.cancelBubble=true;return false}else{this.e.stopPropagation();return true}};Evnt.prototype.toString=function(){return this.e?this.e.toString():""};Evnt.prototype.valueOf=function(){return this.e.valueOf()};Evnt.prototype.which=function(c){if(df(this.e.which)){var a=this.e.which}else{if(df(this.e.button)&&!(df(this.e.keyCode)&&this.e.keyCode>0)){var a=this.e.button;a=a==0||a==1?1:(a==4?2:3)}else{var a=this.e.keyCode}}if(df(c)){if(df(c,"string")){var b={esc:27,backspace:8,del:46,enter:13,home:36,end:35,pageup:33,pagedown:34,space:32,f1:112,f2:113,left:37,up:38,right:39,down:40};if(df(b[S(c).lower()],"number")){c=b[S(c).lower()]}else{if(c.match(/^[a-z]$/)){c="abcdefghijklmnopqstuvwxyz".indexOf(c)+65}else{if(c.match(/^[0-9]$/)){c=parseInt(c)+48}}}}return a==c}else{return a}};function E(a){return new Evnt(a)}Dig=function(a){this.d=a.valueOf()};Dig.prototype=new Number;Dig.prototype._ret=function(a){return this._retWrap&&typeof a=="number"?D(a).wrapAll():a};Dig.prototype._retWrap=true;Dig.prototype.precision=8;Dig.prototype.isFinite=function(){return !isNaN(this.d)&&this.d!=Infinity&&this.d!=-Infinity&&typeof this.d=="number"};Dig.prototype.isInteger=function(){return df(this.d.isInteger,"function")?this.d.isInteger():(typeof this.d=="number"&&this.isFinite()&&Math.floor(this.d)===this.d)};Dig.prototype.isNaN=function(){return isNaN(this.d)};Dig.prototype.pad=function(a,b,e){var c=this.toString(),b=b||"0";while(c.length<a){c=e=="r"?c+b:b+c}return c};Dig.prototype.parseFloat=Dig.prototype.asFloat=function(){return this._ret(parseFloat(this.d))};Dig.prototype.parseInt=Dig.prototype.asInt=function(a){return this._ret(parseInt(this.d,df(a,"number",10)))};Dig.prototype.round=function(b,a){b=df(b,"number",10);a=[-1,1,"down","up"].indexOf(a)>-1?a:0;if(a==-1||a=="down"){this.d=b*Math.floor(this.d/b)}else{if(a==1||a=="up"){this.d=b*Math.ceil(this.d/b)}else{this.d=b*Math.round(this.d/b)}}return this._ret(this.d)};Dig.prototype.toInteger=Dig.prototype.toInt=function(){var a=this.isFinite()?a:this.asInt();return this._ret(D(a).isNaN()?0:a)};Dig.prototype.toPrecision=function(a){a=df(a)?a:this.precision;return this._ret(this.d.toPrecision(a))};Dig.prototype.toString=function(){return this.d.toString()};Dig.prototype.valueOf=function(){return this.d.valueOf()};Dig.prototype.wrapAll=function(a){this._retWrap=df(a,"boolean",true);return this};function D(a){return new Dig(a)}function P(a){if(a instanceof Array){return A(a)}else{if(a instanceof Object){return O(a)}else{if(typeof a=="string"){return S(a)}else{if(a instanceof Node){return N(a)}else{if(a instanceof Event){return E(a)}}}}}return null}function InheritFrom(b,a){if(typeof a=="function"){b.prototype=new a;b.prototype.parent=a.prototype}else{b.prototype=a;b.prototype.parent=a}b.prototype.constructor=b;b.prototype.store=b.prototype.constructor}Function.prototype.inheritFrom=function(a){InheritFrom(this,a)};Function.prototype._ext=function(){A(O(arguments).toArray()).loop(function(a){InheritFrom(a,this)},this)};function addEvent(c,b,a){if(b instanceof Array){A(b).loop(function(d){addEvent(c,d,a)});return}b=b.replace(/^on/,"");if(typeof a!=="function"){return false}if(c.addEventListener){return c.addEventListener(b,a,false)}else{return c.attachEvent("on"+b,a)}}function addOnload(a){addEvent(window,"onload",a)}function df(a,b,e){var c=typeof b=="undefined"?typeof a!="undefined":typeof a==b;if(typeof e!=="undefined"){return c?a:e}else{return c}}function getEvent(a){a=a||window.event;a.target=a.srcElement||a.target;return a};