-
Notifications
You must be signed in to change notification settings - Fork 34
/
StackPrintStyles.user.js
624 lines (564 loc) · 15.7 KB
/
StackPrintStyles.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// ==UserScript==
// @name Stack Print Styles
// @description Print preprocessor and print styles for Stack Exchange Q&A, blog, and chat. Includes a handy load all comments button at bottom right.
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 2.0.13
//
// @match https://*.stackoverflow.com/*
// @match https://*.serverfault.com/*
// @match https://*.superuser.com/*
// @match https://*.askubuntu.com/*
// @match https://*.mathoverflow.net/*
// @match https://*.stackapps.com/*
// @match https://*.stackexchange.com/*
// @match https://stackoverflowteams.com/*
// @match https://stackoverflow.blog/*
//
// @match https://chat.stackoverflow.com/*
// @match https://chat.stackexchange.com/*
// @match https://chat.meta.stackexchange.com/*
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
// ==/UserScript==
/* globals StackExchange */
/// <reference types="./globals" />
'use strict';
function processTimestampTooltips() {
$('.relativetime, .relativetime-clean').not('[data-timestamp]').each(function () {
const title = $(this).attr('title');
$(this).attr('data-timestamp', title.replace(/,.+$/, ''));
});
$('time[datetime]').not('[data-timestamp]').each(function () {
const title = $(this).attr('datetime');
$(this).attr('data-timestamp', title.replace('T', ' ') + 'Z');
});
$('#question-header').next().find('a[title]').each(function () {
const title = $(this).attr('title');
$(this).attr('data-timestamp', title);
});
}
function loadAllAnswersAndComments(inclDeletedComments = false) {
// Load answers from other pages (if more than one)
function loadAllAnswers() {
// Wrap all existing answers in a div
let previousPage = $(`<div class="js-answer-page" data-page="1"></div>`).insertAfter('#answers-header');
$('#answers').children('.answer, a[name]').not('[name="new-answer"], [name="tab-top"]').appendTo(previousPage);
return new Promise(function (resolve, reject) {
// Get other pages, if no other pages resolve immediately
const pages = $('.pager-answers').first().children('a').not('[rel="prev"]').not('[rel="next"]');
if (pages.length == 0) {
console.log('only one page of answers');
resolve();
return;
}
// Remove pagination
$('.pager-answers').remove();
// Load each pager's url into divs below existing answers
let deferreds = [];
pages.each(function (i, el) {
const pageNum = el.innerText.trim();
const page = $(`<div class="js-answer-page" data-page="${pageNum}"></div>`).insertAfter(previousPage);
const aj = page.load(el.href + ' #answers', function () {
page.children().children().not('.answer, a').remove();
});
previousPage = page;
deferreds.push(aj);
});
// Resolve when all pages load
$.when.all(deferreds).then(function () {
console.log('loaded all answer pages');
// short delay to allow comments to be added to page
setTimeout(() => { resolve(); }, 2000);
}, reject);
});
}
// Always expand comments if comments have not been expanded yet
function loadAllComments(inclDeletedComments) {
return new Promise(function (resolve, reject) {
let deferreds = [];
$('.question, #answers .answer').find('.js-comments-container').not('.js-del-loaded').addClass('js-del-loaded').each(function () {
// Get post id which is required for loading comments
const postId = this.dataset.answerid || this.dataset.questionid || this.dataset.postId;
// Remove default comment expander after loading comments
const elems = $(this).next().find('.js-show-link.comments-link:visible').prev('.js-link-separator').addBack();
// If no comment expander and not loading deleted comments,
// do nothing else since everything is already displayed
if (!inclDeletedComments && elems.length == 0) {
return;
}
// Get all including deleted comments
// This method will avoid jumping to comments section
const commentsUrl = `/posts/${postId}/comments?includeDeleted=${inclDeletedComments}&_=${Date.now()}`;
const aj = $('#comments-' + postId).show().children('ul.js-comments-list').load(commentsUrl, function () {
console.log('loaded comments ' + postId);
elems.remove();
});
deferreds.push(aj);
});
// Resolve when all comments load
$.when.all(deferreds).then(function () {
console.log('loaded all comments');
// short delay to allow comments to be added to page
setTimeout(() => { resolve(); }, 2000);
}, reject);
});
}
// Short delay for Q&A to init
setTimeout(() => {
// Do one then the other after
loadAllAnswers().then(() => loadAllComments(inclDeletedComments));
}, 1000);
}
// Append styles
addStylesheet(`
/* Styles for loading comments buttons */
.print-comment-buttons {
position: fixed !important;
bottom: 3px;
right: 3px;
z-index: 999999;
}
.print-comment-buttons:hover button {
display: inline-block !important;
}
.print-comment-buttons button {
display: none;
margin-right: 3px;
opacity: 0.5;
}
.print-comment-buttons button:hover {
opacity: 1;
}
.print-comment-buttons button:nth-last-child(1) {
display: inline-block;
margin-right: 0;
}
`); // end stylesheet
// On script run
(function init() {
if (location.hostname.includes('chat.')) {
appendChatPrintStyles();
}
else if (document.getElementById('question') || location.pathname.includes('/questions/') || document.body.classList.contains('unified-theme')) {
appendQnaPrintStyles();
processTimestampTooltips();
$(document).on('ajaxStop', processTimestampTooltips);
const commentButtons = $(`<div class="print-comment-buttons"><button>Load answers and comments</button></div>`).appendTo('body');
if (StackExchange.options.user.isModerator) {
commentButtons.prepend(`<button data-incldeletedcomments="true">+ deleted comments</button>`);
}
commentButtons.on('click', 'button', function (evt) {
// Remove buttons
commentButtons.remove();
const inclDeletedComments = !!evt.target.dataset.incldeletedcomments;
loadAllAnswersAndComments(inclDeletedComments);
return false;
});
}
})();
function appendQnaPrintStyles() {
const qnaCss = `
@media print {
html, body {
max-width: none;
}
body {
font-size: 1rem;
background-color: #fff;
background-image: none;
}
.comments,
.comments .comment-body > * {
font-size: 1.1rem;
line-height: 1.35;
}
header,
footer,
#topbar,
#sidebar,
#left-sidebar,
#footer,
#post-form,
body > span,
.site-header,
.deleted-comment-info,
.comment-flagging,
.comment-voting,
.js-post-issue,
.comments + div[id^="comments-link"],
.pager-answers,
.bottom-notice,
a.new-answer,
.js-comment-edit,
.js-comment-delete,
.z-banner,
#edit-tags,
.js-bookmark-btn,
.js-new-contributor-indicator,
.new-contributor-indicator
{
display: none;
}
#content
.question-page #answers .answer {
border: none;
}
/* Lighten bg of deleted answers */
.deleted-answer {
background-color: #fff8f8;
}
/* Do not fade downvoted answers */
.downvoted-answer .comment-body,
.downvoted-answer .post-signature,
.downvoted-answer .s-prose,
.downvoted-answer .vote>* {
opacity: 1;
}
/* Don't show if you have voted */
.js-voting-container .s-btn {
color: inherit;
}
/* No relative dates */
.relativetime,
.relativetime-clean,
#question-header + .grid > .flex--item time,
#question-header + .grid > .flex--item a {
font-size: 0;
}
.relativetime:before,
.relativetime-clean:before,
#question-header + .grid > .flex--item time:before,
#question-header + .grid > .flex--item a:before {
content: attr(data-timestamp);
font-size: 13px;
white-space: nowrap;
}
.comment-date .relativetime:before,
.comment-date .relativetime-clean:before,
.user-info .user-action-time .relativetime:before,
.user-info .user-action-time .relativetime-clean:before {
font-size: 12px;
}
/* Answers starts on a new page */
#answers-header,
#answers .pager-answers + a[name],
#answers-header + .js-answer-page[data-page="1"] > a:first-child {
page-break-before: avoid;
}
#answers > a,
.js-answer-page > a {
display: block;
height: 1px;
page-break-before: always;
}
#answers > a:nth-of-type(2),
#answers > a:last-of-type,
.js-answer-page[data-page="1"] > a:first-child,
#mainbar > table .msg {
page-break-before: always;
}
/* Deleted comments on deleted posts */
.deleted-answer .comments .comment.deleted-comment .comment-text,
.deleted-answer .comments .comment.deleted-comment .comment-actions {
background-color: var(--red-100);
}
/* Embiggen images */
.s-prose p {
page-break-inside: avoid;
}
.s-prose img {
width: auto;
max-width: 100%;
max-height: 90vh;
page-break-inside: avoid;
}
/* Do not break comments and usercards between pages */
.comment-text,
.post-signature,
.user-gravatar32,
.s-prose ~ div {
page-break-inside: avoid;
}
/* Hide/Reset SOMU stuff */
#somusidebar,
#usersidebar,
.redact-buttons,
.print-comment-buttons,
.post-id,
.post-mod-menu-link,
.mod-userlinks,
.somu-mod-message-link,
.comment-flagcount,
.meta-mentioned {
display: none;
}
.votecell .vote,
.votecell .js-voting-container,
.post-stickyheader {
position: relative;
top: 0;
}
.post-stickyheader .relativetime {
border-bottom: none;
}
.deleted-answer .votecell .vote, .deleted-answer .votecell .js-voting-container {
background-color: transparent;
}
/* Hide/Reset other userscripts stuff */
#roombaFieldRow,
.mod-tools,
.pronouns
{
display: none;
}
}
`.replace(/ !important/g, '').replace(/;/g, ' !important;'); // failsafe to prevent dupe !important
addStylesheet(qnaCss);
} // End QnA styles
function appendChatPrintStyles() {
const printCss = `
@media print {
html, body {
max-width: 780px;
}
body {
font-size: 11px;
background-color: #fff;
background-image: none;
}
body > span[style*="absolute"],
#topbar,
.topbar,
#feed-ticker,
#bottom,
#input-area,
#sound,
input,
button,
.button,
#container > a,
#container > br,
#widgets > .sidebar-widget:nth-child(2),
#widgets > .sidebar-widget:last-child,
#sidebar .more,
#sidebar .user-currentuser,
#sidebar .js-hasfull .message-orig,
#sidebar #room-ad,
#toggle-favorite,
#transcript-body #info br,
#transcript-body .room-mini ~ br,
#transcript-body .room-mini .mspbar.now,
#transcript-body #info .tag,
#transcript-body #transcript-logo,
#transcript-body #copyright,
#transcript-body .action-link,
#transcript-body .transcript-nav,
.monologue .avatar,
.message-controls,
.message > .action-link,
.message > .meta,
.username .name + br,
.username .pronouns
{
display: none;
}
#sidebar #info #roomdesc > div,
#starred-posts > div > ul > li,
.ob-message.js-onebox-hidden,
#chat .monologue:first-child .js-dynamic-timestamp
{
display: block;
}
#sidebar .js-hasfull .message-full
{
display: inline;
}
#main {
display: flex;
flex-direction: column-reverse;
width: 100%;
}
#sidebar {
position: relative;
width: auto;
margin: 10px 0 20px;
padding: 10px;
border: 1px dotted black;
}
#transcript-body #container {
padding: 0;
}
#transcript-body #sidebar {
margin-top: 0;
margin-bottom: -10px;
}
#sidebar #info #roomdesc {
position: relative;
height: auto;
padding-bottom: 0;
border: none;
background: transparent;
white-space: unset;
}
#sidebar #info #roomdesc + #sidebar-menu {
margin-top: 10px;
}
#sidebar #present-users-list {
max-height: none;
overflow: visible;
color: #000;
}
#sidebar #present-users-list li {
flex: 0 0 20%;
}
#sidebar #present-users-list li.inactive {
opacity: 0.7;
}
#sidebar #starred-posts ul.collapsible,
#sidebar #starred-posts ul.collapsible.expanded {
max-height: none;
padding-bottom: 0;
overflow: visible;
}
#chat-body #container {
padding-top: 0;
}
#chat {
padding-bottom: 20px;
}
.monologue {
display: table;
page-break-inside: avoid;
width: calc(100% - 26px);
margin: 0;
padding: 0;
}
.monologue,
.system-message-container {
padding-top: 15px;
margin-bottom: -15px;
}
.monologue .signature {
flex: 0 1 120px;
margin-right: 8px;
}
.monologue .messages {
flex: 1 0 80%;
border-color: #f2f2f2;
background-color: #f8f8f8;
}
div.message.reply-parent,
div.message.reply-child {
border-color: #f2f2f2;
background-color: #f8f8f8;
}
.monologue.catchup-marker {
padding-top: 0;
border-top: none;
}
#chat .message {
display: flex;
}
.message {
page-break-inside: avoid;
border: none;
}
.message .content {
flex: 1 1 100%;
padding-right: 52px;
}
.message .mention {
background-color: transparent;
}
div.message {
padding-left: 15px;
}
div.message .full,
div.message .partial {
max-height: none;
}
#chat .messages .timestamp,
#chat .message.cmmt-deleted span.deleted {
position: absolute;
right: 38px;
}
.stars .img {
filter: saturate(0) grayscale(1) brightness(0);
}
#transcript-body .pager {
text-align: center;
}
#transcript-body .pager > * {
float: none;
display: inline-block;
}
#transcript-body .pager .page-numbers {
margin-bottom: 3px;
}
/* SOMU - Chat Transcript Helper - switch back to original timestamp (UTC) */
.page-numbers[data-orig-text],
.timestamp[data-orig-timestamp] {
font-size: 0;
}
.page-numbers[data-orig-text]:before,
.timestamp[data-orig-timestamp]:before {
content: attr(data-orig-timestamp);
font-size: 9px;
white-space: nowrap;
}
.page-numbers[data-orig-text]:before {
content: attr(data-orig-text);
font-size: 14px;
}
/* Chat Transcript - room mini - expand full description */
#transcript-body #info .room-mini {
width: auto;
margin-bottom: 15px;
}
#transcript-body #info .room-mini .room-mini-description {
font-size: 0;
}
#transcript-body #info .room-mini .room-current-user-count,
#transcript-body #info .room-mini .room-message-count {
display: none;
width: auto;
font-size: 11px;
}
#transcript-body #info .room-mini .room-current-user-count:before,
#transcript-body #info .room-mini .room-message-count:before,
#transcript-body #info .room-mini .room-mini-description:before {
display: inline-block;
content: attr(title);
margin-right: 3px;
font-size: 11px;
word-break: break-word;
}
/* Chat Transcript - convert calendar to text with year */
#transcript-body #info > h2 {
display: inline-block;
}
#transcript-body #info .icon .calendar,
#transcript-body #info .calendar-small-link {
display: none;
}
#transcript-body #info .icon {
display: inline-block;
float: none;
font-size: 0;
}
#transcript-body #info .icon:before {
content: attr(title);
font-size: 16.5px;
font-weight: bold;
}
}
`.replace(/ !important/g, '').replace(/;/g, ' !important;'); // failsafe to prevent dupe !important
addStylesheet(printCss);
} // End chat print styles