forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoOneboxesInChatTranscripts.user.js
106 lines (91 loc) · 3.38 KB
/
NoOneboxesInChatTranscripts.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// ==UserScript==
// @name No Oneboxes in Chat Transcripts
// @description Collapses oneboxes from chat transcripts, click to display onebox
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author @samliew
// @version 1.3.1
//
// @include https://chat.stackoverflow.com/transcript/*
// @include https://chat.stackexchange.com/transcript/*
// @include https://chat.meta.stackexchange.com/transcript/*
//
// @include https://chat.stackoverflow.com/rooms/*/conversation/*
// @include https://chat.stackexchange.com/rooms/*/conversation/*
// @include https://chat.meta.stackexchange.com/rooms/*/conversation/*
// ==/UserScript==
(function() {
'use strict';
/* Call hideOneboxes() from other scripts to hide all new oneboxes on any page update
* Params:
* mid : message ID to re-hide its expanded onebox, OR
* -1 to re-hide all expanded oneboxes
*/
unsafeWindow.hideOneboxes = function(mid = null) {
// Display original link and hide oneboxes who hasn't been hidden before
$('.onebox').not('.js-onebox-hidden').addClass('js-onebox-hidden').hide().each(function() {
// Onebox permalink is usually the first URL in the onebox
let url = $(this).find('a').first().attr('href');
// If onebox type is a tweet, permalink is the last link in onebox
if($(this).hasClass('ob-tweet')) url = $(this).find('a').last().attr('href');
const loadOneboxText = 'click to load onebox';
const hideOneboxText = 'click to hide onebox';
let isVisible = false;
// Click placeholder to show onebox
$(`<span class="has-onebox" title="${loadOneboxText}"><a href="${url}" class="print-onebox">${url}</a><span>${url}</span></span>`)
.click(function() {
isVisible = !isVisible;
if (isVisible) {
$(this).addClass('js-show-onebox');
$(this).attr('title', hideOneboxText);
} else {
$(this).removeClass('js-show-onebox');
$(this).attr('title', loadOneboxText);
}
}).insertBefore(this);
// Also collapse user signature (use tiny-signature)
$(this).parents('.monologue').find('.tiny-signature').fadeIn(200).siblings().hide();
});
// Re-hide oneboxes if mid is set
if(mid === -1) {
// Re-hide all expanded oneboxes
$('.js-show-onebox').removeClass('js-show-onebox');
}
else if(mid) {
// Re-hide specific message's onebox
$(`#message-${mid}`).find('.js-show-onebox').removeClass('js-show-onebox');
}
};
function appendStyles() {
const styles = `
<style>
.has-onebox {
padding-left: 10px;
border-left: 3px solid orange;
cursor: zoom-in;
}
.js-show-onebox {
cursor: zoom-out;
}
.js-show-onebox + .js-onebox-hidden {
display: block !important;
}
.has-onebox .print-onebox {
display: none;
word-break: break-all;
}
@media print {
.print-onebox {
display: inline !important;
}
.print-onebox + span {
display: none !important;
}
}
</style>
`;
$('body').append(styles);
}
// On page load
appendStyles();
hideOneboxes();
})();