-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatTopBar.user.js
More file actions
1584 lines (1373 loc) · 73.6 KB
/
ChatTopBar.user.js
File metadata and controls
1584 lines (1373 loc) · 73.6 KB
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
// ==UserScript==
// @name Top bar in chat.
// @namespace https://stackexchange.com/users/305991/jason-c
// @version 1.14.1
// @description Add a fully functional top bar to chat windows.
// @author Jason C
// @include /^https?:\/\/chat\.meta\.stackexchange\.com\/rooms\/[0-9]+.*$/
// @include /^https?:\/\/chat\.stackexchange\.com\/rooms\/[0-9]+.*$/
// @include /^https?:\/\/chat\.stackoverflow\.com\/rooms\/[0-9]+.*$/
// @match *://chat.meta.stackexchange.com/chats/join/favorite?ctbjoin
// @match *://chat.stackexchange.com/chats/join/favorite?ctbjoin
// @match *://chat.stackoverflow.com/chats/join/favorite?ctbjoin
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_listValues
// @grant GM_deleteValue
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
const NAMESPACE_UID = '9d2798e5-6d48-488c-924e-899a39b74954';
// Firefox support: Store working copy of settings in tbData.
migrateSettings();
var tbData = {settings: {}, scriptVersion: GM_info.script.version, uid: NAMESPACE_UID };
for (let key of GM_listValues())
tbData.settings[key] = GM_getValue(key);
// Firefox support: Use events for settings instead of GM_* directly.
window.addEventListener(`setvalue-${NAMESPACE_UID}`, function (ev) {
GM_setValue(ev.detail.key, ev.detail.value);
});
// Firefox support: Use events for settings instead of GM_* directly.
window.addEventListener(`deletevalue-${NAMESPACE_UID}`, function (ev) {
GM_deleteValue(ev.detail.key);
});
// Firefox support: Make this easier to debug in FF. Instead of letting GM/Tampermonkey
// run the script, inject it into the page and run it from there.
(function (f, data) {
let funcstr = f.toString();
let datastr = JSON.stringify(data);
let nsstr = encodeURI(GM_info.script.namespace.replace(/\/?$/, '/'));
let namestr = encodeURIComponent(GM_info.script.name);
let script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = `(${funcstr})(window.jQuery, ${datastr})\n\n//# sourceURL=${nsstr}${namestr}`;
document.body.appendChild(script);
})(MakeChatTopbar, tbData);
// From here on out, this is executed in the unprivileged context of the page itself.
function MakeChatTopbar ($, tbData) {
// No jQuery? This is not a chat page that we can enhance!
if (!$)
return;
// Auto-click the button on the favorites page if enabled. Note match rule only
// lets this happen if '?ctbjoin' is in URL, so we're not doing this willy nilly.
if (document.location.href.includes('/chats/join/favorite')) {
$('input[value*="join"]').click();
return;
}
// Don't add the top bar if we're in an iframe. This is only here to make this
// compatible with some other scripts I'm working on. Also never load it on mobile
// versions of the site.
try {
if ($('body').hasClass('mob')) {
log('Not running on mobile site', true);
return;
} else if (window.self !== window.top && !setRunInFrame()) {
log('Not running in iframe', true);
return;
}
} catch (e) {
// If browser blocked access to window.top or something, just run. Better to
// run by accident than not run by accident.
}
const RECONNECT_WAIT_MS = 500;
const AUTO_SEARCH_DELAY_MS = 500;
const URL_UPDATES = 'https://stackapps.com/q/7404/25350';
const URL_MORE = 'https://stackapps.com/search?tab=active&q=user%3a25350%20is%3aq%20%5bscript%5d%20';
// Add a couple useful jQuery functions that we'll use below.
$.fn.extend({
ctb_noclick: function () { return this.removeAttr('href').off('click').click(() => false); },
ctb_linkify: function (no) { return no ? this : this.html(linkify(this.html())); }
});
// The main chat server page has a topbar and is on the same domain, load it up
// in an invisible iframe. Note: We load /faq, because / and the various room tab
// pages generally make periodic requests to e.g. /rooms and stuff to keep their
// info up to date, and there's no reason for us to be making background requests
// that we don't need. The /faq page doesn't do any periodic XHR stuff.
var frame = $('<iframe/>')
.css('display', 'none')
.attr('src', '/faq')
.appendTo('body');
// Start grabbing the account ID while the frame is loading to minimize load time.
var defAccountId = getAccountId();
// Start loading jQuery UI and jQuery-mousewheel dependencies at the same time, too.
var defJQUI = $('script[src*="jquery-ui"]').length > 0 ? $.when() : $.when(
$.Deferred(function (def) {
$('<link/>')
.attr('rel', 'stylesheet')
.attr('href', '//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css')
.appendTo('head')
.load(def.resolve);
}),
$.Deferred(function (def) {
// jQuery hates adding script tags (https://stackoverflow.com/q/610995)
let s = document.createElement('script');
s.src = '//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js';
s.onload = def.resolve;
document.head.appendChild(s);
}),
$.Deferred(function (def) {
// Unfortunately, mousewheel isn't available on googleapis. I was hoping to not
// add other hosts in case the user has them blocked. TODO: Remove mousewheel
// dependency, this is only used to help prevent dropdown list overscrolling
// from scrolling the chat room.
let s = document.createElement('script');
s.src = '//cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js';
s.onload = def.resolve;
s.onerror = function (e) {
log('Failed to load jQuery-mousewheel, overscroll protection won\'t work, no big deal.', true);
def.resolve(); // proceed anyway
};
document.head.appendChild(s);
})
);
// Provide a console interface for certain functionality. (TBD: Should I be paranoid
// about this and not do it until *after* frame and styles are loaded? Or maybe just
// don't expose settings change methods any more since there's a dialog now? Hmm...)
window.ChatTopBar = {
setWiden: setWiden,
setThemed: setThemed,
setBrightness: setBrightness,
setQuiet: setQuiet,
setShowSwitcher: setShowSwitcher,
setRejoinOnSwitch: setRejoinOnSwitch,
setOpenRoomsHere: setOpenRoomsHere,
setAutoSearch: setAutoSearch,
setSearchByActivity: setSearchByActivity,
setLinkifyDescriptions: setLinkifyDescriptions,
setFaviconVisible: setFaviconVisible,
setFaviconStyle: setFaviconStyle,
setCompactResults: setCompactResults,
setAutoLoadMore: setAutoLoadMore,
setPreserveSearch: setPreserveSearch,
setRunInFrame: setRunInFrame,
showChangeLog: showChangeLog,
forgetAccount: () => forgetSetting('account'),
forgetEverything: forgetEverything,
dumpSettings: dumpSettings,
fakeUnreadCounts: fakeUnreadCounts
};
// Once the frame is loaded, everything happens.
frame.load(function () {
var tbframe = frame[0].contentWindow;
var topbar = tbframe.$('.topbar');
var link = topbar.parent().find('link[rel="stylesheet"][href*="topbar"]');
// tbframe.StackExchange.options.enableLogging = true;
// Make a new link instead of stealing the existing one to force a reload so we
// can hook it below. If we simply move the link, while the CSS does reload, it
// doesn't seem to trigger a load() callback.
link = $('<link/>')
.attr('rel', 'stylesheet')
.attr('href', link.attr('href'));
// Steal topbar from iframe. Also change its jQuery to be ours so that deferred
// load functions and stuff (i.e. the site switcher search box) can find topbar
// elements (yeah, hack alert).
$('head').append(link);
$('body').prepend(topbar);
tbframe.$ = tbframe.jQuery = jQuery;
// Float topbar at top of window.
topbar.css({
position: 'fixed',
top: 0,
'z-index': 100,
opacity: 1
});
// Search box ID conflicts with sidebar searchbox, breaks styling. Change it.
topbar.find('#searchbox').attr('id', 'topbar_searchbox');
// Make search box placeholders a little more accurate (todo: localize?).
$('#topbar_searchbox').attr('placeholder', 'search all rooms');
$('#searchbox').attr('placeholder', 'search room');
// Install DOM mutation observers for modifying SE dropdown when it's loaded.
watchSEDropdown(topbar);
// Add chat room search dropdown.
createRoomSearchDropdown(topbar);
// Hide topbar dropdowns (and settings dialog) on click (the SE JS object is in the frame).
$(window).click(function (e) {
if (e.target.tagName.toLowerCase() === 'a' || $(e.target).closest('.topbar-dialog').length === 0) {
tbframe.StackExchange.topbar.hideAll();
toggleRoomSearchDropdown('clickout');
}
hideSettingsIfOutside(e.target);
});
$('.avatar, .action-link, #room-menu').click(function (e) {
tbframe.StackExchange.topbar.hideAll();
toggleRoomSearchDropdown('clickout');
hideSettingsIfOutside(e.target);
});
// The icon-flag thing is static, and does not disappear on click or respond
// to realtime messages. If it happened to be there, remove it
// when the user clicks on it so it doesn't stick around forever.
topbar.find('.icon-flag').click(function () {
$(this).toggle(false);
return true;
});
// Must wait for css to load before topbar.height() and other styles become valid.
link.load(function () {
// Initialize configurable settings.
setWiden();
setThemed();
setBrightness();
setFaviconVisible();
setFaviconStyle();
setCompactResults();
setSearchByActivity(); // Sets data-mc-result-sort for compact mode styling.
// Put settings link at bottom; we're doing this in here so that we don't make the
// dialog available to the user before styles are loaded. Probably being paranoid.
defJQUI.then(function () {
$('#footer-legal')
.prepend(document.createTextNode(' | '))
.prepend($('<a href="#" id="ctb-settings-link"/>').text('topbar').click(() => (showSettings(), false)));
checkUpdateNotify();
});
// Put a white div behind it, easier than trying to futz with opacity component of
// background color.
$('<div/>').css({
background: 'white',
margin: 0,
padding: 0,
position: 'fixed',
top: 0,
'z-index': 99,
height: `${topbar.height()}px`,
width: '100%'
}).prependTo('body');
// Do stuff to compensate for topbar size: Make room for the topbar, sidebar must
// be made smaller so it doesn't hide behind the bottom panel, etc.
$('#container, #sidebar').css({
'margin-top': `${topbar.height()}px`
});
$('#sidebar').css({
height: `calc(100% - ${topbar.height()}px)`
});
$(window).trigger('resize'); // Force sidebar resize, guess SE does it dynamically.
installReplyScrollHandler(topbar.height()); // Also take over scrolling for reply buttons, see comments there.
});
// So, the chat topbar doesn't show realtime notifications (https://meta.stackexchange.com/q/296714/230261),
// for a number of reasons. We have to re-implement this ourselves by setting up
// a websocket and subscribing to topbar events (for which we need the user's network
// account ID).
defAccountId.then(function (id) {
if (id === null) {
log('Not opening WebSocket (no account ID).');
} else {
let realtimeConnect = function () {
log('Opening WebSocket...');
let ws = new WebSocket('wss://qa.sockets.stackexchange.com');
ws.onopen = function () {
log(`WebSocket opened (your network ID is ${id}).`);
ws.send(`${id}-topbar`);
};
ws.onmessage = function (event) {
if (event && event.data) {
try {
var tbevent = JSON.parse(event.data);
if (tbevent && tbevent.data)
tbframe.StackExchange.topbar.handleRealtimeMessage(tbevent.data);
} catch (e) {
// Just ignore, it's a JSON parse error, means event.data wasn't a string or something.
}
}
};
ws.onerror = function (event) {
log(`WebSocket error: ${event.code} (${event.reason})`);
};
ws.onclose = function (event) {
log(`WebSocket closed: ${event.code} (${event.reason}), will reopen in ${RECONNECT_WAIT_MS} ms.`);
window.setTimeout(realtimeConnect, RECONNECT_WAIT_MS);
};
};
realtimeConnect();
}
});
});
// Grab the user's network account ID, which doesn't seem to be directly available.
// anywhere on the chat page. Needed for websocket topbar event subscription. This
// is done like so:
//
// 1. Grab /users/thumbs/<chat_id>
// 2. Parse a site name and site user ID out of the parent profile link.
// 3. Use the API to get the network account ID.
//
// To save precious API quota, this value is cached, and only requeried if the fkey
// on the chat page changes, which I figure is a pretty good indicator that e.g.
// the user logged out then logged in with a different account. The fkey is tracked
// per chat server to prevent unneeded API calls when switching back and forth from
// multiple chat servers. Returns a promise.
function getAccountId () {
// If user is not logged in CHAT.CURRENT_USER_ID will be 0.
return $.Deferred(function (def) {
if (CHAT.CURRENT_USER_ID === 0) {
log('Cannot get account ID: You are not logged in.');
def.resolve(null);
return;
}
let server = window.location.host;
let fkey = $('#fkey').val();
let account_cached = load('account', null);
if (fkey !== load(`fkey-${server}`, null) || !account_cached) {
log(`Obtaining parent profile (your chat ID is ${CHAT.CURRENT_USER_ID})...`);
$.get(`/users/thumbs/${CHAT.CURRENT_USER_ID}`, function (data) {
let a = document.createElement('a');
a.href = data.profileUrl;
let site = a.hostname;
let uid = /\/users\/([0-9]+)/.exec(a.pathname)[1];
log(`Obtaining network ID (your parent ID is ${uid} on ${site})...`);
$.get(`//api.stackexchange.com/2.2/users/${uid}?order=desc&sort=reputation&site=${site}&filter=TiTab6.mdk`, function (r) {
if (r.items && r.items.length > 0) {
store('account', r.items[0].account_id);
store(`fkey-${server}`, fkey);
def.resolve(r.items[0].account_id);
}
});
});
} else {
def.resolve(account_cached);
}
}).promise();
}
// https://github.com/JC3/SEUserScripts/issues/9: Clicking reply links scrolls
// to comment but hides under topbar. However, they aren't actual anchor links,
// the scrolling is handled in master-chat.js, so there's no target anchors to
// offset. Also the scrollbar is on the body so any kind of spacer we add
// scrolls along with chat and doesn't help. So we have to reimplement the SE
// scrolling behavior. This function installs a new click event handler for all
// .reply-info buttons on the page, and also monitors for new ones so that they
// can be modified as well.
function installReplyScrollHandler (correction) {
// Watch for new .reply-info's so we can take over their scroll behavior.
new MutationObserver((ms) => ms.forEach((m) => m.addedNodes.forEach(function (added) {
let id = added && added.getAttribute && added.getAttribute('id');
let el;
if (id && id.startsWith('message-') && (el = added.getElementsByClassName('reply-info')).length > 0) {
$(el[0]).off('click').click(() => handleReplyScroll(el[0], correction));
}
}))).observe(document.getElementById('chat'), {
childList: true,
subtree: true
});
// Also fix any reply-info's that may have existed before we started observing.
$('.reply-info').off('click').click(function () { return handleReplyScroll(this, correction); });
// Temporary workaround for https://meta.stackexchange.com/q/297021.
$('<style type="text/css">.message.highlight{margin-right:0 !important;}</style>').appendTo('head');
}
// New click handler for .reply-info items. Emulates SE behavior:
// - Scroll to message if its on page, and highlight it for 2 seconds.
// - Open the message in a transcript link (return true) if its not on page.
// The 'correction' parameter is the scroll position offset (topbar height).
function handleReplyScroll (reply, correction) {
let to = /#([0-9]+)/.exec(reply.getAttribute('href'));
if (!(to = to[1]))
return true;
let message = $(`#message-${to}`);
let target = message.closest('.user-container');
if (target.length === 0)
return true;
message.addClass('highlight');
window.setTimeout(function () {
message.removeClass('highlight');
}, 2000);
$('html, body').animate({
scrollTop: target.offset().top - correction
}, {
duration: 200,
queue: false
});
return false;
}
// Install hooks necessary to add chat server list to the SE dropdown, which is loaded
// as needed and not initially present. Be careful not to call this more than once.
function watchSEDropdown (topbar) {
// Readability is for the birds. Looking forward to forgetting what this does some day.
new MutationObserver((ms) => ms.forEach((m) => m.addedNodes.forEach(function (a) {
if (a.classList && a.classList.contains('siteSwitcher-dialog') && a.getElementsByTagName('link').length > 0) {
log('SE dropdown loaded.');
// Select insert point explicitly, ajax loader icon may still be there at this point.
// Also, still not sure which of the following two options I like better:
//let insert = $(a).find('.current-site-container').prev('.header');
let insert = $(a).find('#your-communities-header');
// Build and add the chat switcher section.
$('<div class="header ctb-chat-switcher"><h3>CHAT SERVERS</h3></div>')
.insertBefore(insert);
$('<div class="modal-content ctb-chat-switcher" id="ctb-chat-servers"><ul class="my-sites"/></div>')
.insertBefore(insert)
.find('.my-sites')
.append($('<li><a class="site-link" href="//chat.stackexchange.com"><div class="site-icon favicon favicon-stackexchange"></div> Stack Exchange Chat</a></li>'))
.append($('<li><a class="site-link" href="//chat.stackoverflow.com"><div class="site-icon favicon favicon-stackoverflow"></div> Stack Overflow Chat</a></li>'))
.append($('<li><a class="site-link" href="//chat.meta.stackexchange.com"><div class="site-icon favicon favicon-stackexchangemeta"></div> Meta Stack Exchange Chat</a></li>'));
$('#ctb-chat-servers a.site-link').each(function (_, link) {
if (link.hostname == document.location.hostname)
$(link).css('font-weight', 'bold');
$(`<span class="rep-score"><a class="ctb-chat-switch" target="_top" data-ctb-host="${link.hostname}">switch</a></span>`).insertBefore(link);
});
setRejoinOnSwitch(); // Will set switch link hrefs.
setShowSwitcher();
}
if (a.classList && a.classList.contains('topbar-dialog') && a.getElementsByTagName('link').length > 0) {
log('Generic topbar dropdown loaded.');
// Stop dropdown over-scrolling from scrolling chat window (adapted from https://stackoverflow.com/a/10514680):
let dropdown = $(a);
let isgeneric;
if ((isgeneric = !dropdown.hasClass('siteSwitcher-dialog'))) // only site-switcher has scrollbar at top level
dropdown = dropdown.find('.modal-content:first');
blockOverscrollEvents(dropdown, isgeneric);
}
}))).observe(topbar.find('.js-topbar-dialog-corral')[0], {
childList: true,
subtree: true
});
}
// Stop over-scrolling on an element from scrolling chat window (adapted from https://stackoverflow.com/a/10514680).
// outer = true to use outer height, false to use "regular" height.
function blockOverscrollEvents (elem, outer) {
elem.off("mousewheel")
.on("mousewheel", function (event) {
// Note: grab height every time in case the dialogs aren't fully laid out
// when this handler is installed, or the scroll height changes (e.g. the
// SE dropdown site filter changes the scroll height).
let height = outer ? elem.outerHeight() : elem.height(), sheight = elem[0].scrollHeight;
let block = ((this.scrollTop === sheight - height && event.deltaY < 0) ||
(this.scrollTop === 0 && event.deltaY > 0));
return !block;
});
}
// Create and initialize the room search dropdown.
function createRoomSearchDropdown (topbar) {
// Add a button for it.
let icon = $('<span class="topbar-icon icon-inbox"/>')
.css({
'background-position-x': -101,
'background-position-y': 0,
'width': 18,
'height': 18,
'top': 8,
'margin': '0 9px',
'filter': `brightness(${133.0/160.0})` // Make it match the other icons.
});
$('<a href="#" class="topbar-icon yes-hover" id="mc-roomfinder-button"/>')
.attr('title', 'Chat room list')
.append($('<span class="hidden-text">Chat room list</span>'))
.append(icon)
.appendTo(topbar.find('.network-items'))
.mouseenter((e) => (toggleRoomSearchDropdown('enter', e.target), false))
.click((e) => (toggleRoomSearchDropdown('click', e.target), false))
.css({
'background-position-x': -(220 - 72),
'background-position-y': -(54 - 36)
});
// We'll need to add mouseenter handlers for the other buttons to support topbar
// style behaviors. More comments in toggleRoomSearchDropdown().
topbar.find('.network-items > .topbar-icon:not(#mc-roomfinder-button)')
.mouseenter((e) => (toggleRoomSearchDropdown('away', e.target), false));
// Create the dropdown. We might as well put it in the corral with the others.
let search;
let roomlist;
let dropdown = $('<div class="topbar-dialog" id="mc-roomfinder-dialog"/>')
.append($('<div class="header" style="padding-top:0;padding-bottom:0;">')
.append($('<h3 style="padding-top:7px;padding-bottom:7px;">chat rooms</h3>'))
.append($('<select id="mc-roomfinder-tab"></select></div>')))
.append(search = $('<div class="modal-content"/>'))
.append(roomlist = $('<div class="modal-content" id="mc-roomfinder-results"/>'))
.appendTo(topbar.find('.js-topbar-dialog-corral'))
.data('mc-display', 'flex')
.css({
'display': 'none',
'width': 375,
'min-height': 420, // Same as site switcher (inbox/achievements are 390).
'max-height': 420,
'font-size': '12px',
'flex-direction': 'column'
});
$('<div class="site-filter-container"/>')
.css('display', 'flex')
.append($('<input type="text" class="site-filter-input" id="mc-roomfinder-filter" placeholder="Find a chat room"/>').css('flex-grow', '1'))
.append($('<button id="mc-roomfinder-go">SEARCH</button>').css({
'margin': '5px 0 5px 5px',
'padding': '3px',
'font-size': '11px'
}))
.appendTo(search);
$('<a href="#" class="mc-result-container" id="mc-result-more"\>')
.text('No results.')
.appendTo(roomlist);
$('#mc-roomfinder-tab') // Note: 'site' is not currently useful, requires a host and I have no UI for it.
.append($('<option/>').text('all'))
.append($('<option/>').text('mine'))
.append($('<option/>').text('favorite'))
.change(function () { doRoomSearch(); });
dropdown.find('.header, .model-content').css('flex-shrink', '0');
dropdown.find('.modal-content').css('padding', 0);
search.find('button').click(() => (doRoomSearch(), false));
roomlist.css({
'flex-grow': '1',
'max-height': 'none',
'overflow-x': 'hidden',
'overflow-y': 'scroll'
});
blockOverscrollEvents(roomlist);
// Auto load more results scroll handler.
roomlist[0].onscroll = function (event) {
if (roomlist[0].scrollTop + roomlist[0].offsetHeight >= roomlist[0].scrollHeight - 2) {
if (setAutoLoadMore() && $('#mc-result-more').data('mc-auto-click'))
$('#mc-result-more').data('mc-auto-click', false).click();
}
};
// If search parameters were preserved, restore them now.
let preserved = restoreSearchParams();
if (preserved) {
$('#mc-roomfinder-filter').val(preserved.filter);
$('#mc-roomfinder-tab').val(preserved.tab);
}
// I'm sick of typing .css() everywhere. Style the search results in a stylesheet.
// Note: We're cheating a little and styling select elements in the settings dialog
// here, too. Should probably reorganize the stylesheets if this gets complicated.
$('<style type="text/css"/>').text(`
.mc-result-container { padding: 10px; border-top: 1px solid #eff0f1; line-height: 1.3; display:block; }
.mc-result-container[href]:hover { background: #f7f8f8; }
.mc-result-container a:hover { text-decoration: underline; }
.mc-result-title { margin-bottom: 4px; pointer-events: none; }
.mc-result-title img { display: none; position: relative; top: -1px; }
.mc-result-title .mc-result-users { float: right; }
.mc-result-description { margin-bottom: 4px; color: #2f3337; }
.mc-result-info, .mc-result-users, .mc-result-activity { color: #848d95; }
.mc-result-compact-only { display: none; }
.mc-result-current .mc-result-title { font-weight: bold; }
.mc-result-current .mc-result-title > * { font-weight: normal; }
.mc-favicon-visible .mc-result-title img { display: block; float: right; }
.mc-favicon-visible[data-mc-favicon-style="left"] .mc-result-title img { float: left !important; margin-right: 1ex; }
.mc-favicon-visible[data-mc-favicon-style="margin"] .mc-result-title img { float: left !important; margin-right: 1ex; }
.mc-favicon-visible[data-mc-favicon-style="margin"] .mc-result-description { margin-left: calc(16px + 1ex); }
.mc-favicon-visible[data-mc-favicon-style="margin"] .mc-result-info { margin-left: calc(16px + 1ex); }
.mc-favicon-visible[data-mc-favicon-style="right"].mc-compact-finder .mc-result-users { margin-right: 1ex; }
.mc-favicon-visible[data-mc-favicon-style="right"].mc-compact-finder .mc-result-activity { margin-right: 1ex; }
.mc-compact-finder .mc-result-container { padding: 5px 10px; }
.mc-compact-finder .mc-result-title { margin-bottom: inherit; pointer-events: auto; }
.mc-compact-finder .mc-result-description { display: none; }
.mc-compact-finder .mc-result-info { display: none; }
.mc-compact-finder .mc-result-compact-only { display: initial; }
.mc-compact-finder[data-mc-result-sort="users"] .mc-result-activity { display: none }
.mc-compact-finder[data-mc-result-sort="activity"] .mc-result-users { display: none }
.mc-result-users { }
.mc-result-activity { float: right; }
#mc-result-more { color: #999; }
.mc-result-more-link { font-weight: bold; color: #0077cc !important; }
#mc-roomfinder-tab, #ctb-settings-dialog select { border: 1px solid #cbcbcb; box-shadow: inset 0 1px 2px #eff0f1,0 0 0 #FFF; color: #2f3337; }
.topbar-dialog.ui-widget select { font-family: inherit; font-size: inherit; }
/* The following styles prevent theme brightness from affecting hover/on. But they're kinda weird looking. */
/*.topbar .topbar-icon-on { filter: brightness(1.0) !important; }*/
/*.topbar .yes-hover:hover { filter: brightness(1.0) !important; }*/
/*.topbar .topbar-menu-links a:hover { filter: brightness(1.0) !important; }*/
`).appendTo('head');
// Site input does this but I don't really like it on the dropdown:
// #mc-roomfinder-tab:hover { border-color: rgba(0,149,255,0.5); box-shadow: inset 0 1px 2px #e4e6e8,0 0 2px rgba(0,119,204,0.1); }
// Sets search button visibility.
setAutoSearch();
let filter = $('#mc-roomfinder-filter');
let timerId;
filter.keyup(function (e) {
let auto = filter.data('mc-auto');
// Enter key searches.
if (e.keyCode == 13 && !auto)
$('#mc-roomfinder-go').click();
// Do auto search 1 second after last key pressed.
if (timerId) {
window.clearTimeout(timerId);
timerId = null;
}
if (auto) {
timerId = window.setTimeout(function () {
let cur = filter.val().trim();
let old = (filter.data('mc-last-auto') || '');
if (cur !== old) {
//console.log(`filter change ${old} => ${cur}`);
filter.data('mc-last-auto', cur);
if (filter.data('mc-auto')) {
//console.log('auto search');
doRoomSearch();
}
}
}, AUTO_SEARCH_DELAY_MS);
}
});
}
// Show/hide the room search dropdown.
function toggleRoomSearchDropdown (why, source) {
let dropdown = $('#mc-roomfinder-dialog');
let button = $('#mc-roomfinder-button');
// Figure out what state we're in and what state we should be in. Since the
// topbar doesn't publicly expose its dialog management functions, we have
// to implement matching behavior ourselves (hide other dialogs, show on hover,
// etc.).
let isVisible = (dropdown.css('display') !== 'none');
let othersVisible = ($('.network-items > .topbar-icon-on:not(#mc-roomfinder-button)').length > 0);
let wantVisible;
let wantOthersVisible = false;
// All the logic for topbar-compatible click/hover behavior is here:
if ((why || 'click') === 'click') { // Clicked on the icon.
wantVisible = !isVisible;
} else if (why === 'enter') { // Mouse entered the icon.
wantVisible = isVisible || othersVisible;
} else if (why === 'away') { // Mouse left the icon.
wantVisible = false;
wantOthersVisible = isVisible || othersVisible;
} else if (why === 'clickout') { // Clicked outside the dialog.
wantVisible = false;
wantOthersVisible = othersVisible;
} else {
return;
}
// Hide/show native topbar dropdowns as needed.
if (source && (othersVisible !== wantOthersVisible)) {
if (wantOthersVisible)
source.click();
else
frame[0].contentWindow.StackExchange.topbar.hideAll();
}
// Hide/show room search dropdown as needed.
if (isVisible !== wantVisible) {
if (wantVisible) {
dropdown.css({
'display': dropdown.data('mc-display'),
'left': button.position().left,
'top': button.position().top + $('.topbar').height()
});
button.addClass('topbar-icon-on');
} else {
dropdown.css('display', 'none');
button.removeClass('topbar-icon-on');
}
}
// First time it is displayed, load it up with some rooms.
if (wantVisible && !dropdown.data('mc-shown-once')) {
dropdown.data('mc-shown-once', true);
doRoomSearch();
}
}
// Perform room search.
function doRoomSearch (more) {
let res = $('#mc-roomfinder-results');
let status = $('#mc-result-more');
let sinput = $('#mc-roomfinder-filter');
let sbutton = $('#mc-roomfinder-go');
let stab = $('#mc-roomfinder-tab');
let params;
sinput.prop('disabled', !sinput.data('mc-auto'));
sbutton.prop('disabled', true);
stab.prop('disabled', true);
status.removeClass('mc-result-more-link').data('mc-auto-click', false)
// New search vs. loading more results.
if (more && res.data('mc-params')) {
// Update status.
status.toggle(true).ctb_noclick().text('Loading More...');
// Next page, from data.
params = res.data('mc-params');
params.page = (params.page || 1) + 1;
res.data('mc-params', params);
} else {
// Clear existing results and update status.
status.toggle(true).ctb_noclick().text('Loading...');
res.find('.mc-result-card').remove();
// First page, use filter from text box and store it.
params = {
tab: stab.val(),
sort: setSearchByActivity() ? 'active' : 'people',
filter: sinput.val().trim(),
pageSize: 20,
nohide: false
};
res.data('mc-params', params);
sinput.data('mc-last-auto', sinput.val());
}
// Run search.
log(`Running search: ${JSON.stringify(params)}`);
let nolinks = !setLinkifyDescriptions();
$.post('/rooms', params).then(function (html) {
let doc = $('<div/>').html(html);
doc.find('.roomcard').each(function (_, roomcard) {
roomcard = $(roomcard);
let result = {
name: roomcard.find('.room-name').text().trim(),
description: roomcard.find('.room-description'),
activity: roomcard.find('.last-activity'),
users: Number(roomcard.find('.room-users').attr('title').replace(/[^0-9]/g, '')),
id: Number(roomcard.attr('id').replace(/[^0-9]/g, '')),
icon: roomcard.find('.small-site-logo')
};
let compactActivity = /^([\w\s]*)/.exec(result.activity.text());
compactActivity = (compactActivity ? compactActivity[1].trim() : '');
$(`<a class="mc-result-container mc-result-card mc-result-link${result.id === CHAT.CURRENT_ROOM_ID ? ' mc-result-current' : ''}"\>`)
.attr('href', `//${window.location.hostname}/rooms/${result.id}`)
.click(() => (preserveSearchParams(params), true))
.append($('<div class="mc-result-title"/>')
.attr('title', result.description.text().trim())
.text(result.name)
.append(result.icon.removeClass("small-site-logo"))
.append(`<span class="mc-result-users mc-result-compact-only">${withs(result.users, 'user')}</span>`)
.append(`<span class="mc-result-activity mc-result-compact-only">${compactActivity}</span>`))
.append($('<div class="mc-result-description"/>').html(result.description.html().trim()).ctb_linkify(nolinks))
.append($(`<div class="mc-result-info"><span class="mc-result-users">${withs(result.users, 'user')}</span><span class="mc-result-activity">${result.activity.html().trim()}</span></div>`))
.appendTo(res);
});
if (doc.find('.pager a[rel="next"').length > 0) {
status
.addClass('mc-result-more-link')
.toggle(true)
.text('Load More...')
.off('click')
.click(() => (doRoomSearch(true), false))
.attr('href', '#')
.data('mc-auto-click', true)
.appendTo(res);
} else if (res.find('.mc-result-card').length === 0) {
status
.removeClass('mc-result-more-link')
.toggle(true)
.text(params.filter === '' ? 'No results.' : `No results for "${params.filter}".`)
.ctb_noclick();
} else {
status.toggle(false);
}
setOpenRoomsHere(); // Update target attribute in result links.
}).fail(function (e) {
status
.removeClass('mc-result-more-link')
.toggle(true)
.text('An error occurred.')
.ctb_noclick();
}).always(function () {
sinput.prop('disabled', false);
sbutton.prop('disabled', false);
stab.prop('disabled', false);
if (!sinput.data('mc-auto'))
sinput.focus();
});
}
// Concatenate a number to a string then pluralize a string.
function withs (n, str, suffix) {
return (n === 1) ? `${n} ${str}` : `${n} ${str}${suffix || 's'}`;
}
// Show settings popup.
function showSettings () {
// If version updated and change log not viewed yet, show that instead.
if ($('#ctb-settings-link').data('updated')) {
showChangeLog();
return;
}
// Initialize dialog first time through.
if ($('#ctb-settings-dialog').length === 0) {
let title = (typeof tbData.scriptVersion === 'undefined' ? '' : ` (${tbData.scriptVersion})`);
$('body').append(
`<div id="ctb-settings-dialog" title="Settings${title}">
<label><input type="checkbox" name="themed" onchange="ChatTopBar.setThemed(this.checked)"><span>Use chat room themes</span></label>
<label><input type="checkbox" name="widen" onchange="ChatTopBar.setWiden(this.checked)"><span>Wide layout</span></label>
<label><input type="checkbox" name="switch" onchange="ChatTopBar.setShowSwitcher(this.checked)"><span>Show chat servers in SE dropdown</span></label>
<label><input type="checkbox" name="rejoin" onchange="ChatTopBar.setRejoinOnSwitch(this.checked)"><span>Rejoin favorites on switch</span></label>
<label><input type="checkbox" name="autosearch" onchange="ChatTopBar.setAutoSearch(this.checked)"><span>Search for rooms as you type</span></label>
<label><input type="checkbox" name="byactivity" onchange="ChatTopBar.setSearchByActivity(this.checked)"><span>Sort rooms by activity instead of people</span></label>
<label><input type="checkbox" name="linkify" onchange="ChatTopBar.setLinkifyDescriptions(this.checked)"><span>Linkify URLs in search results</span></label>
<label><input type="checkbox" name="open" onchange="ChatTopBar.setOpenRoomsHere(this.checked)"><span>Open search result rooms in this tab</span></label>
<span style="display:flex;align-items:center;">
<label><input type="checkbox" name="favvis" onchange="ChatTopBar.setFaviconVisible(this.checked)"><span>Display site icons in results:</span></label>
<select name="favstyle" onchange="ChatTopBar.setFaviconStyle(this.value)"><option>margin<option>left<option>right</select></label></span>
<label><input type="checkbox" name="compact" onchange="ChatTopBar.setCompactResults(this.checked)"><span>Display compact room search results.</span></label>
<label><input type="checkbox" name="quiet" onchange="ChatTopBar.setQuiet(this.checked)"><span>Suppress console output</span></label>
<hr><label class="ctb-fixheight"><span>Brightness (this theme only):</span></label>
<div class="ctb-fixheight"><div style="flex-grow:1" id="ctb-settings-brightness"></div></div><hr>
<div class="ctb-fixheight" style="white-space:nowrap"><a href="${URL_UPDATES}">Updates</a> | <a href="${URL_MORE}">More Scripts</a> | <a href="#" id="ctb-show-log">Change Log</a></div>
</div>`);
$('#ctb-show-log').click(() => (showChangeLog(), showSettings(), false));
let elem = $('#ctb-settings-dialog');
elem.find('hr').css({'border':'0', 'border-bottom':$('#present-users').css('border-bottom')});
elem.find('label, .ctb-fixheight').css({'display':'flex', 'align-items':'center'});
let rowHeight = $('input[name="themed"]').closest('label').css('height');
elem.find('.ctb-fixheight').css({'height':rowHeight, 'justify-content':'center'});
elem.find('a').css('color', $('#sidebar-menu a').css('color')); // Because #input-area a color is too light.
let work = elem.find('#ctb-settings-brightness');
work.slider({
min: 0,
max: 200,
value: 100,
slide: (_,ui) => setBrightness(ui.value / 100.0),
classes: {
'ui-slider': '',
'ui-slider-handle': '',
'ui-slider-range': ''
}
});
let sliderMargin = work.find('.ui-slider-handle').css('width');
work.css('margin', `0 calc(${sliderMargin} / 2)`);
elem.dialog({
appendTo: '#input-area', // Body can scroll; this will keep us fixed.
show: 100,
hide: 100,
autoOpen: false,
width: 'auto',
height: 'auto',
resizable: false,
draggable: false,
position: {
my: 'center bottom',
at: 'center top',
of: '#ctb-settings-link'
},
classes: {
'ui-dialog': 'topbar-dialog',
'ui-dialog-content': '',
'ui-dialog-buttonpane': '',
'ui-dialog-titlebar': '',
'ui-dialog-titlebar-close': '',
'ui-dialog-title': ''
}
});
}
// Toggle visibility.
let dialog = $('#ctb-settings-dialog');
if (dialog.dialog('isOpen')) {
dialog.dialog('close');
} else {
dialog.find('[name="widen"]').prop('checked', setWiden());
dialog.find('[name="themed"]').prop('checked', setThemed());
dialog.find('[name="quiet"]').prop('checked', setQuiet());
dialog.find('[name="rejoin"]').prop('checked', setRejoinOnSwitch());
dialog.find('[name="switch"]').prop('checked', setShowSwitcher());
dialog.find('[name="autosearch"]').prop('checked', setAutoSearch());
dialog.find('[name="byactivity"]').prop('checked', setSearchByActivity());
dialog.find('[name="linkify"]').prop('checked', setLinkifyDescriptions());
dialog.find('[name="favvis"]').prop('checked', setFaviconVisible());
dialog.find('[name="favstyle"]').val(setFaviconStyle());
dialog.find('[name="compact"]').prop('checked', setCompactResults());
dialog.find('[name="open"]').prop('checked', setOpenRoomsHere());
dialog.find('#ctb-settings-brightness').slider('value', 100.0 * setBrightness());
dialog.dialog('open');
}
}
// Hide settings dialog if target element is outside the dialog. Used when
// processing global mouse click events.
function hideSettingsIfOutside (target) {
target = $(target);
let dialog = $('#ctb-settings-dialog');
// https://stackoverflow.com/a/11003694
if (dialog.length > 0 && dialog.dialog('isOpen') &&
!target.is('.ui-dialog') && target.closest('.ui-dialog').length === 0)
dialog.dialog('close');
}
// Check if the script has been updated and, if so, do things to make the change
// log visible.
function checkUpdateNotify () {
if (typeof tbData.scriptVersion === 'undefined')
return;
let oldVersion = load('changesViewedFor', null);
let newVersion = /^([0-9]+\.[0-9]+)/.exec(tbData.scriptVersion)[1]; // no flashing for minor versions.
if (oldVersion === newVersion)
return;
else
log(`Detected update, ${oldVersion} => ${newVersion}`);
// Highlight the link; it's data property will be read by showSettings(), which
// will show the change log instead.
$('#ctb-settings-link').css({
background: '#0f0',
color: 'black'
}).data('updated', newVersion);
// Blinky blinky.
let i = window.setInterval(function () {
let link = $('#ctb-settings-link');
if (link.data('updated')) {
if (link.data('flasher'))
link.css('background', '#0f0');
else
link.css('background', '#ff0');
link.data('flasher', link.data('flasher') ? false : true);
} else {
window.clearInterval(i);
}
}, 500);
}
// Shows change log dialog.
function showChangeLog () {
// Clear update highlights and remember that the user viewed this.
if ($('#ctb-settings-link').data('updated')) {
store('changesViewedFor', $('#ctb-settings-link').data('updated'));
$('#ctb-settings-link').css({
background: '',
color: ''
}).data('updated', null);
}