-
Notifications
You must be signed in to change notification settings - Fork 188
/
xkit.js
executable file
·1298 lines (1166 loc) · 43.2 KB
/
xkit.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
var available_extensions = new Array();
var start = +new Date(); // log start timestamp
(function(){
if (typeof XKit !== "undefined") { return; }
XKit = {
version: framework_version,
page: {
standard: false,
ask_frame: false,
blog_frame: false,
xkit: false
},
init: function() {
// Check page then return control to init_extension.
if (document.location.href.indexOf('http://www.tumblr.com/xkit_reset') !== -1 ||
document.location.href.indexOf('http://www.tumblr.com/xkit_log') !== -1 ||
document.location.href.indexOf('http://www.tumblr.com/xkit_editor') !== -1 ||
document.location.href.indexOf('http://www.tumblr.com/xkit_update=') !== -1) {
XKit.page.xkit = true;
XKit.init_extension();
return;
}
XKit.init_flags();
if (top === self && document.location.href.indexOf("http://www.tumblr.com/dashboard/iframe?") === -1) {
XKit.page.standard = true;
XKit.init_extension();
} else {
if (document.location.href.indexOf("http://www.tumblr.com/dashboard/iframe?") !== -1) {
XKit.page.blog_frame = true;
}
if (document.location.href.indexOf("http://www.tumblr.com/ask_form/") !== -1) {
XKit.page.ask_frame = true;
}
XKit.init_extension();
}
},
init_extension: function() {
XKit.console.add("init_extension: " + JSON.stringify(XKit.page));
if (XKit.page.xkit == true) {
xkit_init_special();
return;
}
if (XKit.page.standard == true) {
XKit.init_normal();
return;
}
if (XKit.page.ask_frame == true || XKit.page.blog_frame == true) {
XKit.init_frame();
return;
}
},
init_normal: function() {
try {
// Check for Bridge errors.
var bridge_status = getBridgeError();
if (bridge_status.errors === true) {
// Bridge encountered an error!
XKit.window.show("XKit couldn't start.", "<b>Generated Error message:</b><br/><p>XKit Bridge Error<br/>" + bridge_status.error.message + "</p>Please reload the page to try again or click on the Troubleshooting Help button below to see how you can fix this error.", "error","<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div><a href=\"http://www.xkit.info/troubleshooting\" class=\"xkit-button\">Troubleshooting Help</a>");
return;
}
if (XKit.conflicts.check().count > 0) {
if (XKit.tools.get_setting("xkit_disable_conflicts", "false") !== "true") {
XKit.conflicts.show(XKit.conflicts.check());
}
if (XKit.conflicts.check().fatal === true) {
return;
}
}
// Load dashboard extensions.
XKit.notifications.init();
var m_browser = XKit.browser();
if (XKit.tools.get_setting("xkit_log_enabled","") === "true") {
XKit.console.show();
XKit.console.add("Running on " + m_browser.name + ", version " + m_browser.version);
}
if (m_browser.spoofed === true) {
// Nope, I won't be running here.
XKit.window.show("Your browser is pretending to be something it is not.", "Spoofing your browser name/version can cause problems not just with XKit but other websites and extensions too. Please turn off any User Agent modifier you have installed. XKit will stop running now.", "error","<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div><a href=\"http://www.xkit.info/troubleshooting\" class=\"xkit-button\">Troubleshooting Help</a>");
return;
}
xkit_check_storage();
if (XKit.tools.get_setting("xkit_installation_complete","") !== "true") {
// We need to install XKit!
xkit_install();
return;
}
// Okay lets boot our extensions now. This is actually done
// by xkit_main.js so let's just call it.
// First lets check if it actually exists.
if (XKit.installed.check("xkit_main") === false) {
XKit.console.add("xkit_main not found! Re-installing....");
xkit_install();
return;
}
// It exists! Great.
var xkit_main = XKit.installed.get("xkit_main");
if (xkit_main.errors == false && xkit_main.script !== "") {
XKit.console.add("Trying to run xkit_main.");
try {
eval(xkit_main.script);
XKit.extensions.xkit_main.run();
} catch(e) {
show_error_reset("Can't run xkit_main:" + e.message);
}
} else {
if (xkit_main.error == "not_installed") {
// Still not installed?
XKit.console.add("xkit_main not found! Re-installing....");
xkit_install();
return;
}
if (xkit_main.error == "parse_error") {
// Corrupt storage? Recomment reset.
XKit.console.add("xkit_main is corrupt!");
show_error_reset("Package xkit_main is corrupted!");
return;
}
}
} catch(e) {
show_error_update("xkit_init(): " + e.message);
}
},
init_frame: function() {
// Load frame extensions.
// First lets check if it actually exists.
if (XKit.installed.check("xkit_main") === false) {
XKit.console.add("xkit_main not found! Quitting.");
return;
}
// It exists! Great.
var xkit_main = XKit.installed.get("xkit_main");
if (xkit_main.errors == false && xkit_main.script !== "") {
XKit.console.add("Trying to run xkit_main.");
try {
eval(xkit_main.script);
XKit.frame_mode = true;
XKit.extensions.xkit_main.run();
} catch(e) {
XKit.console.add("Can't run xkit_main:" + e.message);
}
}
},
init_flags: function() {
for(var flag in XKit.flags) {
var m_value = XKit.tools.get_setting("xkit__flag__" + flag, "");
if (m_value === true) { m_value = true; } else { m_value = false; }
XKit.set_flag(flag, m_value);
}
},
flags: {
do_not_limit_extension_storage: false,
disable_notifications: false,
do_not_load_xkit_patches: false,
do_not_show_news: false,
allow_removal_of_internal_extensions: false
},
read_flag: function(flag_id) {
if (typeof XKit.flags[flag_id] === "undefined") {
return false;
} else {
return XKit.flags[flag_id];
}
},
set_flag: function(flag_id, value) {
if (typeof XKit.flags[flag_id] === "undefined") {
return false;
}
if (value === "true") { value = true; }
if (value === "false") { value = false; }
if (value === "" || typeof value === "undefined") { return; }
XKit.flags[flag_id] = value;
XKit.tools.set_setting("xkit__flag__" + flag_id, value);
},
servers: {
list: [
"http://sds1.puaga.com",
"http://sds2.puaga.com",
"http://sds3.puaga.com",
"http://sds4.puaga.com",
"http://xds1.puaga.com",
"http://xds2.puaga.com",
"http://xds3.puaga.com",
"http://www.puaga.com",
"http://puaga.com",
"http://www.studioxenix.com",
"http://studioxenix.com"
],
count: 0,
get: function() {
return XKit.servers.list[XKit.servers.count];
},
next: function() {
XKit.servers.count++;
if (XKit.servers.count >= XKit.servers.list.length) {
XKit.servers.count = 0;
}
return XKit.servers.list[XKit.servers.count];
}
},
extensions: {},
download: {
try_count: 0,
max_try_count: 8,
extension: function(extension_id, callback) {
// Downloads the extension file.
if (XKit.download.try_count >= XKit.download.max_try_count) {
XKit.download.try_count = 0;
var mdata = new Object();
mdata.errors = true;
mdata.server_down = true;
callback(mdata);
return;
}
var m_url = XKit.servers.next() + "/seven/get.php?extension=" + extension_id + "&ftch_id=" + XKit.tools.random_string();
XKit.console.add("Trying to fetch: " + m_url);
GM_xmlhttpRequest({
method: "GET",
url: m_url,
onerror: function(response) {
XKit.console.add("Server error, retrying download of page " + extension_id);
XKit.download.try_count++;
return XKit.download.extension(extension_id, callback);
},
onload: function(response) {
// We are done!
try {
var mdata = jQuery.parseJSON(response.responseText);
} catch(e) {
// Server returned bad thingy.
XKit.console.add("Unable to download extension '" + extension_id + "', server returned non-json object." + e.message);
XKit.download.try_count++;
return XKit.download.extension(extension_id, callback);
}
if (mdata.errors == true) {
XKit.download.try_count = 0;
XKit.console.add("Fetch successful, but mdata.errors is true or script is empty.");
callback(mdata);
} else {
XKit.download.try_count = 0;
XKit.console.add("Fetch successful, calling callback.");
callback(mdata);
}
}
});
},
page: function(page, callback) {
// Downloads page from servers.
if (XKit.download.try_count >= XKit.download.max_try_count) {
XKit.download.try_count = 0;
var mdata = new Object();
mdata.errors = true;
mdata.server_down = true;
callback(mdata);
return;
}
var m_url = XKit.servers.next() + "/seven/" + page;
if (m_url.indexOf("?") !== -1) {
m_url = m_url + "&ftch_id=" + XKit.tools.random_string();
} else {
m_url = m_url + "?ftch_id=" + XKit.tools.random_string();
}
XKit.console.add("Trying to fetch: " + m_url);
GM_xmlhttpRequest({
method: "GET",
url: m_url,
onerror: function(response) {
XKit.console.add("Server error, retrying download of page " + page);
XKit.download.try_count++;
return XKit.download.page(page, callback);
},
onload: function(response) {
// We are done!
try {
var mdata = jQuery.parseJSON(response.responseText);
} catch(e) {
// Server returned bad thingy.
XKit.console.add("Unable to download page '" + page + "', server returned non-json object.");
XKit.download.try_count++;
return XKit.download.page(page, callback);
}
if (mdata.errors == true) {
XKit.download.try_count = 0;
XKit.console.add("Fetch successful, but mdata.errors is true or script is empty.");
callback(mdata);
} else {
XKit.download.try_count = 0;
XKit.console.add("Fetch successful, calling callback.");
callback(mdata);
}
}
});
}
},
install: function(extension_id, callback) {
// Installs the extension.
XKit.download.extension(extension_id, function(mdata) {
XKit.console.add("download.extension of '" + extension_id + "' was successful. Calling callback.");
install_extension(mdata, callback);
});
},
installed: {
add: function(extension_id) {
// Add extension to the installed list.
if (XKit.installed.check(extension_id) === true) {
// Already added, stop.
return;
}
var m_string = XKit.tools.get_setting("xkit_installed_extensions","");
var installed_extensions;
try {
installed_extensions = JSON.parse(m_string);
} catch(e) {
installed_extensions = new Object();
installed_extensions.extensions = new Array();
}
installed_extensions.extensions.push(extension_id);
m_string = JSON.stringify(installed_extensions);
XKit.tools.set_setting("xkit_installed_extensions",m_string);
},
remove: function(extension_id) {
// Remove extension from installed list.
var current_extensions = XKit.installed.list();
for (i=0;i<current_extensions.length;i++) {
if (current_extensions[i] == extension_id) {
current_extensions.splice(i, 1);
}
}
var installed_extensions = new Object();
installed_extensions.extensions = current_extensions;
m_string = JSON.stringify(installed_extensions);
XKit.tools.set_setting("xkit_installed_extensions",m_string);
},
list: function() {
// Get an array containing all installed extensions.
var m_string = XKit.tools.get_setting("xkit_installed_extensions","");
var installed_extensions;
try {
installed_extensions = JSON.parse(m_string);
} catch(e) {
installed_extensions = new Object();
installed_extensions.extensions = new Array();
}
return installed_extensions.extensions;
},
check: function(extension_id) {
// Check if an extension is installed.
var current_extensions = XKit.installed.list();
for (i=0;i<current_extensions.length;i++) {
if (current_extensions[i] == extension_id) {
return true;
}
}
return false;
},
get: function(extension_id) {
// Returns the object.
var to_return = new Object();
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
if (app_data === "") {
to_return.errors = true;
to_return.error = "not_installed";
return to_return;
}
try {
var m_object = JSON.parse(app_data);
m_object.errors = false;
return m_object;
} catch(e) {
to_return.errors = true;
to_return.error = "parse_error";
return to_return;
}
},
update: function(extension_id, new_object) {
XKit.tools.set_setting("extension_" + extension_id, JSON.stringify(new_object));
if (XKit.installed.check(extension_id) === false) {
XKit.installed.add(extension_id);
}
},
enable: function(extension_id) {
XKit.tools.set_setting("extension__" + extension_id + "__enabled", "true");
},
disable: function(extension_id) {
XKit.tools.set_setting("extension__" + extension_id + "__enabled", "false");
},
enabled: function(extension_id) {
var m_result = XKit.tools.get_setting("extension__" + extension_id + "__enabled", "true");
if (m_result !== "false") {
return true;
} else {
return false;
}
},
version: function(extension_id) {
// Returns extension version if it's installed.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
try {
var m_object = JSON.parse(app_data);
return m_object.version;
} catch(e) {
return "";
}
},
title: function(extension_id) {
// Returns extension title if it's installed.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
try {
var m_object = JSON.parse(app_data);
return m_object.title;
} catch(e) {
return "";
}
},
developer: function(extension_id) {
// Returns extension developer if it's installed.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
try {
var m_object = JSON.parse(app_data);
return m_object.developer;
} catch(e) {
return "";
}
},
description: function(extension_id) {
// Returns extension description if it's installed.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
try {
var m_object = JSON.parse(app_data);
return m_object.description;
} catch(e) {
return "";
}
},
icon: function(extension_id) {
// Returns extension icon if it's installed.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
try {
var m_object = JSON.parse(app_data);
return m_object.icon;
} catch(e) {
return "";
}
},
css: function(extension_id) {
// Returns extension css if it's installed.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
try {
var m_object = JSON.parse(app_data);
return m_object.css;
} catch(e) {
return "";
}
}
},
progress: {
add: function(id) {
return "<div id=\"xkit-progress-" + id + "\" class=\"xkit-progress-bar\"><div id=\"xkit-progress-inner-" + id + "\" class=\"xkit-progress-bar-inner\"> </div></div>";
},
value: function(id, value) {
$("#xkit-progress-inner-" + id).css("width", value + "%");
}
},
storage: {
// Extension storage.
// Each extension is limited to 50 kb storage.
// Each extension's data is stored as a stringified JSON object.
max_area_size: 51200,
size: function(extension_id) {
return XKit.tools.get_setting("xkit_extension_storage__" + extension_id, "").length;
},
quota: function(extension_id) {
var m_size = XKit.tools.get_setting("xkit_extension_storage__" + extension_id, "").length;
return XKit.storage.max_area_size - m_size;
},
get: function(extension_id, key, default_value) {
var m_storage = XKit.storage.get_all(extension_id);
if (typeof m_storage[key] === "undefined") {
if (typeof default_value !== "undefined") {
return default_value;
} else {
return "";
}
} else {
if (!m_storage[key].value || /^\s*$/.test(m_storage[key].value)) {
return "";
}
if (isNaN(m_storage[key].value) === true && isNaN(parseFloat(m_storage[key].value)) === true) {
return m_storage[key].value;
} else {
return parseFloat(m_storage[key].value);
}
}
},
set: function(extension_id, key, value) {
var m_storage = XKit.storage.get_all(extension_id);
if (typeof m_storage[key] === "undefined") {
var m_object = new Object();
m_object.value = value;
m_storage[key] = m_object;
} else {
m_storage[key].value = value;
}
var save_this = true;
if (JSON.stringify(m_storage).length >= XKit.storage.max_area_size) {
save_this = false;
}
if (XKit.flags.do_not_limit_extension_storage === true) {
save_this = true;
}
if (save_this === false) {
XKit.storage.show_error(extension_id, JSON.stringify(m_storage).length);
return false;
} else {
var mresult = XKit.tools.set_setting("xkit_extension_storage__" + extension_id, JSON.stringify(m_storage));
if (mresult.errors === false) {
return true;
} else {
return false;
}
}
},
get_all: function(extension_id) {
var m_data = XKit.tools.get_setting("xkit_extension_storage__" + extension_id, "");
var m_object;
if (m_data === "") {
m_object = new Object();
} else {
try {
m_object = JSON.parse(m_data);
} catch(e) {
m_object = new Object();
}
}
return m_object;
},
clear: function(extension_id) {
XKit.tools.set_setting("xkit_extension_storage__" + extension_id, "");
},
show_error: function(extension_id, size) {
var m_extension = XKit.installed.get(extension_id);
var m_max_size = Math.round(XKit.storage.max_area_size / 1024);
var m_my_size = Math.round(size / 1024);
var m_html = "<b>The extension " + m_extension.title + " reached it's storage quota.</b><br/><br/>Every extension can store up to " + m_max_size + " kilobytes on your computer, but this extension is storing or trying to store ~" + m_my_size + " kilobytes.<br/><br/>You can clean the storage area of this extension, which might delete it's settings (recommended) or disable this extension.";
XKit.window.show("Extension error", m_html, "error", "<div class=\"xkit-button default\" id=\"xkit-delete-extension-storage-area\">Reset this extensions' storage and restart it</div><div class=\"xkit-button\" id=\"xkit-disable-extension-for-storage-area\">Disable this extension</div>");
$("#xkit-disable-extension-for-storage-area").click(function() {
XKit.extensions[extension_id].destroy();
XKit.installed.disable(extension_id);
XKit.window.close();
});
$("#xkit-delete-extension-storage-area").click(function() {
XKit.storage.clear(extension_id);
XKit.extensions[extension_id].destroy();
setTimeout(function() {
XKit.extensions[extension_id].run();
}, 1000);
XKit.window.close();
});
}
},
browser: function() {
var to_return = new Object();
to_return.name = "UNKNOWN";
to_return.spoofed = false;
to_return.chrome = false;
to_return.firefox = false;
to_return.safari = false;
to_return.opera = false;
to_return.version = 0;
// First, let's check if it's chrome.
if (window.chrome) {
to_return.chrome = true;
} else {
// it can still be chrome?
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
to_return.chrome = is_chrome;
}
if (to_return.chrome === true) {
// Get version.
to_return.name = "Google Chrome";
var index = navigator.userAgent.toLowerCase().indexOf("chrome/");
var real_version = parseFloat(navigator.userAgent.toLowerCase().substring(index + ("chrome/".length)));
to_return.version = real_version;
}
// Then, let's check if it's firefox.
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
to_return.name = "Mozilla Firefox";
to_return.firefox = true;
var index = navigator.userAgent.toLowerCase().indexOf("firefox/");
var real_version = parseFloat(navigator.userAgent.toLowerCase().substring(index + ("Firefox/".length)));
to_return.version = real_version;
}
// Check if there is spoofing!
// A lot of people now switch to IE.
if (navigator.userAgent.indexOf('MSIE') > -1) {
to_return.spoofed = true;
}
return to_return;
},
console: {
shown: false,
cache: "",
show: function() {
XKit.tools.add_css("#xkit_console { text-align: left; position: fixed; bottom: 0; left: 0; height: 110px; " +
" z-index: 1000000; width: 100%; background: black; color: #75fa96; font-family: Courier; " +
" padding: 10px; font-size: 10px; line-height: 14px; text-shadow: 0px 2px 3px black; " +
" overflow: scroll; background: rgba(0,0,0,0.53); }" +
" .xkit-toggle-extension-setting, .xkit-terminate-extension { " +
" text-decoration: underline; display: inline-block; " +
" cursor: pointer; margin-left: 15px; display: none; } ", "console");
$("body").append("<div id=\"xkit_console\">Welcome to XKit console!</div>");
$(document).on("click", ".xkit-terminate-extension", function() {
unload_extension($(this).attr('ext-id'));
});
$(document).on("click", ".xkit-toggle-extension-setting", function() {
var default_value = XKit.extensions[$(this).attr('ext-id')].preferences[$(this).attr('setting-id')].default;
var current_value = XKit.tools.get_extension_setting($(this).attr('ext-id'), $(this).attr('setting-id'), default_value);
XKit.console.add("current value = " + current_value);
if (current_value === true || current_value === "true") {
current_value = false;
} else {
if (current_value === false || current_value === "false") {
current_value = true;
}
}
XKit.tools.set_extension_setting($(this).attr('ext-id'), $(this).attr('setting-id'), current_value);
XKit.console.add("Default value = " + default_value + ", new value = " + current_value);
restart_extension($(this).attr('ext-id'));
});
if (XKit.console.cache !== "") {
$("#xkit_console").append("<br/>" + XKit.console.cache);
XKit.console.cache = "";
}
},
hide: function() {
XKit.tools.remove_css("console");
$("#xkit_console").remove();
},
add: function(text) {
if ($("#xkit_console").length > 0) {
$("#xkit_console").append("<br/>" + text);
var objDiv = document.getElementById("xkit_console");
objDiv.scrollTop = objDiv.scrollHeight;
} else {
XKit.console.cache = XKit.console.cache + ("<br/>" + text);
}
console.log(text);
}
},
window: {
show: function(title, msg, icon, buttons) {
show_message(title, msg, icon, buttons);
},
close: function() {
$("#xkit-window-shadow").fadeOut('fast');
$("#xkit-window-old").fadeOut('fast');
$("#xkit-window").fadeOut('fast', function() {
$(this).remove();
$("#xkit-window-shadow").remove();
$("#xkit-window-old").remove();
});
}
},
notifications: {
count: 0,
init: function() {
if (XKit.flags.disable_notifications !== true) {
$("body").append("<div id=\"xkit-notifications\"></div>");
}
},
show_error: function(id, e) {
XKit.notifications.add("<b>Can not load " + id + "</b>: " + e.message, "error");
},
add: function(message, type, sticky, callback) {
XKit.notifications.count++;
var m_class = "";
if (type === "ok") { m_class = "notification-ok"; }
if (type === "error") { m_class = "notification-error"; }
if (type === "warning") { m_class = "notification-warning"; }
if (sticky === true) {
m_class = m_class + " sticky";
}
var m_html = "<div class=\"xkit-notification " + m_class + "\" id=\"xkit_notification_" + XKit.notifications.count + "\">" +
message +
"</div>";
$("#xkit-notifications").append(m_html);
XKit.console.add(" Notification > " + message);
var m_notification_id = XKit.notifications.count;
$("#xkit_notification_" + m_notification_id).slideDown('slow');
$("#xkit_notification_" + m_notification_id).click(function() {
if(typeof callback !== undefined) {
try {
callback();
} catch(e) {
// Meh.
}
}
$("#xkit_notification_" + m_notification_id).slideUp('slow');
});
if (sticky !== true) {
setTimeout(function() {
$("#xkit_notification_" + m_notification_id).slideUp('slow');
}, 5000);
}
}
},
conflicts: {
check: function() {
var m_return = new Object;
m_return.count = 0;
m_return.fatal = false;
// Checks for modes that might break XKit.
m_return.html = "<ol>";
if ($("#xkit-control-panel-icon").length > 0) {
m_return.html = m_return.html + "<li>XKit Next found</li>";
m_return.fatal = true;
m_return.count++;
}
if ($("#xkit_top_button").length > 0) {
m_return.html = m_return.html + "<li>XKit 5.x/6.x found</li>";
m_return.fatal = true;
m_return.count++;
}
if ($("#missinge_button").length > 0) {
m_return.html = m_return.html + "<li>Missing E found (removal optional)</li>";
m_return.count++;
}
if ($("body").hasClass("dashboard_index") === true) {
var conflicting_extension = false;
if ($("#dashboard_controls_open_blog").length === 0) {
conflicting_extension = true;
}
if ($("#bt_NewPost").length > 0) {
conflicting_extension = true;
}
if ($(".likes").length === 0 || $(".following").length === 0 || $(".spotlight").length === 0) {
conflicting_extension = true;
}
if (conflicting_extension) {
m_return.html = m_return.html + "<li>Unknown extension found (removal optional)</li>";
m_return.count++;
}
}
if (m_return.fatal === true) {
XKit.tools.set_setting("xkit_disable_conflicts", "false");
}
m_return.html = m_return.html + "</ol>";
return m_return;
},
show: function(m_obj) {
if (m_obj.fatal === false) {
if (XKit.tools.get_setting("xkit_disable_conflicts", "false") === "true") {
return;
}
}
if (m_obj.fatal === false) {
XKit.window.show("Potential Extension Conflicts Found", "<b>XKit found the following potential conflicts:</b>" + m_obj.html + "It is highly recommended that you disable or remove the extensions/options listed below. You can click Ignore and Continue, which will prevent XKit from showing this window again, but no support will be provided to you if something goes wrong.", "warning", "<div class=\"xkit-button default\" id=\"xkit-disable-conflict-warning\">Ignore and Continue</div>" + "<a href=\"http://xkit.info/conflicts/\" class=\"xkit-button\">Learn More</a>");
} else {
XKit.window.show("Fatal Extension Conflicts Found", "<b>XKit found the following potential conflicts:</b>" + m_obj.html + "XKit can not continue loading before you disable other versions of XKit. Please check your browser's documentation on how to disable/remove extensions and remove other versions of XKit.", "warning", "<a href=\"http://xkit.info/conflicts/\" class=\"xkit-button\">Learn More</a>");
}
$("#xkit-disable-conflict-warning").click(function() {
XKit.tools.set_setting("xkit_disable_conflicts", "true");
XKit.window.close();
});
}
},
tools: {
init_css: function(ext_id) {
var my_css = XKit.installed.get(ext_id).css;
if (my_css !== "") {
XKit.tools.add_css(my_css, ext_id);
}
},
add_css: function(css, ext_id) {
if ($("#xkit-css-added-by-" + ext_id).length == 0) {
$("head").append("<style id=\"xkit-css-added-by-" + ext_id + "\">" + css + "</style>");
} else {
$("#xkit-css-added-by-" + ext_id).append(css);
}
},
remove_css: function(ext_id) {
$("#xkit-css-added-by-" + ext_id).remove();
},
random_string: function() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 30; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
},
get_blogs: function() {
var m_blogs_to_save = "";
var m_blogs = new Array();
if ($("#popover_blogs").length > 0) {
$("#popover_blogs > .popover_inner").children(".item").not(":last-child").each(function(index, obj) {
var mX = $(this).attr("id");
mX = mX.substring(9, mX.length);
m_blogs_to_save = m_blogs_to_save + ";" + mX;
m_blogs.push(mX);
});
XKit.tools.set_setting("xkit_cached_blogs", m_blogs_to_save);
return m_blogs;
} else {
var m_blogs = XKit.tools.get_setting("xkit_cached_blogs","");
if (m_blogs !== "") {
return m_blogs.split(";");
}
}
},
replace_all: function(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'),with_this);
},
get_setting: function(setting_name, default_value) {
try {
return GM_getValue(setting_name, default_value);
} catch(e) {
return "";
}
},
set_setting: function(setting_name, new_value) {
var to_return = new Object();
try {
GM_setValue(setting_name, new_value);
to_return.errors = false;
return to_return;
} catch(e) {
XKit.console.add("Can not save " + setting_name + ": " + e.message);
to_return.errors = true;
to_return.error = e.message;
return to_return;
}
},
get_extension_setting: function(ext_id, setting_name, default_value) {
var m_value = GM_getValue(ext_id + "_____" + setting_name, default_value);
if (m_value === "true" || m_value === true) {
return true;
} else {
if (m_value === "false" || m_value === false) {
return false;
} else {
return m_value;
}
}
},
set_extension_setting: function(ext_id, setting_name, new_value) {
return GM_setValue(ext_id + "_____" + setting_name, new_value);
},
add_function: function(func, exec, addt) {
try { var script = document.createElement("script");
addt = addt.replace(/(\r\n|\n|\r)/gm,"");
script.textContent = "var add_tag = '" + addt + "';";
script.textContent = script.textContent + (exec ? "(" : "") + func.toString() + (exec ? ")();" : "");
document.body.appendChild(script); } catch(e) { alert(e.message); }
}
},
post_listener: {
callbacks: [],
callback_ids: [],
count: 0,
add: function(id, func) {
XKit.post_listener.callbacks.push(func);
XKit.post_listener.callback_ids.push(id);
},
remove: function(id) {
var m_id = XKit.post_listener.callback_ids.indexOf(id);
if (m_id !== -1) {
XKit.post_listener.callback_ids.splice(m_id, 1);
XKit.post_listener.callbacks.splice(m_id, 1);
}
},
check: function() {
if ($("#posts").length === 0) {
return;
}
var post_count = $("#posts > .post").length;
if (XKit.post_listener.count === 0) {
XKit.post_listener.count = post_count;
} else {
if (post_count !== XKit.post_listener.count) {
XKit.post_listener.count = post_count;
XKit.post_listener.run_callbacks();
}
}
setTimeout(XKit.post_listener.check, 3500);
},
run_callbacks: function() {
if (XKit.post_listener.callbacks.length === 0) {
return;
}
for (i=0;i<XKit.post_listener.callbacks.length;i++) {
try {
XKit.post_listener.callbacks[i]();
} catch(e) {
console.log("Can not call callback with id " + XKit.post_listener.callback_ids[i] + ": " + e.message);
}
}
}
},
special: {
reset: function() {
XKit.window.show("Reset XKit","Really delete all the data stored in XKit?<br/>Your settings will be lost. You can not undo this action.","question","<div id=\"reset-xkit-yes\" class=\"xkit-button default\">Yes, reset XKit</div><div id=\"reset-xkit-no\" class=\"xkit-button\">Cancel</div>");
$("#reset-xkit-no").click(function() {
XKit.window.close();
});
$("#reset-xkit-yes").click(function() {
$(this).addClass("disabled");
$("#reset-xkit-no").addClass("disabled");
$(this).html("Resetting, please wait...");
XKit.shutdown();
setTimeout(function() {
GM_deleteAllValues(function() {
XKit.window.show("Reset complete.","XKit finished resetting itself.<br/>Please restart your browser and visit your dashboard to re-install XKit.","info","<div id=\"reset-xkit-close\" class=\"xkit-button default\">OK</div>");
$("#reset-xkit-close").click(function() {
XKit.window.close();
});
});
}, 500);
});
}
},
shutdown: function() {
for(var ext in XKit.extensions) {
try {
XKit.console.add("Shutting down " + ext + "...");
XKit.tools.remove_css(ext);
XKit.extensions[ext].destroy();
} catch(e) {
XKit.console.add("Can not shut down " + ext + ".");
continue;
}
}
}