]> jfr.im git - irc/quakenet/qwebirc.git/blob - static/js/jslib.js
Add command parsing and lots of other stuff...
[irc/quakenet/qwebirc.git] / static / js / jslib.js
1 if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
2 try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
3 try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
4 try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
5 try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
6 throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.")
7 };
8
9 function jsdecode(data) {
10 return eval('(' + data + ')');
11 }
12
13 function XHR(url, fn) {
14 var r = new XMLHttpRequest();
15 r.onreadystatechange = function() {
16 if(r.readyState == 4 && r.status == 200) {
17 var o = jsdecode(r.responseText);
18 fn(o);
19 }
20 }
21 r.open("GET", url, true);
22 r.send(null);
23 }
24
25 function empty(y) {
26 for(var x in y) {
27 return false;
28 }
29 return true;
30 }
31
32 function ANI(p, d) {
33 if(d < 0)
34 return p[p.length + d];
35
36 return p[d];
37 }
38
39 var forEach = function(x, fn) {
40 for(var i=0;i<x.length;i++)
41 if(fn(x[i]))
42 return;
43 }
44
45 /* how horribly inefficient */
46 String.prototype.replaceAll = function(f, t) {
47 var i = this.indexOf(f);
48 var c = this;
49
50 while(i > -1) {
51 c = c.replace(f, t);
52 i = c.indexOf(f);
53 }
54 return c;
55 }
56
57 /* how horribly inefficient (again) */
58 String.prototype.splitMax = function(by, max) {
59 var items = this.split(by);
60 var newitems = items.slice(0, max-1);
61
62 if(items.length >= max)
63 newitems.push(items.slice(max-1).join(by));
64
65 return newitems;
66 }
67
68 alert("a".splitMax(" ", 2));
69 alert("a b".splitMax(" ", 2));
70 alert("a b c".splitMax(" ", 2));
71 alert("a b".splitMax(" ", 3));
72 alert("a b c".splitMax(" ", 3));
73 alert("a".splitMax(" ", 4));
74 alert("a b".splitMax(" ", 4));
75 alert("a b c".splitMax(" ", 4));
76 alert("a".splitMax(" ", 1));
77 alert("a b".splitMax(" ", 1));
78 alert("a b c".splitMax(" ", 1));
79
80 DaysOfWeek = {
81 0: "Sun",
82 1: "Mon",
83 2: "Tue",
84 3: "Wed",
85 4: "Thu",
86 5: "Fri",
87 6: "Sat"
88 }
89
90 MonthsOfYear = {
91 0: "Jan",
92 1: "Feb",
93 2: "Mar",
94 3: "Apr",
95 4: "May",
96 5: "Jun",
97 6: "Jul",
98 7: "Aug",
99 8: "Sep",
100 9: "Oct",
101 10: "Nov",
102 11: "Dec"
103 }
104