-
Notifications
You must be signed in to change notification settings - Fork 34
/
RefreshEmptyModQueue.user.js
134 lines (112 loc) · 4.39 KB
/
RefreshEmptyModQueue.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
// ==UserScript==
// @name Refresh Empty Mod Queue
// @description If current mod queue is empty, reload page occasionally
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 4.1.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/*
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *chat.*
// @exclude *blog.*
// @exclude */tour
//
// @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, _window */
/// <reference types="./globals" />
'use strict';
const goToMain = () => location.href = '/admin/dashboard?filtered=false';
const reloadPage = () => location.search.contains('filtered=false') ? location.reload() : location.search += (location.search.length == 0 ? '' : '&') + 'filtered=false';
let timeoutSecs = _window.modRefreshInterval || 10;
let timeout, interval;
/**
* @summary Trigger page refresh
* @param {boolean} main - Go to main dashboard instead of reloading page
*/
const initRefresh = function (main = false) {
if ($('.js-flagged-post:visible, .flagged-post-row:visible').length > 0) return;
if (timeoutSecs < 1) timeoutSecs = 5;
// Function called again, reset
if (timeout || interval) {
clearTimeout(timeout);
clearInterval(interval);
timeout = null;
interval = null;
$('#somu-refresh-queue-counter').remove();
}
let c = timeoutSecs;
$(`<div id="somu-refresh-queue-counter">Refreshing page in <b id="refresh-counter">${timeoutSecs}</b> seconds...</div>`).appendTo('body');
// Main timeout
timeout = setTimeout(main ? goToMain : reloadPage, timeoutSecs * 1000);
// Counter update interval
interval = setInterval(function () {
$('#refresh-counter').text(--c > 0 ? c : 0);
}, 1000);
};
// Function promoted to global scope for use in other scripts
_window.initRefresh = initRefresh;
// Append styles
addStylesheet(`
#somu-refresh-queue-counter {
position:fixed;
bottom:0;
left:50%;
line-height:2em;
transform:translateX(-50%);
}
.js-admin-dashboard > div > div > fieldset {
display: none;
}
`); // end stylesheet
// On script run
(function init() {
// If no mod flags, insert mod flags indicator in header anyway...
if ($('.js-admin-dashboard-button').length === 0) {
$('.js-mod-inbox-button').parent().after(`
<li><a href="/admin/dashboard" class="s-topbar--item px4 js-admin-dashboard-button" aria-label="no flagged posts" title="no posts flagged for moderator attention">
<span class="s-badge s-badge__bounty">0</span>
</a></li>`);
}
// If not on mod flag pages, ignore rest of script
if (!isModDashboardPage || ($('.js-admin-dashboard').length == 0 && !document.body.classList.contains('flag-page'))) return;
// If completely no post flags, redirect to main
if ($('.s-sidebarwidget--header .bounty-indicator-tab').length === 0 && $('.so-flag, .m-flag, .c-flag').length === 0) {
initRefresh(true);
}
// Refresh if no flags left in current queue
else {
initRefresh();
}
// On user action on page, restart and lengthen countdown
$(document).on('mouseup keyup', 'body', function () {
if (timeout) timeoutSecs++;
initRefresh();
});
// On skip post link click
$('.js-flagged-post, .flagged-post-row').on('click', '.skip-post', initRefresh);
// When ajax requests have completed
$(document).ajaxComplete(function (event, xhr, settings) {
// If post deleted, remove from queue
if (!settings.url.includes('/comments/') && settings.url.includes('/vote/10')) {
const pid = settings.url.match(/\/\d+\//)[0].replace(/\//g, '');
$('#flagged-' + pid).remove();
// Refresh if no flags in current queue
initRefresh();
}
});
// When flags are handled, refresh if no flags in current queue
$(document).ajaxStop(initRefresh);
})();