Prot.js

Helps neaten code without being a heavy library

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/1780/8575/Protjs.js

  1. // ==UserScript==
  2. // @name Prot.js
  3. // @namespace http://mdev.me/
  4. // @description Helps neaten code without being a heavy library
  5. // @include http://*stackoverflow.com/questions/*
  6. // @version 3
  7. // @grant none
  8. // ==/UserScript==
  9. /**
  10. * ||| Array wrapper for custom prototype functionality |||
  11. *
  12. * ----------------------------------------------------
  13. * Usage: A(myArr).loop() - or - A([1,2,3]).loop()
  14. * ----------------------------------------------------
  15. *
  16. * Functions available:
  17. * =====================
  18. * A(a).getUnique() - - - - (array) unique elements from the original array
  19. * A(a).contains(b) - - - - (bool) whether b exists in a
  20. * A(a).has(b) - - - - - - (bool) alias for .contains()
  21. * A(a).loop(f) - - - - - - (mixed) runs function f for every element in array a, passes params (value,key,array)
  22. * A(a).wrapAll() - - - - - (object) makes any method from now on, that would have returned an array, return the array in this wrapper ( returns this for chained inst, e.g. var ar = A([]).wrapAll() )
  23. *
  24. * Plus polyfills for all new methods (every,forEach,some,map,filter,indexOf,lastIndexOf)
  25. */
  26. Arr = function(a){this.a=a}
  27. Arr.prototype = new Array;
  28. Arr.prototype._ret = function(ret){return this._retWrap&&ret instanceof Array?A(ret).wrapAll():ret;}
  29. Arr.prototype._retWrap = false;
  30. Arr.prototype.concat = function() { return this.a.concat.apply(this.a,arguments); }
  31. Arr.prototype.contains = function(b){return this.a.indexOf(b)>-1;}
  32. Arr.prototype.every = function(func,that) {
  33. that=that||this.a;if(typeof this.a.every=="function") return this.a.every.apply(that,arguments);
  34. else{for(var i=0,c=this.a.length;i<c;i++){if(func.call(that,this.a[i],i,this.a)!==true)return false}return true;}
  35. }
  36. Arr.prototype.filter = function(func,that) {
  37. that=that||this.a;if(typeof this.a.filter=="function") return this._ret(this.a.filter.apply(that,arguments));
  38. else{var newA=[];for(var i=0,c=this.a.length;i<c;i++){if(func.call(that,this.a[i],i,this.a)===true)newA.push(this.a[i])}return this._ret(newA);}
  39. }
  40. Arr.prototype.forEach = function(func,that){that=that||this.a;if(typeof this.a.forEach=="function") this.a.forEach.apply(that,arguments);else this.loop(func,that);}
  41. Arr.prototype.getUnique=function(){var a=[];this.loop(function(l){if(a.indexOf(l)>-1)return;a.push(l);});return this._ret(a);}
  42. Arr.prototype.has = Arr.prototype.contains;
  43. Arr.prototype.indexOf = function(searchElement/*,fromIndex*/) {
  44. if(typeof this.a.indexOf == "function") return this.a.indexOf.apply(this.a,arguments);
  45. else { if(this===null)throw new TypeError();var n=0,k,t=new Object(this.a),len=t.length>>>0;
  46. if(len === 0)return -1;if(arguments.length>1){n=Number(arguments[1]);if(n!==n)n=0;else if(n!==0&&n!==Infinity&&n!==-Infinity)n=(n>0||-1)*Math.floor(Math.abs(n));}
  47. if(n>=len)return -1;for(k=n>=0?n:Math.max(len-Math.abs(n),0);k<len;k++){if(t[k]&&t[k]===searchElement)return k;}return -1; }
  48. }
  49. Arr.prototype.join = function() { return this.a.join.apply(this.a,arguments); }
  50. Arr.prototype.last = function() { return this.a[this.length-1]; }
  51. Arr.prototype.lastIndexOf = function(searchElement/*,fromIndex*/) {
  52. if(typeof this.a.lastIndexOf == "function") return this.a.lastIndexOf.apply(this.a,arguments);
  53. else { if(this===null)throw new TypeError();var n,k,t=new Object(this.a),len=t.length>>>0;
  54. if(len === 0)return -1;n=len;if(arguments.length>1){n=Number(arguments[1]);if(n!==n)n=0;else if(n!==0&&n!==Infinity&&n!==-Infinity)n=(n>0||-1)*Math.floor(Math.abs(n));}
  55. for(k=n>=0?Math.min(n,len-1):len-Math.abs(n);k>=0;k--){if(t[k]&&t[k]===searchElement)return k;}return -1; }
  56. }
  57. Arr.prototype.loop = function(func,that) {
  58. for(var i=0,c=this.a.length;i<c;i++)
  59. {
  60. var r = null;
  61. if(that) r = func.apply(that,[this.a[i],i,this.a]);
  62. else r = func(this.a[i],i,this.a);
  63. if(typeof r != "undefined")
  64. { if(r == '__continue') continue; else return r; }
  65. } return;
  66. }
  67. Arr.prototype.map = function(func,that) {
  68. that=that||this.a;
  69. if(typeof this.a.map == "function"){this.a=this.a.map.apply(that,arguments);return this._ret(this.a);}
  70. else{var bk=this.a;for(var i=0,c=this.a.length;i<c;i++){this.a[i]=func.call(that,this.a[i],i,bk)}return this._ret(this.a);}
  71. }
  72. Arr.prototype.pop = function() { return this.a.pop.apply(this.a,arguments); }
  73. Arr.prototype.push = function() { return this.a.push.apply(this.a,arguments); }
  74. Arr.prototype.reverse = function() { return this._ret(this.a.reverse.apply(this.a,arguments)); }
  75. Arr.prototype.shift = function() { return this.a.shift.apply(this.a,arguments); }
  76. Arr.prototype.slice = function() { return this._ret(this.a.slice.apply(this.a,arguments)); }
  77. Arr.prototype.some = function(func,that) {
  78. that=that||this.a;
  79. if(typeof this.a.some=="function") return this.a.some.apply(that,arguments);
  80. else{for(var i=0,c=this.a.length;i<c;i++){if(func.call(that,this.a[i],i,this.a)===true)return true}return false;}
  81. }
  82. Arr.prototype.sort = function(f) {
  83. if(df(f,"function") || !df(f)) return this._ret(this.a.sort.apply(this.a,arguments));
  84. else if(df(f,"object"))
  85. {
  86. var settings={col:f.col||0,dir:f.dir||'asc'};if(settings.dir==-1)settings.dir='desc';if(settings.dir==1)settings.dir='asc';
  87. return this.sort(function(a,b) {
  88. if((a instanceof Array || a instanceof Object) && (b instanceof Array || b instanceof Object) && df(a[settings.col]) && df(b[settings.col]))
  89. {a = a[settings.col];b = b[settings.col];}return settings.dir=='desc'?(a<b?1:a>b?-1:0):(a>b?1:a<b?-1:0);
  90. });
  91. }else return false;
  92. }
  93. Arr.prototype.splice = function() { return this._ret(this.a.splice.apply(this.a,arguments)); }
  94. Arr.prototype.toSource = function() { return this.a.toSource ? this.a.toSource.apply(this.a,arguments) : '['+this.a.toString.apply(this.a,arguments)+']'; }
  95. Arr.prototype.toString = function() { return this.a.toString.apply(this.a,arguments); }
  96. Arr.prototype.unshift = function() { return this.a.unshift.apply(this.a,arguments); }
  97. Arr.prototype.vAdjust=function(v,prep){
  98. prep = df(prep)?prep:false;
  99. if(df(v,"number")) this.map(function(e){return e+v});
  100. else if(df(v,"string"))
  101. {
  102. var m=v.match(/^([*/])(\d+(\.\d+)?)$/);
  103. if(v.match(/^\\([*/])(\d+(\.\d+)?)$/)) v = v.substr(1);
  104. if(m!=null)
  105. {
  106. if(m[1]=='*') this.map(function(e){return e*m[2]});
  107. if(m[1]=='/') this.map(function(e){return e/m[2]});
  108. }
  109. else
  110. {
  111. var b=prep?v:'',a=prep?'':v;
  112. this.map(function(e){return b+e+a});
  113. }
  114. }
  115. else if(df(v,"function")) this.map(v);
  116. return this._ret(this.a);
  117. }
  118. Arr.prototype.valueOf=function(){return this.a.valueOf();}
  119. Arr.prototype.wrapAll=function(r){this._retWrap=df(r,"boolean",true);return this;}
  120.  
  121. function A(a){if(arguments.length>1||!(a instanceof Array))a=Array.prototype.slice.call(arguments,0);return new Arr(a)}
  122.  
  123. /**
  124. * ||| Object wrapper for custom prototype functionality |||
  125. *
  126. * ----------------------------------------------------------
  127. * Usage: O({foo:'bar'}).loop() - or - O(miscObj).loop()
  128. * ----------------------------------------------------------
  129. *
  130. * Functions available:
  131. * =====================
  132. * O(a).toArray() - - - - - (array) an array of the object's values
  133. * O(a).loop(f) - - - - - - (mixed) runs func f for every element in object a with params (value,key,object), 2nd param = thisObject, 3rd param true enumerates all properties
  134. * O(a).length() - - - - - (number) number of own properties
  135. * O(a).keys() - - - - - - (array) array of keys for the object's own properties
  136. */
  137. Obj = function(o){this.o=o;this.length();}
  138. Obj.prototype._ret = function(ret){return this._retWrap&&ret instanceof Object?O(ret).wrapAll():ret;}
  139. Obj.prototype._retWrap = false;
  140. Obj.prototype.hasProp=function(p){return Object.prototype.hasOwnProperty.call(this.o,p)}
  141. Obj.prototype.keys=function(){var ks=[];this.loop(function(v,k){ks.push(k)});return ks;}
  142. Obj.prototype.length=function(){var l=0;for(var i in this.o){if(this.o.hasOwnProperty(i))l++};this.len=l;return l;}
  143. Obj.prototype.loop=function(func,that,enumAll){
  144. enumAll = df(enumAll,"boolean") ? enumAll : false;
  145. if(!df(func,"function")) return false;
  146. for(var i in this.o)
  147. {
  148. if((!enumAll && !this.hasProp(i) || i == 'length')) continue;
  149. var r = null;
  150. if(that) r=func.apply(that,[this.o[i],i,this.o]);
  151. else r=func(this.o[i],i,this.o);
  152. if(typeof r != "undefined"){if(r=='__continue')continue;else return r;}
  153. }
  154. }
  155. Obj.prototype.map=function(f){
  156. this.loop(function(v,i){
  157. this.o[i] = f(v);
  158. },this);
  159. return this._ret(this.o);
  160. }
  161. Obj.prototype.toArray=function(){
  162. var r=Array.prototype.slice.call(this.o,0);
  163. if(r.length==0&&this.length()>0){var vs=[];this.loop(function(v){vs.push(v)});return vs;}
  164. var retA = A(r);
  165. retA._retWrap = this._retWrap;
  166. return retA.filter(function(){return true;});
  167. }
  168. Obj.prototype.vAdjust=function(v,prep){
  169. prep = df(prep)?prep:false;
  170. if(df(v,"number")) this.map(function(e){return e+v});
  171. else if(df(v,"string"))
  172. {
  173. var m=v.match(/^([*/])(\d+(\.\d+)?)$/);
  174. if(v.match(/^\\([*/])(\d+(\.\d+)?)$/)) v = v.substr(1);
  175. if(m!=null)
  176. {
  177. if(m[1]=='*') this.map(function(e){return e*m[2]});
  178. if(m[1]=='/') this.map(function(e){return e/m[2]});
  179. }
  180. else
  181. {
  182. var b=prep?v:'',a=prep?'':v;
  183. this.map(function(e){return b+e+a});
  184. }
  185. }
  186. else if(df(v,"function")) this.map(v);
  187. return this._ret(this.o);
  188. }
  189. Obj.prototype.valueOf=function(){return this.o.valueOf();}
  190. Obj.prototype.wrapAll=function(r){this._retWrap=df(r,"boolean",true);return this;}
  191. function O(o){return new Obj(o)}
  192.  
  193. /**
  194. * ||| String wrapper for custom prototype functionality |||
  195. *
  196. * ----------------------------------------------------------
  197. * Usage: S("my string").lower() - or - S(strVar).lower()
  198. * ----------------------------------------------------------
  199. *
  200. * Functions available:
  201. * =====================
  202. * S(a).lower() - - - - - - (string) the string in full lowercase
  203. * S(a).matches(/regex/) - (bool) whether the string matches that regex
  204. * S(a).is(b) - - - - - - - (bool) whether a equals b (case insensitive), 2nd param false = case sensitive, 3rd param true = strict (===) comparison
  205. * S(a).len(min,max) - - - (bool/int) whether string length between min & max (either optional with null - pass no args to retrieve length)
  206. * S(a).isIn(b) - - - - - - (bool) whether b contains a
  207. * S(a).contains(b) - - - - (bool) whether a contains b
  208. * S(a).has(b) - - - - - - (bool) alias for .contains
  209. * S(a).rereplace() - - - - (string) simplifies code when chaining replaces ( instead of str.replace(a,b).replace(c,d).replace(e,f) do S(str).replace(a,b,c,d,e,f) )
  210. * S(a).trim() - - - - - - (string) the original string with all space characters trimmed from either side params 1&2 are bool for enabling left and right trim respectively. 3rd param is trim char list
  211. *
  212. * Includes polyfills for new methods such as quote,startsWith,endsWith,trimLeft,trimRight,toSource
  213. */
  214. Str=function(s){this.s=s}
  215. Str.prototype=new String;
  216. Str.prototype._ret = function(ret){return this._retWrap&&typeof ret=='string'?S(ret).wrapAll():ret;}
  217. Str.prototype._retWrap = false;
  218. Str.prototype.contains=function(t,p){return this.s.indexOf(t,p)>-1}
  219. Str.prototype.endsWith=function(str,pos){
  220. if(typeof this.s.endsWith=="function")return this.s.endsWith.call(this.s,str,pos);
  221. else{pos=pos||this.s.length;pos=pos-str.length;return this.lastIndexOf(str)==pos;}
  222. }
  223. Str.prototype.has=Str.prototype.contains;
  224. Str.prototype.is=function(m,c,s){c=typeof c=="boolean"?c:true;if(m instanceof RegExp){return this.matches(m);}else{return c&&!s?this.lower()==S(m).lower():(s?this.s===m:this.s==m)}}
  225. Str.prototype.isIn=function(ss){return ss.indexOf(this.s)>-1}
  226. Str.prototype.len=function(n,x){if(!n&&!x)return this.s.length;n=n?this.s.length>=n:true;x=x?this.s.length<=x:true;return n&&x}
  227. Str.prototype.log=function(){console.log(this.valueOf())}
  228. Str.prototype.lower=function(){return this._ret(this.toLowerCase())}
  229. Str.prototype.matches=function(r){return r.test(this)}
  230. Str.prototype.quote=function(){
  231. if(typeof this.s.quote == "function") return this._ret(this.s.quote.call(this.s));
  232. else return this._ret('"'+this.rereplace(/(["\\])/g,'\\$1',/\r?\n/g,'\\n',/\t/g,'\\t')+'"');
  233. }
  234. Str.prototype.rereplace=function(){var s=this.s;A(O(arguments).toArray()).loop(function(r,i){if(i%2!=0||typeof this[i+1]=="undefined")return;s=s.replace(r,this[i+1]);},arguments);return this._ret(s)}
  235. Str.prototype.startsWith=function(str,pos){
  236. if(typeof this.s.startsWith=="function")return this.s.startsWith.call(this.s,str,pos);
  237. else{pos=pos||0;return this.indexOf(str)==pos;}
  238. }
  239. Str.prototype.toSource=function(){return this._ret('"'+this.toString()+'"')}
  240. Str.prototype.toString=function(){return this._ret(this.s ? this.s.toString() : '')}
  241. Str.prototype.trim=function(l,r,c){c=c||'\\s';l=(typeof l)[0]=="u"||l===true?'^['+c+']*':'';r=(typeof r)[0]=="u"||r===true?'['+c+']*$':'';return this._ret(this.s.replace(new RegExp(l+'(.*?)'+r),'$1'))}
  242. Str.prototype.trimLeft=function(c){return this._ret(this.trim(true,false,c))}
  243. Str.prototype.trimRight=function(c){return this._ret(this.trim(false,true,c))}
  244. Str.prototype.valueOf=function(){return this.s.valueOf();}
  245. Str.prototype.wrapAll=function(r){this._retWrap=df(r,"boolean",true);return this;}
  246. function S(s){return new Str(s)}
  247.  
  248. /**
  249. * ||| Node wrapper for custom prototype functionality |||
  250. *
  251. * ----------------------------------------------------------
  252. * Usage: N('#title').remove() - or - N(myEl).remove()
  253. * ----------------------------------------------------------
  254. *
  255. * Functions available:
  256. * =====================
  257. * N(a).remove() - - - - - - (object) The removed element
  258. * N(a).find(b) - - - - - - (object) The first matching element, or null if none found
  259. * N(a).findAll(b) - - - - - (object) The first matching element, or null if none found
  260. * N(a).children() - - - - - (array) The child nodes of a (elements only)
  261. * N(a).selectContents() - - (null) Selects the contents of element
  262. * N(a).hasParent(b) - - - - (bool) Whether or not a is a child of b
  263. * N(a).hasClass(b) - - - - (bool) Whether or not a has a class of b
  264. * N(a).isElement() - - - - (bool) Whether or not a is a valid element (rather than text/comment node)
  265. * N(a).index() - - - - - - (int) 0-based index of a in respect to it's immediate parent node
  266. * N(a).prev(tag,class) - - (object) The previous sibling element with the specified tag and class (both optional for wildcard match)
  267. * N(a).next(tag,class) - - (object) The next sibling element with the specified tag and class (both optional for wildcard match)
  268. */
  269. Nod=function(e){if(df(e,'string'))e=document.querySelector(e);this.n=e}
  270. Nod.prototype.addClass=function(className){
  271. var classes = S(this.n.getAttribute('class') || '').trim(); var classNames = classes.replace(/^\s*(.*)\s*$/,'$1').split(' ');
  272. classNames = A(classNames).filter(function(v){return v!=''}); if(!this.hasClass(className)) classNames.push(className);
  273. this.n.setAttribute('class',classNames.join(' ')); return this.hasClass(className);
  274. }
  275. Nod.prototype.adj=function(tagName,className,dir) {
  276. tagName=tagName?tagName.toLowerCase():'';className=className?className:false;
  277. var nextTag='',happy=false,elem=this.n,dirProp=dir=='prev'?'previousSibling':'nextSibling';
  278. while(!happy)
  279. {
  280. elem=elem[dirProp];if(elem==null){elem=false;break;}nextTag=elem?N(elem).getTagName():'';
  281. if(nextTag=='')continue; if(tagName=='' || nextTag==tagName) happy = true;
  282. if(className !== false && happy === true){happy = N(elem).hasClass(className);}
  283. }
  284. return elem;
  285. }
  286. Nod.prototype.children=function(){var nodes = this.n.childNodes,ret = []; O(nodes).loop(function(n) { if(n.nodeType==1)ret.push(n); }); return ret;}
  287. Nod.prototype.find=function(s){if(!this.isElement())return null;return this.n.querySelector(s);}
  288. Nod.prototype.findAll=function(s){if(!this.isElement())return {};return this.n.querySelectorAll(s);}
  289. Nod.prototype.getCompStyle=function(style){return this.n.currentStyle?this.n.currentStyle[style]:getComputedStyle(this.n,null)[style];}
  290. Nod.prototype.getPos=function(){
  291. var posX = this.n.offsetLeft;
  292. var posY = this.n.offsetTop;
  293. while(this.n.offsetParent){
  294. if(this.n == document.getElementsByTagName('body')[0])break;
  295. else{posX=posX+this.n.offsetParent.offsetLeft;posY=posY+this.n.offsetParent.offsetTop;this.n=this.n.offsetParent;}
  296. }
  297. return [posX,posY];
  298. }
  299. Nod.prototype.getTagName=function(){return (!this.n.tagName ? '' : this.n.tagName.toLowerCase());};
  300. Nod.prototype.hasClass=function(className){
  301. if(arguments.length==1) { return new RegExp('(^|\\s)'+className+'(\\s|$)').test(this.n.className);} else { return O(arguments).toArray().every(function(c){return this.hasClass(c)},this); }
  302. }
  303. Nod.prototype.hasParent=function hasParent(s,testThis)
  304. {
  305. testThis = df(testThis,'boolean') ? testThis : false; if(df(s,'string')){var t = s.substr(0,1);if(t != '.' && t != '#') t = 'tag';s = s.replace(/^(\.|#)/,'');}
  306. else{var t = 'node';}var p = testThis ? this.n : this.n.parentNode;
  307. while(p != null){if(t=='#'&&p.id==s)return true;else if(t=='.'&&N(p).hasClass(s))return true;else if(t=='tag'&&N(p).getTagName()==s)return true;else if(t=='node'&&p==s)return true;p=p.parentNode;}
  308. return false;
  309. }
  310. Nod.prototype.hide = function() { this.n.style.display='none' }
  311. Nod.prototype.html=function(h){return df(h)?this.n.innerHTML=h:this.n.innerHTML}
  312. Nod.prototype.index=function(){return A(N(this.n.parentNode).children()).indexOf(this.n);}
  313. Nod.prototype.isElement=function(){return this.n&&df(this.n.nodeType)&&this.n.nodeType==1&&this.n.tagName!=''}
  314. Nod.prototype.next=function(tagName,className){return this.adj(tagName,className,'next');}
  315. Nod.prototype.on=function(e,f){addEvent(this.n,e,f)}
  316. Nod.prototype.parent=function(){return this.n.parentNode||false;}
  317. Nod.prototype.prev=function(tagName,className){return this.adj(tagName,className,'prev');}
  318. Nod.prototype.remove=function(){return this.n.parentNode.removeChild(this.n);}
  319. Nod.prototype.removeClass=function(className){
  320. var classes = S(this.n.getAttribute('class') || '').trim(),i=-1; var classNames = classes.replace(/^\s*(.*)\s*$/,'$1').split(' ');
  321. classNames = A(classNames).filter(function(v){return v!=''}); if((i=classNames.indexOf(className))>-1) classNames.splice(i,1);
  322. this.n.setAttribute('class',classNames.join(' ')); return !this.hasClass(className);
  323. }
  324. Nod.prototype.selectContents=function(){
  325. if(document.body.createTextRange){
  326. var range = document.body.createTextRange(); range.moveToElementText(this.n);range.select();
  327. } else if (window.getSelection) { var selection = window.getSelection(),range = document.createRange();
  328. range.selectNodeContents(this.n);selection.removeAllRanges();selection.addRange(range); }
  329. }
  330. Nod.prototype.setStyles=function(styles){if(this.n.style && df(styles,'object') && O(styles).len>0) O(styles).loop(function(v,k){this.style[k] = v;},this.n,true);}
  331. Nod.prototype.style=function(n,v){ return df(v) ? this.n.style[n]=v : this.getCompStyle(n)}
  332. Nod.prototype.show = function() { this.n.style.display='' }
  333. Nod.prototype.toString=function(){return this.n ? this.n.toString() : ''}
  334. Nod.prototype.toggle=function(){this.n.style.display = (this.n.style.display=='none') ? '' : 'none';}
  335. Nod.prototype.value=function(){return this.n.value;}
  336. Nod.prototype.valueOf=function(){return this.n.valueOf();}
  337. function N(n){return new Nod(n)}
  338.  
  339. /**
  340. * ||| Event wrapper for custom prototype functionality |||
  341. *
  342. * ----------------------------------------------------
  343. * Usage: E(e).target - or - E(e).preventDefault()
  344. * ----------------------------------------------------
  345. *
  346. * Functions available:
  347. * =====================
  348. * E(e).target - - - - - - - (object) The event's target element
  349. * E(e).kill(stopBubble) - - Prevent the event's default actions (1st param true stops bubbling)
  350. * E(e).stopBubble() - - - - Allow event's default actions but stop bubbling
  351. * E(e).which(test) - - - - (int/bool) Get the keyCode/button digit (int) - if first param is supplied the function will compare the return value to the test value (boolean)
  352. */
  353. Evnt=function(e){this.e=e||window.event;this.getTarget();}
  354. Evnt.prototype.target=null;
  355. Evnt.prototype.getTarget=function(){this.target=this.e.srcElement||this.e.target;}
  356. Evnt.prototype.kill=function(stopBubble){
  357. stopBubble=df(stopBubble,'boolean')?stopBubble:true;
  358. if(this.e.preventDefault)this.e.preventDefault();else this.e.returnValue=false;
  359. return stopBubble?this.stopBubble():true;
  360. }
  361. Evnt.prototype.stopBubble=function(){
  362. if(!this.e.stopPropagation){this.e.cancelBubble=true;return false;}
  363. else{this.e.stopPropagation();return true;}
  364. }
  365. Evnt.prototype.toString=function(){return this.e?this.e.toString():''}
  366. Evnt.prototype.valueOf=function(){return this.e.valueOf();}
  367. Evnt.prototype.which=function(test){
  368. if(df(this.e.which))var w=this.e.which;
  369. else{if(df(this.e.button)&&!(df(this.e.keyCode)&&this.e.keyCode>0)){var w=this.e.button;w=w==0||w==1?1:(w==4?2:3);}else var w=this.e.keyCode;}
  370. if(df(test)){
  371. if(df(test,"string")){
  372. var map = {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};
  373. if(df(map[S(test).lower()],"number")) test=map[S(test).lower()]; else if(test.match(/^[a-z]$/)) test = 'abcdefghijklmnopqstuvwxyz'.indexOf(test)+65;
  374. else if(test.match(/^[0-9]$/)) test = parseInt(test)+48;
  375. }
  376. return w==test;
  377. }else return w;
  378. }
  379. function E(e){return new Evnt(e)}
  380.  
  381. /**
  382. * ||| Digit wrapper for custom prototype functionality |||
  383. *
  384. * ----------------------------------------------------
  385. * Usage: D(mynum).round() - or - D(8).toPrecision()
  386. * ----------------------------------------------------
  387. *
  388. * Functions available:
  389. * =====================
  390. * D(d).isNaN() - - - - - - (bool) Whether or not the value is NaN
  391. * D(d).isFinite() - - - - - (bool) Whether or not the value is finite
  392. * D(d).pad(l,s,d) - - - - - (string) The number, padded to length l, with the string s (default '0'). Puts padding on left, unless d=='r'
  393. * D(d).round(n,d) - - - - - (number) The number, rounded to the nearest multiple of n (default 10). Round direction can be forced with 1/'up' or -1/'down'
  394. * D(d).toPrecision(p) - - - (number) The number (d) to the specified precision (p)
  395. */
  396. Dig=function(d){this.d=d.valueOf()}
  397. Dig.prototype = new Number;
  398. Dig.prototype._ret = function(ret){return this._retWrap&&typeof ret=='number'?D(ret).wrapAll():ret;}
  399. Dig.prototype._retWrap = true;
  400. Dig.prototype.precision = 8;
  401. Dig.prototype.isFinite=function(){return !isNaN(this.d)&&this.d!=Infinity&&this.d!=-Infinity&&typeof this.d=='number'}
  402. 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)}
  403. Dig.prototype.isNaN=function(){return isNaN(this.d)}
  404. Dig.prototype.pad=function(l,s,d){var r=this.toString(),s=s||'0';while(r.length<l){r=d=='r'?r+s:s+r}return r}
  405. Dig.prototype.parseFloat=Dig.prototype.asFloat=function(){return this._ret(parseFloat(this.d))}
  406. Dig.prototype.parseInt=Dig.prototype.asInt=function(r){return this._ret(parseInt(this.d,df(r,'number',10)))}
  407. Dig.prototype.round=function(n,d){
  408. n=df(n,'number',10);d=[-1,1,'down','up'].indexOf(d)>-1?d:0;if(d==-1||d=='down')this.d=n*Math.floor(this.d/n);
  409. else if(d==1||d=='up')this.d=n*Math.ceil(this.d/n);else this.d=n*Math.round(this.d/n);return this._ret(this.d);
  410. }
  411. Dig.prototype.toInteger=Dig.prototype.toInt=function(){var ret=this.isFinite()?ret:this.asInt();return this._ret(D(ret).isNaN()?0:ret)}
  412. Dig.prototype.toPrecision=function(p){p=df(p)?p:this.precision;return this._ret(this.d.toPrecision(p));}
  413. Dig.prototype.toString=function(){return this.d.toString()}
  414. Dig.prototype.valueOf=function(){return this.d.valueOf()}
  415. Dig.prototype.wrapAll=function(r){this._retWrap=df(r,"boolean",true);return this;}
  416. function D(d){return new Dig(d)}
  417.  
  418.  
  419. //Generic instantiator
  420. function P(v){if(v instanceof Array)return A(v);else if(v instanceof Object)return O(v);else if(typeof v == "string")return S(v);else if(v instanceof Node)return N(v);else if(v instanceof Event)return E(v);return null;}
  421.  
  422.  
  423. function InheritFrom(o1,o2)
  424. {
  425. if(typeof o2 == "function"){o1.prototype = new o2;o1.prototype.parent = o2.prototype;}
  426. else{o1.prototype = o2;o1.prototype.parent = o2;}
  427. o1.prototype.constructor = o1;
  428. o1.prototype.store = o1.prototype.constructor;
  429. }
  430. Function.prototype.inheritFrom=function(o){InheritFrom(this,o);}
  431. Function.prototype._ext=function(){A(O(arguments).toArray()).loop(function(o){InheritFrom(o,this)},this);}
  432. Function.prototype.throttle=function(w,i){var t,r=true,f=this,i=(typeof i)[0]=='b'?i:true;return function(){var c=this,a=arguments;if(r){if(i)f.apply(c,a);r=false;t=setTimeout(function(){r=true;if(!i)f.apply(c,a)},w)}}}
  433. Function.prototype.debounce=function(b,c){var d,a=this;return function(){var e=this,f=arguments;clearTimeout(d),d=setTimeout(function(){d=null,c||a.apply(e,f)},b),c&&!d&&a.apply(e,f)}}
  434.  
  435.  
  436.  
  437. /**
  438. * ||| Useful functions |||
  439. */
  440. function addEvent(o,e,f){if(e instanceof Array){A(e).loop(function(a){addEvent(o,a,f)});return;}e=e.replace(/^on/,'');if(typeof f!=='function'){return false;}if(o.addEventListener){return o.addEventListener(e,f,false);}else return o.attachEvent("on"+e,f);}
  441. function addOnload(myfunc){addEvent(window,'onload',myfunc);}
  442. function df(v,t,d){var r=typeof t=="undefined"?typeof v!="undefined":typeof v==t;if(typeof d!=='undefined')return r?v:d;else return r;}
  443. function getEvent(e){e=e||window.event;e.target=e.srcElement||e.target;return e;}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址