forked from fossasia/star-me
-
Notifications
You must be signed in to change notification settings - Fork 1
/
star.user.js
1454 lines (1434 loc) · 43.7 KB
/
star.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Create Element:
$Rainb.el('div',{'attribute':"value",style:{"color":"red"}},[ (childnodes) ])
becomes: <div attribute="value" style="color: red;"></div>
Append Element
$Rainb.add(element,elementToAppend)
Get Element By Id
$Rainb.id(id);
Create TextNode
$Rainb.tn(text);
Create a RainbNode (an element wrapper)
$Rainb.node(elem);
You can modify RainbNode attributes and children and it will not affect DOM until you call .render
Parameter is boolean, true for rendering children, false for not rendering children
$Rainb.node.render(boolean);
*/
//This probably proves I've gotten better at programming.
//DOM helper functions.
var genParser = (function(tys, tree, start) {
var source, index;
var treeRewrite = tree;
//Apparently, you don't tokenize and then parse, you do it on the go, but with more specific techniques which people call grammars, oh well, how was I suppesd to know that anyway.
//reference {type:"type",is:"type"} "hue"
//repetition {type:"repeat",optional:false,from:1,to:Infinity,contains:{},delimiting:null,multipleDelimeters:null} optional to and from are defaulted, delimiters can be used for lists like a,b,c and stuff
//array {type:"tyArray",contains:[]}
//alternate {type:"alternate",contains:[]}
//Expression {type:"expression",contains:{},operators:[{precedence:1,rightAssociative:false,tokens:[]}}],delimeters=[["(",")"]],whiteSpaceIgnore:null}
var mains = { //START PARSE
type: "type",
is: start
}
//yay extendibility
var funcs = { //funcions/types used, hue
expression: function(o) { //parse it like an expression
//this is probably a little bit hard to understand
var r = {
type: "alternate",
contains: [o.contains]
}, //is it a token, an operator, or a parenthesis?
opers = {
type: "alternate",
contains: []
},
delims = {
type: "alternate",
contains: []
},
i, I, l, L, props, t, n, ret = {},
_ind = index,
EXPRS = [],
OPERATORS = [],
O, precedence, rightAssociative, arg1, arg2, k; //I use and reuse most variables I can, damn
if (O = o.operators) {
for (i = 0, l = O.length; i < l; i++) {
for (I = 0, L = O[i].tokens.length; I < L; I++) {
t = O[i].tokens[I];
if (o.whiteSpaceIgnore) {
if (typeof t === "string") {
opers.contains.push(new RegExp("\\s*(?:" + t.replace(/([-+\\?.!$^&*(){}[\]])/g, "\\$1") + ")\\s*"));
} else if (t instanceof RegExp) {
opers.contains.push(new RegExp("\\s*(?:" + t.source + ")\\s*", (t.multiline ? "m" : "") + (t.ignoreCase ? "i" : "")))
} else {
opers.contains.push({
type: "tyArray",
contains: [/\s*/, t, /\s*/]
}); /*Ahh I HATE THIS! D:*/
}
} else {
opers.contains.push(t);
}
}
}
r.contains[1] = opers; //ADD THEM TO THE LIST
}
if (O = o.delimeters) { //this is like a carbon copy of the previous if, should I try to make it a function? Don't repeat yourself
for (i = 0, l = O.length; i < l; i++) {
for (I = 0, L = O[i].length; I < L; I++) {
t = O[i][I];
if (o.whiteSpaceIgnore) {
if (typeof t === "string") {
delims.contains.push(new RegExp("\s*(?:" + t + ")\s*"));
} else if (t instanceof RegExp) {
delims.contains.push(new RegExp("\s*(?:" + t.source + ")\s*", (t.multiline ? "m" : "") + (t.ignoreCase ? "i" : "")))
} else {
delims.contains.push({
type: "tyArray",
contains: [/\s*/, t, /\s*/]
}); /*Ahh I HATE THIS! D:*/
}
} else {
delims.contains.push(t);
}
}
}
r.contains[2] = delims;
}
/*Shunting Yard Algorithm*/
while (n = isIndexItem(r, props = {})) { //While there are tokens to be read
//read a token
if (props._matched === r.contains[0]) { //If the token is a number, then add it to the output queue.
EXPRS.push(n);
} else
if (props._matched === opers) { //If the token is an operator, o1, then
if ((I = opers.contains.indexOf(props.props._matched)) !== -1) {
for (i = 0, l = (O = o.operators).length, k = 0; i < l; i++) { //
if ((k += O[i].tokens.length) > I) {
precedence = O[i].precedence;
rightAssociative = O[i].rightAssociative;
break;
}
}
} else {
throw new Error("props.props._matched not found at oper.contains, This is impossible.. or is it?");
}
while ((L = OPERATORS.length) && (((!rightAssociative) && precedence === OPERATORS[L - 1][1]) || precedence < OPERATORS[L - 1][1])) { //while there is an operator token, o2, at the top of the stack, and
//either o1 is left-associative and its precedence is equal to that of o2,
//or o1 has precedence less than that of o2,
/*POPPINGG!!*/
//pop o2 off the stack, onto the output queue;
//This popping is also a bit of PRN execution, basically it is shunting yard and prn, or something weird
arg2 = EXPRS.pop();
arg1 = EXPRS.pop();
if (!(EXPRS.length || arg1)) {
console.warn("NOT ENOUGH TERMS");
}
t = OPERATORS.pop();
for (i = 0, l = (O = o.operators).length, k = 0; i < l; i++) {
if ((k += O[i].tokens.length) > t[2]) {
EXPRS.push({
operation: O[i].tokens[t[2] - (k - O[i].tokens.length)],
op: t[0],
arguments: [arg1, arg2],
name: "operator"
});
break;
}
}
}
OPERATORS.push([n, precedence, I]);
} else
if (props._match === delims) {} else {
throw Error("This is impossible! It has matched an unknown value..???");
}
}
//When there are no more tokens to read
while (L = OPERATORS.length) { //While there are still operator tokens in the stack
//Pop the operator onto the output queue.
arg2 = EXPRS.pop();
arg1 = EXPRS.pop();
if (!(EXPRS.length || arg1)) {
console.warn("NOT ENOUGH TERMS");
}
t = OPERATORS.pop();
for (i = 0, l = (O = o.operators).length, k = 0; i < l; i++) {
if ((k += O[i].tokens.length) > t[2]) {
EXPRS.push({
operation: O[i].tokens[t[2] - (k - O[i].tokens.length)],
op: t[0],
arguments: [arg1, arg2],
name: "operator"
});
break;
}
}
}
if (EXPRS.length < 1) {
return null;
}
if (EXPRS.length !== 1) {
throw new Error("Operators and expressions mismatch!!");
}
return EXPRS[0];
},
type: function(o) { //get type and parse it
var props = {},
a = isIndexItem(tys[o.is], props),
t, ret; //this is where props originally started, in short words, it is used to pass properties from other functions to here
if (a === null) return null;
//console.log()
ret = {
type: (t = tys[o.is]) && (t.delimiting ? "list" : t.type || ((typeof t === "string" || t instanceof RegExp) ? "String" : undefined)),
name: o.is,
content: a
}
for (var k in props) {
if (props.hasOwnProperty(k) && (!ret[k])) {
ret[k] = props[k];
}
}
return ret;
},
repeat: function(o, props) { //repeat
var reto = [],
e, d, _ind = index,
l, p, D = o.delimiting,
i = 0,
p = D && o.multipleDelimeters, //say, if the delimeter is just once, there is no point in putting it each time it appears.. right? so an CSV like "abc,dfe,ege" will appear as ["abc","dfe","ege"] instead of ["abc",',',"dfe",',',"ege"]
props2;
d = o.contains;
props.props = [];
do {
e = isIndexItem(D ? i & 1 ? D : d : d, props2 = {});
if ((!p) && D && i & 1) {
i++;
if (e !== null) {
continue;
} else {
break;
}
}
i++;
if (e !== null) {
reto.push(e)
props.props.push(props2)
}
} while (e !== null && i !== o.to);
l = reto.length;
if (((!o.optional) && l == 0) || ((!isNaN(p = o.from)) && l < p)) {
index = _ind;
return null;
}
if (D && !p) {
props.delimeter = D
}
return reto;
},
tyArray: function(o, props) { //tokens are in some order
var reto = [],
e, _ind = index,
opt = o.optional || [],
props2;
props.props = [];
for (var i = 0, l = o.contains.length, d; i < l; i++) {
d = o.contains[i];
e = isIndexItem(d, props2 = {});
if (e === null && (opt.indexOf(i) < 0)) {
index = _ind;
return null;
}
if (e !== null) props.props.push(props2);
reto.push(e);
}
return reto;
},
alternate: function(o, props) { //It alternates
var reto = null,
e, props2 = {};
for (var i = 0, l = o.contains.length, d; i < l; i++) {
d = o.contains[i];
e = isIndexItem(d, props2);
if (e !== null) {
reto = e;
props.props = props2;
props._matched = d;
break;
}
}
return reto;
}
}
function isIndexItem(item, props) { //recursive
//returns item or null
var s, t, r,
f;
if (!item) {
return null
} else
if (item instanceof RegExp) {
r = new RegExp
r.compile("^(?:" + item.source + ")", (item.multiline ? "m" : "") + (item.eturnignoreCase ? "i" : ""))
//r.lastIndex = index;
s = r.exec(source.substr(index)); //RAAAWR damn it
t = s && s[0];
if (t === null) return null;
index += t.length;
return t;
} else if (typeof item == "string") { //literal match
//console.log("DOES "+item+" and"+source.substr(index,item.length)+" MATCHES??");
if (item === source.substr(index, item.length)) return (index += item.length), item;
return null;
} else {
t = item.type;
f = funcs[t];
s = f(item, props);
if (f) return s;
else return null;
}
}
function Parser(arg) {
source = arg,
index = 0; //index is 0!!!
return treeRewrite.unknown(isIndexItem(mains)); //wasn't that just pretty understandable?
}
return Parser;
});
var CssSelectorParser = genParser({ //tys, meaning types
"type selector": /\*|(?:[\w_]|\\x?[a-f0-9]{2,6}\s?|\\[\S\s])(?:[^\\\s#.>&+~:,="'[\]\)]|\\x?[a-f0-9]{2,6}\s?|\\[\S\s])*/i, //regex for tagname
attributeValue: { //the vaue of an attibute, it can be
type: "alternate",
contains: [/"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'/i, {
type: "type",
is: "type selector"
}]
},
"pseudo-class": {
type: "alternate",
contains: [{
type: "tyArray",
contains: [":not", {
type: "tyArray",
contains: ["(", {
type: "type",
is: "selectorArray"
}, ")"]
}],
}, {
type: "tyArray",
contains: [/::?(?:[\w_]|\\x?[a-f0-9]{2,6}\s?|\\[\S\s])(?:[^\\\s#.>&+~:,(]|\\x?[a-f0-9]{2,6}\s?|\\[\S\s])*/, {
type: "tyArray",
contains: ["(", /(?:[^)\\]|\\[\S\s])*/, ")"]
}],
optional: [1]
}]
}, //is for this I was thinking of implementing my own regex, this is beyond ridiculous
operator: /\s*(?:\$=|\^=|~=|\|=|\*=|=)\s*/, //you know the thing at [attr=value]
"attribute selector": {
type: "tyArray",
contains: ['[', {
type: "tyArray",
contains: [{
type: "type",
is: "type selector"
}, {
type: "type",
is: "operator"
}, {
type: "type",
is: "attributeValue"
}],
optional: [1, 2]
}, ']']
},
"ID selector": {
type: "tyArray",
contains: ['#', { //an id starts with an #
type: "type",
is: "type selector"
}]
},
"class selector": { //a classname starts with a dot
type: "tyArray",
contains: ['.', {
type: "type",
is: "type selector"
}]
},
"simple selector": { //a element selector is composed from tagname, clasname,attributesm, and pseudoclasses
//this is a sequence of simple selectors
type: "repeat",
contains: {
type: "alternate",
contains: [{
type: "type",
is: "type selector"
}, {
type: "type",
is: "class selector"
}, {
type: "type",
is: "ID selector"
}, {
type: "type",
is: "attribute selector"
}, {
type: "type",
is: "pseudo-class"
}]
}
},
selector:
/* {OLD LOL
type: "repeat",
delimiting: {
type: "type",
is: "relationship"
},
contains: {
type: "type",
is: "element"
}*/
{
type: "expression",
contains: {
type: "type",
is: "simple selector"
},
whiteSpaceIgnore: true,
rightAssociative: true,
operators: [{
precedence: 1,
tokens: ['>', '&', '+', '~', /\s/] //these are not actually operators this are combinators
}]
},
selectorArray: { //this is a selector group
type: "repeat",
delimiting: /\s*,\s*/, //it is separated by a comma, and optionally whitespace
contains: {
type: "type",
is: "selector"
}
}
}, {
unknown: function(a) {
return a.name ? this[a.name](a) : a;
},
selectorArray: function(a) {
var b = {
name: "selector group",
list: []
};
for (var i = 0, l = a.content.length; i < l; i++) b.list.push(this.unknown(a.content[i]))
return b
},
selector: function(a) {
return this.unknown(a.content)
},
"simple selector": function(a) {
var b = {},
att, c;
b.class = [];
b.attributes = [];
b.pseudoClass = [];
for (var i = 0, l = a.content.length, d; i < l; i++) {
d = a.content[i];
switch (d.name) {
case "type selector":
if (!b.tagName) {
b.tagname = this.unescape(d.content);
}
break;
case "class selector":
b.class.push(this.unescape(d.content[1].content))
break;
case "ID selector":
if (!b.ID) {
b.ID = this.unescape(d.content[1].content);
}
break;
case "attribute selector":
att = {
attributeName: this.unescape(d.content[1][0].content)
}
if (c = d.content[1][1]) {
att.operator = c.content;
att.attributeValue = this.unescape(d.content[1][2].content)
}
b.attributes.push(att);
break;
case "pseudo-class":
b.pseudoClass.push({
class: this.unescape(d.content[0]),
value: d.content[1] && this.unknown(d.content[1][1])
})
break;
}
}
return b;
},
operator: function(a) {
var b = this.unknown(a.arguments[1]);
b.parent = this.unknown(a.arguments[0]);
b.parentRelationship = a.op;
return b;
},
unescape: function(string) {
var unescape = [{
search: /\\([0-9A-fa-f]{1,6} ?)/g,
replace: {
f: "hexadecimal",
for: 1
}
}, {
search: /\\(.)/g,
replace: {
for: 1
}
}];
var replacement, string2 = string,
func;
if ((string[0] == '"' || string[0] == "'") && (string[0] === string[string.length - 1])) {
string2 = string.substring(1, string.length - 1)
}
for (var i = 0; i < unescape.length; i++) {
if ((func = unescape[i].replace.f) === undefined) {
replacement = "$" + unescape[i].replace.for
} else {
if (func == "hexadecimal") replacement = function(s) {
return String.fromCharCode(parseInt(arguments[unescape[i].replace.for], 16))
}
}
string2 = string2.replace(unescape[i].search, replacement)
}
return string2;
}
}, "selectorArray");
$Rainb = {
d: document.documentElement
}
$Rainb.id = function(id) {
return document.getElementById(id);
};
$Rainb.ready = function(fc) {
var cb;
if (document.readyState !== 'loading') {
fc();
return;
}
cb = function() {
$Rainb.off(document, 'DOMContentLoaded', cb);
return fc();
};
return $Rainb.on(document, 'DOMContentLoaded', cb);
};
$Rainb.formData = function(form) {
var fd, key, val;
if (form instanceof HTMLFormElement) {
return new FormData(form);
}
fd = new FormData();
for (key in form) {
val = form[key];
if (val) {
if (typeof val === 'object' && 'newName' in val) {
fd.append(key, val, val.newName);
} else {
fd.append(key, val);
}
}
}
return fd;
};
$Rainb.extend = function(object, properties) {
var key, val;
for (key in properties) {
val = properties[key];
object[key] = val;
}
};
$Rainb.deepCompare = function() {
var i, l, leftChain, rightChain;
function compare2Objects(x, y) {
var p;
// remember that NaN === NaN returns false
// and isNaN(undefined) returns true
if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
return true;
}
// Compare primitives and functions.
// Check if both arguments link to the same object.
// Especially useful on step when comparing prototypes
if (x === y) {
return true;
}
// Works in case when functions are created in constructor.
// Comparing dates is a common scenario. Another built-ins?
// We can even handle functions passed across iframes
if ((typeof x === 'function' && typeof y === 'function') || (x instanceof Date && y instanceof Date) || (x instanceof RegExp && y instanceof RegExp) || (x instanceof String && y instanceof String) || (x instanceof Number && y instanceof Number)) {
return x.toString() === y.toString();
}
// At last checking prototypes as good a we can
if (!(x instanceof Object && y instanceof Object)) {
return false;
}
if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
return false;
}
if (x.constructor !== y.constructor) {
return false;
}
if (x.prototype !== y.prototype) {
return false;
}
// Check for infinitive linking loops
if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
return false;
}
// Quick checking of one object beeing a subset of another.
// todo: cache the structure of arguments[0] for performance
for (p in y) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
}
for (p in x) {
if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
return false;
} else if (typeof y[p] !== typeof x[p]) {
return false;
}
switch (typeof(x[p])) {
case 'object':
case 'function':
leftChain.push(x);
rightChain.push(y);
if (!compare2Objects(x[p], y[p])) {
return false;
}
leftChain.pop();
rightChain.pop();
break;
default:
if (x[p] !== y[p]) {
return false;
}
break;
}
}
return true;
}
if (arguments.length < 1) {
return true; //Die silently? Don't know how to handle such case, please help...
// throw "Need two or more arguments to compare";
}
for (i = 1, l = arguments.length; i < l; i++) {
leftChain = []; //Todo: this can be cached
rightChain = [];
if (!compare2Objects(arguments[0], arguments[i])) {
return false;
}
}
return true;
}
$Rainb.on = function(el, events, handler) {
var event, _i, _len, _ref;
_ref = events.split(' ');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
event = _ref[_i];
el.addEventListener(event, handler, false);
}
};
$Rainb.off = function(el, events, handler) {
var event, _i, _len, _ref;
_ref = events.split(' ');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
event = _ref[_i];
el.removeEventListener(event, handler, false);
}
};
$Rainb.el = function(elem, attributes, childnodes, listeners) {
//listener format: {lstng:"click",cb:callback}
var e = (elem instanceof Element) ? elem : document.createElement(elem),
l;
for (var a in attributes) {
if (a == "style") {
for (var d in attributes[a]) {
e.style[d] = attributes[a][d];
}
continue;
}
if (a == "__properties") {
for (d in attributes[a]) {
e[d] = attributes[a][d];
}
continue;
}
e.setAttribute(a, attributes[a])
}
if (childnodes && (l = childnodes.length)) {
for (var i = 0, c; i < l; i++) {
c = childnodes[i];
if (c.length && typeof c == "string") {
e.appendChild(document.createTextNode(c));
continue;
}
e.appendChild(c)
}
}
if (listeners && (l = listeners.length)) {
for (var i = 0, c; i < l; i++) {
c = listeners[i];
$Rainb.on(e, c.lstng, c.cb);
}
}
return e;
}
$Rainb.HTTP = function() {
var lastModified = {};
return (function(url, extra, callback, headers) {
//headers is an object like this {Connection:"keep-alive"}
extra = extra || {};
function createXMLHttpRequest() {
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (typeof window.ActiveXObject != "undefined") {
try {
return new ActiveXObject("Msxml2.XMLHTTP.4.0");
} catch (e) {
try {
return new ActiveXObject("MSXML2.XMLHTTP");
} catch (e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
return null;
}
}
}
}
}
function looProp(object, callback) {
var a;
for (a in object) {
if (object.hasOwnProperty(a)) callback.call(object, a, object[a]);
}
}
extra.method = extra.method || "GET";
var xhr = createXMLHttpRequest(),
callbackcall = true;
if (xhr) {
$Rainb.extend(xhr, extra.opts);
$Rainb.extend(xhr.upload, extra.upCallbacks);
xhr.open(extra.method, url, !extra.sync);
if (extra.whenModified) {
if (url in lastModified) {
xhr.setRequestHeader('If-Modified-Since', lastModified[url]);
}
$Rainb.on(r, 'load', function() {
return lastModified[url] = r.getResponseHeader('Last-Modified');
});
}
looProp(headers, function(a, b) {
xhr.setRequestHeader(a, b)
})
xhr.onreadystatechange = function() {
if (xhr.readyState == xhr.DONE && callbackcall) {
callbackcall = false;
callback(xhr)
}
};
xhr.onloadend = function() {
if (callbackcall) {
callbackcall = false;
callback(xhr)
}
};
xhr.send(extra.post);
return xhr;
} else {
return null;
}
});
}()
$Rainb.hasClass = function(el, className) {
return el.classList && el.classList.contains(className);
};
$Rainb.rm = function(el) {
return el && el.parentNode.removeChild(el);
}
$Rainb.tn = function(s) {
return document.createTextNode(s);
};
$Rainb.add = function(parent, el) {
return parent.appendChild($Rainb.nodes(el));
};
$Rainb.nodes = function(nodes) {
var frag, node, _i, _len;
if (!(nodes instanceof Array)) {
return nodes;
}
frag = d.createDocumentFragment();
for (_i = 0, _len = nodes.length; _i < _len; _i++) {
node = nodes[_i];
frag.appendChild(node);
}
return frag;
};
$Rainb.prepend = function(parent, el) {
return parent.insertBefore($Rainb.nodes(el), parent.firstChild);
};
$Rainb.bubbleFind = function(element, elementSelector) {
while (element !== null) {
if ($Rainb.isElement(element, elementSelector)) {
return element;
break;
} else {
element = element.parentNode
}
}
}
$Rainb.nodes = function(nodes) {
var frag, node, _i, _len;
if (!(nodes instanceof Array)) {
return nodes;
}
frag = document.createDocumentFragment();
for (_i = 0, _len = nodes.length; _i < _len; _i++) {
node = nodes[_i];
frag.appendChild(node);
}
return frag;
};
$Rainb.after = function(root, el) {
return root.parentNode.insertBefore($Rainb.nodes(el), root.nextSibling);
};
$Rainb.before = function(root, el) {
return root.parentNode.insertBefore($Rainb.nodes(el), root);
};
$Rainb.replace = function(root, el) {
return root.parentNode.replaceChild($Rainb.nodes(el), root);
};
$Rainb.ins = function(txtarea, text, textEnd) {
var scrollPos = txtarea.scrollTop;
var strPos = 0;
textEnd = textEnd || "";
var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false));
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
strPos = range.text.length;
} else if (br == "ff") strPos = txtarea.selectionStart;
var front = (txtarea.value).substring(0, strPos);
var selectedText = (txtarea.value).substring(strPos, txtarea.selectionEnd);
var back = (txtarea.value).substring(txtarea.selectionEnd, txtarea.value.length);
txtarea.value = front + text + selectedText + textEnd + back;
strPos = strPos + text.length + selectedText.length + textEnd.length;
if (br == "ie") {
txtarea.focus();
var range = document.selection.createRange();
range.moveStart('character', -txtarea.value.length);
range.moveStart('character', strPos);
range.moveEnd('character', 0);
range.select();
} else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
};
$Rainb.alast = function(arr) {
return arr[arr.length - 1];
}
$Rainb.till = function() {
var selects = [],
listening = false;
function nodeInserted(event) {
if (!selects.length) {
$Rainb.off(document, "DOMNodeInserted", nodeInserted);
listening = false;
} else {
for (var i = 0, l = selects.length; i < l; i++) {
if ($Rainb.isElement(selects[i].selector, event.target)) {
selects[i].cb(event.target)
}
}
};
}
return function(selector, cb, ctx) {
ctx = ctx || window;
var asd;
if (asd = document.querySelector(selector)) {
cb(asd);
} else {
selects.push({
selector: selector,
cb: cb
});
if (!listening) {
$Rainb.on(document, "DOMNodeInserted", nodeInserted);
listening = true;
}
}
}
}();
$Rainb.addStyle = function(css, cb) {
var style = [];
for (var i = 0, l = css.length; i < l; i++) {
style[i] = $Rainb.el('style', null, [css[i]]);
}
$Rainb.till("head", function(a) {
for (var i = 0, l = style.length; i < l; i++) {
$Rainb.add(document.head, style[i]);
}
return cb(style);
});
return style;
};
(function() {
//INCOMPLETE
$Rainb.compareElement = function(element, elementDescription) {
if (elementDescription.tagname && (elementDescription.tagname.toUpperCase() !== element.tagName)) return false;
for (var i2 = 0, item, l2 = (item = elementDescription.class).length; i2 < l2; i2++) {
if (!$Rainb.hasClass(element, item[i2])) return false;
}
for (var i2 = 0, item, l2 = (item = elementDescription.pseudoClass).length; i2 < l2; i2++) {
if (item[i2].class == ":not" && isElement(element, item[i2].value)) return false
}
for (var i2 = 0, item, l2 = (item = elementDescription.attributes).length; i2 < l2; i2++) {
var val;
if (val = element.attributes.getNamedItem(item[i2].attributeName)) {
if (!compare(item[i2].operator, item[i2].attributeValue, val.value)) {
return false
}
} else {
return false
}
}
return true;
};
function compare(operator, attribute, attributeCompare) {
switch (operator) {
case "*=":
return attributeCompare.indexOf(attribute) !== -1
break;
case "~=":
return new RegExp("(?:^|\\s)" + attribute + "(?:\\s|$)").test(attributeCompare)
break;
case "|=":
return new RegExp("^" + attribute + "-?").test(attributeCompare);
break;
case "$=":
return new RegExp(attribute + "$").test(attributeCompare);
break;
case "^=":
return new RegExp("^" + attribute).test(attributeCompare);
break;
case "=":
return attribute == attributeCompare;
break;
default:
return true;
}
}
function isElement(element, abstractparsetree) {
for (var i = 0, l = abstractparsetree.list.length; i < l; i++) {
if ($Rainb.compareElement(element, abstractparsetree.list[i])) {
return true;
};
}
}
//compareElement is incomplete
$Rainb.isElement = function(element, elementSelector) {
if (!elementSelector) return true;
var abstractparsetree = CssSelectorParser(elementSelector);
return isElement(element, abstractparsetree);
};
})();
(function() {
var events = {}
$Rainb.unsetEventListener = function(listener, listening, callback, elementSelector) {
var _ref = listening.split(' ');
if (!listener.__$Rainb_Events) {
return false;
}
for (var _i = 0, _len = _ref.length; _i < _len; _i++) {
event = _ref[_i];
var events;
if ((events = listener.__$Rainb_Events)[event]) {
if (!(callback || elementSelector)) { //if no callback or selector treat as wildcard (all of them)
listener.__$Rainb_Events[event] = [];
$Rainb.off(listener, event, callBack);
continue;
}
for (var i = 0, l = events[event].length; i < l; i++) {
if ((events[event][i].callback == callback || !callback) && (!elementSelector || events[event][i].selector == elementSelector)) {
events[event].splice(i--, 1);
continue;
}
}
}
if (!(events[event] && events[event].length)) {
$Rainb.off(listener, event, callBack);
}
}
}
function callBack(event, callback) {
var x, element = event.target,
events = event.currentTarget.__$Rainb_Events[event.type].slice();
while (element !== null) {
for (var i = 0; i < events.length; i++) {
x = events[i];
if ($Rainb.isElement(element, x.selector)) {
callback.call(element, event);
events.splice(i--, 1);
continue;
}
}
if (!events.length) break;