forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefreshEmptyModQueue.user.js
130 lines (104 loc) · 4.2 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
// ==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 @samliew
// @version 2.6.1
//
// @include https://*stackoverflow.com/*
// @include https://*serverfault.com/*
// @include https://*superuser.com/*
// @include https://*askubuntu.com/*
// @include https://*mathoverflow.net/*
// @include https://*.stackexchange.com/*
//
// @exclude *chat.*
// @exclude https://stackoverflow.com/c/*
// ==/UserScript==
(function() {
'use strict';
const goToMain = () => location.href = '/admin/dashboard?filtered=false';
const reloadPage = () => location.search.contains('filtered=false') ? location.reload(true) : location.search += (location.search.length == 0 ? '' : '&') + 'filtered=false';
let timeoutSecs = unsafeWindow.modRefreshInterval || 10;
let timeout, interval;
let 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);
};
unsafeWindow.initRefresh = initRefresh;
function doPageload() {
// If no mod flags, insert mod flags indicator in header anyway...
if($('.flag-count-item').length === 0) {
$('.js-mod-inbox-button').parent().after(`<li class="-item flag-count-item" data-remove-order="3">
<a href="/admin/dashboard" class="-link _text-only" title="no flags!">
<span class="indicator-badge _regular">0</span>
</a>
</li>`);
}
// If not on mod flag pages, ignore rest of script
if(!location.pathname.includes('/admin/dashboard') || ( $('.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();
});
// 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);
// On skip post link click
$('.js-flagged-post, .flagged-post-row').on('click', '.skip-post', initRefresh);
}
function appendStyles() {
const styles = `
<style>
#somu-refresh-queue-counter {
position:fixed;
bottom:0;
left:50%;
line-height:2em;
transform:translateX(-50%);
}
.js-admin-dashboard > div > div > fieldset {
display: none;
}
</style>
`;
$('body').append(styles);
}
// On page load
appendStyles();
doPageload();
})();