]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/jslib.js
Add URL parsing for channels/nick selection.
[irc/quakenet/qwebirc.git] / js / jslib.js
1 Array.prototype.indexFromEnd = function(d) {
2 var p = this;
3
4 if(d < 0)
5 return p[p.length + d];
6
7 return p[d];
8 }
9
10 /* how horribly inefficient */
11 String.prototype.replaceAll = function(f, t) {
12 var i = this.indexOf(f);
13 var c = this;
14
15 while(i > -1) {
16 c = c.replace(f, t);
17 i = c.indexOf(f);
18 }
19 return c;
20 }
21
22 /* how horribly inefficient (again) */
23 String.prototype.splitMax = function(by, max) {
24 var items = this.split(by);
25 var newitems = items.slice(0, max-1);
26
27 if(items.length >= max)
28 newitems.push(items.slice(max-1).join(by));
29
30 return newitems;
31 }
32
33 function setAtEnd(obj) {
34 pos = obj.value.length;
35
36 if(obj.createTextRange) {
37 var range = obj.createTextRange();
38 range.move("character", pos);
39 range.select();
40 } else if(obj.selectionStart) {
41 obj.focus();
42 obj.setSelectionRange(pos, pos);
43 }
44 }
45
46 /* returns the arguments */
47 function parseURI(uri) {
48 var result = {}
49
50 var start = uri.indexOf('?');
51 if(start == -1)
52 return result;
53
54 var querystring = uri.substring(start + 1);
55
56 var args = querystring.split("&");
57
58 for(i=0;i<args.length;i++) {
59 var r = args[i].splitMax("=", 2);
60 if(r.length < 2)
61 continue;
62
63 result[unescape(r[0])] = unescape(r[1]);
64 }
65
66 return result;
67 }