forked from Tylian/XKit
-
Notifications
You must be signed in to change notification settings - Fork 135
/
xkit.js
executable file
·4827 lines (4131 loc) · 150 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 = [];
var xkit_global_start = Date.now(); // log start timestamp
(function() {
if (typeof XKit !== "undefined") { return; }
XKit = {
version: framework_version,
api_key: "kZSI0VnPBJom8cpIeTFw4huEh9gGbq4KfWKY7z5QECutAAki6D",
page: {
standard:
window.window === window.top &&
document.location.href.indexOf("://www.tumblr.com/dashboard/iframe?") === -1,
form_frame:
document.location.href.indexOf("://www.tumblr.com/neue_web/iframe/") !== -1,
ask_frame:
document.location.href.indexOf("://www.tumblr.com/ask_form/") !== -1,
blog_frame:
document.location.href.indexOf("://www.tumblr.com/send") !== -1 ||
document.location.href.indexOf("://www.tumblr.com/dashboard/iframe?") !== -1 ||
document.location.href.indexOf("://secure.assets.tumblr.com/assets/html/iframe/") !== -1,
xkit:
document.location.href.indexOf('://www.tumblr.com/xkit_') !== -1
},
init: function() {
if (!XKit.page.xkit) {
XKit.init_flags();
}
$(document).ready(XKit.init_extension);
},
init_extension: function() {
console.log("init_extension: " + JSON.stringify(XKit.page));
if (XKit.page.xkit) {
xkit_init_special();
return;
}
if (XKit.page.standard || XKit.page.form_frame) {
XKit.init_normal();
return;
}
if (XKit.page.ask_frame || XKit.page.blog_frame) {
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 get in contact with New XKit Support.", "error", "<div id=\"xkit-close-message\" class=\"xkit-button default\">OK</div><a href=\"https://new-xkit-support.tumblr.com\" class=\"xkit-button\">Support</a>");
return;
}
var conflicts_check_results = XKit.conflicts.check();
if (conflicts_check_results.count > 0) {
if (XKit.tools.get_setting("xkit_disable_conflicts", "false") !== "true") {
XKit.conflicts.show(conflicts_check_results);
}
if (conflicts_check_results.fatal === true) {
return;
}
}
// Load dashboard extensions.
XKit.notifications.init();
var m_browser = XKit.browser();
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=\"https://new-xkit-support.tumblr.com\" class=\"xkit-button\">Support</a>");
return;
}
xkit_check_storage();
if (XKit.tools.get_setting("xkit_installation_complete", "") !== "true") {
// We need to install XKit!
console.log("Installation complete is not true, it is = " + XKit.tools.get_setting("xkit_installation_complete", ""));
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) {
console.log("xkit_main not found! Re-installing....");
xkit_install();
return;
}
// Before we run main--if patches is a broken version,
// we need to force an update.
if (XKit.installed.version('xkit_patches') === '7.2.8') {
XKit.special.force_update();
}
// It exists! Great.
var xkit_main = XKit.installed.get("xkit_main");
if (!xkit_main.errors && xkit_main.script) {
console.log("Trying to run xkit_main.");
try {
new Function(xkit_main.script + "\n//# sourceURL=xkit/xkit_main.js")();
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?
console.log("xkit_main not found! Re-installing....");
xkit_install();
return;
}
if (xkit_main.error == "parse_error") {
// Corrupt storage? Recomment reset.
console.error("xkit_main is corrupt!");
show_error_reset("Package xkit_main is corrupted!");
return;
}
}
} catch (e) {
console.error(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) {
console.log("xkit_main not found! Quitting.");
return;
}
// It exists! Great.
var xkit_main = XKit.installed.get("xkit_main");
if (!xkit_main.errors && xkit_main.script) {
console.log("Trying to run xkit_main.");
try {
new Function(xkit_main.script + "\n//# sourceURL=xkit/xkit_main.js")();
XKit.frame_mode = true;
XKit.extensions.xkit_main.run();
} catch (e) {
console.error("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);
},
extensions: {},
download: {
// TODO: implement as module, lose most of this code
github_fetch: function(path, callback) {
var url = 'https://new-xkit.github.io/XKit/Extensions/dist/' + path;
GM_xmlhttpRequest({
method: "GET",
url: url,
onerror: function(response) {
console.error("Unable to download '" + path + "'");
callback({errors: true, server_down: true});
},
onload: function(response) {
// We are done!
var mdata = {};
try {
mdata = JSON.parse(response.responseText);
} catch (e) {
// Server returned bad thingy.
console.error("Unable to download '" + path +
"', server returned non-json object." + e.message);
callback({errors: true, server_down: true});
return;
}
callback(mdata);
}
});
},
extension: function(extension_id, callback) {
getExtensionData(extension_id).then(callback);
},
page: function(page, callback) {
if (page === 'list.php') {
getListData().then(callback);
return;
}
if (page === 'gallery.php') {
getGalleryData().then(callback);
return;
}
if (page === 'themes/index.php') {
getThemeGalleryData().then(callback);
return;
}
if (page === 'paperboy/index.php') {
XKit.download.github_fetch('page/paperboy.json', callback);
return;
}
if (page === 'framework_version.php') {
XKit.download.github_fetch('page/framework_version.json', callback);
return;
}
}
},
install: function(extension_id, callback) {
// Installs the extension.
XKit.download.extension(extension_id, function(mdata) {
console.log("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 = {extensions: []};
}
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 (var i = 0; i < current_extensions.length; i++) {
if (current_extensions[i] == extension_id) {
current_extensions.splice(i, 1);
}
}
var installed_extensions = {extensions: current_extensions};
var 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 = {extensions: []};
}
return installed_extensions.extensions;
},
check: function(extension_id) {
// Check if an extension is installed.
var current_extensions = XKit.installed.list();
for (var i = 0; i < current_extensions.length; i++) {
if (current_extensions[i] == extension_id) {
return true;
}
}
return false;
},
is_running: function(extension) {
return XKit.installed.check(extension) &&
typeof(XKit.extensions[extension]) !== "undefined" &&
XKit.extensions[extension].running;
},
when_running: function(extension, onRunning, onFailure) {
if (!XKit.installed.check(extension)) {
if (onFailure) {
onFailure();
}
return;
}
// Wait up to 8 seconds for the extension to begin running
var tries = 20;
var timeout = 400;
function check() {
if (tries < 0) {
if (onFailure) {
onFailure();
}
return;
}
if (!XKit.installed.is_running(extension)) {
tries--;
setTimeout(check, timeout);
return;
}
// The extension exists and has been installed
onRunning(XKit.extensions[extension]);
}
setTimeout(check, 0);
},
get: function(extension_id) {
// Returns the object.
var app_data = XKit.tools.get_setting("extension_" + extension_id, "");
if (app_data === "") {
return {
errors: true,
error: "not_installed"
};
}
try {
var m_object = JSON.parse(app_data);
m_object.errors = false;
return m_object;
} catch (e) {
return {
errors: true,
error: "parse_error"
};
}
},
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.
return XKit.installed.get(extension_id).version || "";
},
title: function(extension_id) {
// Returns extension title if it's installed.
return XKit.installed.get(extension_id).title || "";
},
developer: function(extension_id) {
// Returns extension developer if it's installed.
return XKit.installed.get(extension_id).developer || "";
},
description: function(extension_id) {
// Returns extension description if it's installed.
return XKit.installed.get(extension_id).description || "";
},
script: function(extension_id) {
// Returns extension script if it's installed.
return XKit.installed.get(extension_id).script || "";
},
icon: function(extension_id) {
// Returns extension icon if it's installed.
return XKit.installed.get(extension_id).icon || "";
},
css: function(extension_id) {
// Returns extension css if it's installed.
return XKit.installed.get(extension_id).css || "";
}
},
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,
unlimited_storage: false,
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") {
m_storage[key] = {value: value};
} 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) {
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;
}
}
},
remove: function(extension_id, key) {
var m_storage = XKit.storage.get_all(extension_id);
if (!m_storage.hasOwnProperty(key)) return true;
delete m_storage[key];
// We're going to skip the storage limit checks, on the basis that:
// - the previous serialisation should have fit within limits; and
// - deletion shouldn't increase the size of our new serialisation.
var mresult = XKit.tools.set_setting("xkit_extension_storage__" + extension_id, JSON.stringify(m_storage));
return !mresult.errors;
},
get_all: function(extension_id) {
var m_data = XKit.tools.get_setting("xkit_extension_storage__" + extension_id, "");
if (m_data !== "") {
try {
return JSON.parse(m_data);
} catch (e) {
}
}
return {};
},
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();
});
}
},
/**
* @return {Object} An overview of the browser's information:
* name: "Google Chrome" | "Mozilla Firefox" | "Apple Safari" - The browser's human-readable name
* spoofed: boolean - Whether XKit suspects the user of spoofing an IE user agent.
* chrome: boolean - Whether the browser is Chrome
* firefox: boolean - Whether the browser is Firefox
* safari: boolean - Whether the browser is Safari
* opera: boolean - Whether the browser is Opera (Always false as implemented currently)
* version: number - The numerical representation of the browser's version or 0 if unknown
* mobile: boolean - Whether Tumblr is serving the mobile version of the site
*/
browser: function() {
var to_return = {};
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;
to_return.mobile = false;
// First, let's check if it's chrome.
if (window.chrome) {
to_return.chrome = true;
} else {
// it can still be chrome?
var is_chrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
to_return.chrome = is_chrome;
}
function get_ua_version(user_agent) {
var index = navigator.userAgent.toLowerCase().indexOf(user_agent);
var real_version = parseFloat(navigator.userAgent.toLowerCase().substring(index + (user_agent.length)));
return real_version;
}
if (to_return.chrome === true) {
// Get version.
to_return.name = "Google Chrome";
to_return.version = get_ua_version("chrome/");
}
// Then, let's check if it's firefox.
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
to_return.name = "Mozilla Firefox";
to_return.firefox = true;
to_return.version = get_ua_version("firefox/");
}
// Blahblah Safari blah.
if (/Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor)) {
to_return.name = "Apple Safari";
to_return.safari = true;
to_return.firefox = false;
to_return.version = get_ua_version("safari/");
}
to_return.ug = navigator.userAgent.toLowerCase();
// Check if there is spoofing!
// A lot of people now switch to IE.
if (navigator.userAgent.indexOf('MSIE') > -1) {
to_return.spoofed = true;
}
// Check if you're viewing the mobile version
if ($('.is_mobile').length > 0) {
to_return.mobile = true;
}
return to_return;
},
iframe: {
/**
* @return {String} Id of blog which the iframe refers to (usually
* the blog in which the iframe is embedded)
*/
get_tumblelog: function() {
var new_channel_id = document.location.href.match(/[&?]tumblelogName=([\w-]+)/);
var archive_channel_id = document.location.href.match(/[&?]name=([\w-]+)/);
var old_channel_id = $("#tumblelog_name").attr('data-tumblelog-name');
return (new_channel_id && new_channel_id[1]) || (archive_channel_id && archive_channel_id[1]) || old_channel_id;
},
/**
* @return {String} Post to which this iframe refers
*/
single_post_id: function() {
var all_post_ids = document.location.href.match(/[&?](singlePostId|pid|postId)=(\d+)/);
return all_post_ids[2];
},
/**
* @return {String} Form key of the iframe (the data to use in a
* reblog or other API request)
*/
form_key: function() {
var new_form_key = $("meta[name=tumblr-form-key]").attr("content");
var old_form_key = $(".btn.reblog").attr('data-form-key');
return new_form_key || old_form_key;
},
/**
* Hide the text of a button in the iframe.
* @param {JQuery} button
*/
hide_button: function(button) {
button.addClass("no_label");
button.addClass("no-text").contents()
.filter(function() {
return this.nodeType === 3;
}).wrap('<span class="hidden">');
button.children(".button-label").hide();
},
/**
* @param {String} name: the css class name of the button
* @return {JQuery} the element for that css class name
*/
tx_button_selector: function(name) {
return $(`.tx-button.${name}-button, .tx-icon-button.${name}-button`);
},
/**
* @return {JQuery} The follow button in the iframe
*/
follow_button: function() {
return this.tx_button_selector("follow");
},
/**
* @return {JQuery} The unfollow button in the iframe
*/
unfollow_button: function() {
return this.tx_button_selector("unfollow");
},
/**
* @return {JQuery} The delete button in the iframe
*/
delete_button: function() {
return this.tx_button_selector("delete");
},
/**
* @return {JQuery} The reblog button in the iframe
*/
reblog_button: function() {
return this.tx_button_selector("reblog");
},
/**
* @return {JQuery} The dashboard button in the iframe
*/
dashboard_button: function() {
return this.tx_button_selector("dashboard");
},
/**
* Uses tumblr's built-in RPC functionality to resize the in-blog iframe.
* The iframe will be resized to the maximum of the current body size or
* the iframe-controls-container size.
*
* This is the same postMessage call that happens when you click on the
* profile menu, without the body classes arguments. (Tumblr also allows
* for the code to set classes on the iframe element and the body of the page)
*/
size_frame_to_fit: function() {
var button_container = $(".iframe-controls-container")[0] || {};
var width = Math.max(
button_container.scrollWidth || -Infinity,
document.body.scrollWidth);
var height = Math.max(
button_container.scrollHeight || -Infinity,
document.body.scrollHeight);
var payload = {
method: "tumblr-unified-controls:IframeControls:size",
args: [{
width: width,
height: height
}]
};
window.top.postMessage(JSON.stringify(payload), "*");
}
},
window: {
/**
* Show an XKit alert window
* @param {String} title - Text for alert window's title bar
* @param {String} msg - Text for body of window, can be HTML
* @param {"error"|"warning"|"question"|"info"} icon - Window's
* icon type, determined by CSS class `icon`.
* See also xkit.css.
* @param {String} buttons - The HTML to be used in the button area of the window.
* Usually divs with class "xkit-button".
* @param {boolean} wide - Whether the XKit window should be wide.
*/
show: function(title, msg, icon, buttons, wide) {
if (typeof icon === "undefined") {
icon = "";
}
var additional_classes = "";
if (wide) {
additional_classes = "xkit-wide-window";
}
if ($("#xkit-window").length > 0) {
$("#xkit-window").attr('id', "xkit-window-old");
$("#xkit-window-old").fadeOut('fast', function() {
$(this).remove();
});
}
var m_html = "<div id=\"xkit-window\" class=\"" + icon + " " + additional_classes + "\" style=\"display: none;\">" +
"<div class=\"xkit-window-title\">" + title + "</div>" +
"<div class=\"xkit-window-msg\">" + msg + "</div>";
if (typeof buttons !== "undefined") {
m_html = m_html + "<div class=\"xkit-window-buttons\">" + buttons + "</div>";
}
if ($("#xkit-window-shadow").length === 0) {
m_html = m_html + "</div><div id=\"xkit-window-shadow\"></div>";
}
$("body").prepend(m_html);
$("#tiptip_holder").css("z-index", "99000000");
centerIt($("#xkit-window"));
$("#xkit-window").fadeIn('fast');
$("#xkit-close-message").click(function() {
$("#xkit-window-shadow").fadeOut('fast', function() {
$(this).remove();
});
$("#xkit-window").fadeOut('fast', function() {
$(this).remove();
});
});
},
close: function() {
$("#xkit-window-shadow").fadeOut('fast');
$("#xkit-window-old").fadeOut('fast');
$("#tiptip_holder").css("z-index", "99999");
$("#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) {
if ($("#xkit-notifications").length <= 0) {
setTimeout(function() { XKit.notifications.add(message, type, sticky, callback); }, 500);
return;
}
XKit.notifications.count++;
var m_class = "";
if (type === "mail") { m_class = "notification-mail"; }
if (type === "ok") { m_class = "notification-ok"; }
if (type === "error") { m_class = "notification-error"; }
if (type === "warning") { m_class = "notification-warning"; }
if (type === "pokes") { m_class = "notification-pokes"; }
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);
// console.log(" Notification > " + message);
var m_notification_id = XKit.notifications.count;
setTimeout(function() {
$("#xkit_notification_" + m_notification_id).slideDown('slow');
}, 100);
$("#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);
}
}
},
toast: {
count: 0,
/**
* Simulates a tumblr notification ("toast")
* @param {Boolean} created - true if post was not queued/drafted
* @param {String} action - post action description (i.e. "Reblogged to ")
* @param {String} url - tumblr blog name (for both notification and API)
* @param {Integer/String} id - created post id for peepr (optional)
* @param {String} crumb - arbitrary class for "crumb" (optional)
*/
add: function(created, action, url, id, crumb) {
var toastno = XKit.toast.count;
XKit.toast.count++;
if (created) {
action = "<strong>" + action;
} else {
action += "<strong>";
}
var endData = "}";
if (typeof id !== "undefined") {
endData = `,"postId": ${id}}`;
}
if (typeof crumb === "undefined") {
crumb = "";
}
var toast =
`<li class="toast blog-sub xtoast-${toastno}" data-subview="toast" data-peepr='{"tumblelog": "${url}"${endData}'>
<img class="toaster avatar" src="https://api.tumblr.com/v2/blog/${url}/avatar/128">
<div class="crumb ${crumb}"></div>
<span class="toast-bread">
${action}${url}</strong>
</span>
</li>`;
$(".toastr").addClass("show-toast");
$(".multi-toasts").append(toast);
var $toast = $(".xtoast-" + toastno);
setTimeout(function() {
$toast.addClass("fade-out");
}, 4000);
setTimeout(function() {
$toast.remove();
if (!$(".toast").length) {
$(".toastr").removeClass("show-toast");
}
}, 5000);
}
},
conflicts: {
check: function() {
var m_return = {
count: 0,
fatal: false,
// Checks for modes that might break XKit.
html: "<ol>"
};
if ($("#xkit-control").length > 0) {
m_return.html = m_return.html + "<li>Old XKit found</li>";
m_return.fatal = true;
m_return.count++;
}
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 ($("archive_btn").length > 0) {
m_return.html = m_return.html + "<li>Archive Poster found (removal optional)</li>";
m_return.count++;
}
if ($("body").hasClass("dashboard_index") === true) {
var conflicting_extension = false;
if ($("#bt_NewPost").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>New 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>");
} else {
XKit.window.show("Fatal Extension Conflicts Found", "<b>New XKit found the following 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");
}
$("#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;
},
blog_list_message_listener: function(e) {
if (e.origin == window.location.protocol + "//" + window.location.host &&
e.data.hasOwnProperty("xkit_blogs")) {
XKit.blogs_from_tumblr = e.data.xkit_blogs.map(XKit.tools.escape_html);
XKit.tools.set_setting('xkit_cached_blogs', XKit.blogs_from_tumblr.join(';'));