forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatPagination.user.js
121 lines (102 loc) · 3.26 KB
/
ChatPagination.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
// ==UserScript==
// @name Chat Pagination
// @description Improvements to pagination of user recent messages page. Do not install if you have ChatImprovements as it also implements this.
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 3.0.13
//
// @match https://chat.stackoverflow.com/*
// @match https://chat.stackexchange.com/*
// @match https://chat.meta.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 updatePager(curr) {
curr = Number(curr);
if (typeof curr !== 'number') return;
const qs = location.search.replace(/&page=\d+/, '');
const pager = $('.pager').empty();
const start = Math.max(1, curr - 5);
const stop = Math.max(10, curr + 5);
const prev = Math.max(1, curr - 1);
const next = curr + 1;
let htmlstr = `<a href="${location.origin}${location.pathname}${qs}&page=${prev}" data-page="${prev}"><span class="page-numbers prev">prev</span></a>`;
for (let i = start; i <= stop; i++) {
htmlstr += `<a href="${location.origin}${location.pathname}${qs}&page=${i}" data-page="${i}"><span class="page-numbers ${i == curr ? 'current' : ''}">${i}</span></a>`;
}
htmlstr += `<a href="${location.origin}${location.pathname}${qs}&page=${next}" data-page="${next}"><span class="page-numbers next">next</span></a>`;
pager.append(htmlstr);
}
function getPage(url, selector, callback = null) {
window.history.replaceState({}, '', url);
$.ajax({
url: url,
xhr: jQueryXhrOverride,
success: function (data) {
let tmp = $(selector, data);
$(selector).html(tmp.html());
if (typeof callback === 'function') callback.call();
}
});
}
// Append styles
addStylesheet(`
#roomlist {
clear: both;
}
#roomlist .pager,
#roomlist .fr {
display: inherit;
}
.fr {
margin: 15px 0;
}
.pager {
padding: 20px 0 0px;
text-align: center;
}
.pager > *,
.pager .page-numbers {
display: inline-block !important;
float: none;
}
.pager .page-numbers {
padding: 4px 8px;
margin: 0 1px;
border-radius: 2px;
text-align: center;
min-width: 14px;
}
#content .subheader ~ div:last-of-type > div[style*="float:left; width:230px"] {
position: sticky;
top: 10px;
}
.room-mini {
min-height: 110px;
}
`); // end stylesheet
// On script run
(function init() {
const content = $('#content');
const userpage = location.pathname.includes('/users/') && getQueryParam('tab') == 'recent';
const roomspage = location.pathname.includes('/rooms');
if (!userpage) return;
$('.pager').first().remove();
const pager = $(`<div class="pager clear-both"></div>`).insertAfter('#content');
pager.clone(true).insertAfter('#content .subheader');
let curr = getQueryParam('page') || 1;
updatePager(curr);
$('.pager').on('click', 'a', function () {
window.scrollTo(0, 0);
const num = Number(this.dataset.page);
getPage(this.href, '#content', function () {
pager.clone(true).insertAfter('#content .subheader');
updatePager(num);
});
return false;
});
})();