-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-member.user.js
235 lines (199 loc) · 6.6 KB
/
find-member.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
/*
* Wikidot member list locator userscript
*
* For installation instructions, see https://scpwiki.com/usertools
*/
// ==UserScript==
// @name Wikidot find member script
// @description Adds a button to search for members in the Wikidot admin panel.
// @version v0.1.4
// @updateURL https://github.com/scpwiki/find-member-script/raw/main/find-member.user.js
// @downloadURL https://github.com/scpwiki/find-member-script/raw/main/find-member.user.js
// @supportURL https://www.wikidot.com/account/messages#/new/4598089
// @include http://*.wikidot.com/_admin
// @include https://*.wikidot.com/_admin
// ==/UserScript==
const MAX_STEPS = 20;
const CSS = `
.findmember-notice {
font-family: 'Courier New', monospace;
text-align: center;
font-size: 1.2em;
}
.findmember-success {
color: green;
font-weight: bold;
}
.findmember-error {
color: red;
font-weight: bold;
}
`;
const WIKIDOT = typeof unsafeWindow === 'undefined'
? window.WIKIDOT
: unsafeWindow.WIKIDOT;
if (WIKIDOT === undefined) {
alert('Cannot load WIKIDOT.js');
}
let dateEntryInput;
let noticeElement;
let currentlyRunning = false;
function parseOdate(element) {
for (let i = 0; i < element.classList.length; i++) {
klass = element.classList[i];
if (klass.startsWith('time_')) {
const timestamp = parseInt(klass.slice(5));
return new Date(timestamp * 1000);
}
}
}
function dateInRange(date, start, end) {
return start <= date && date <= end;
}
function notice(message) {
noticeElement.classList.remove('findmember-error');
noticeElement.classList.remove('findmember-success');
noticeElement.innerText = message;
}
function error(message) {
noticeElement.classList.remove('findmember-success');
noticeElement.classList.add('findmember-error');
noticeElement.innerText = message;
}
function success(message) {
noticeElement.classList.remove('findmember-error');
noticeElement.classList.add('findmember-success');
noticeElement.innerText = message;
}
async function openMemberPage(pageNum) {
WIKIDOT.modules.ManageSiteMembersListModule.listeners.loadMemberList(pageNum);
await new Promise(resolve => {
const element = document.querySelector('#all-members');
const observer = new MutationObserver(() => {
for (let i = 0; i < element.childNodes.length; i++) {
const child = element.childNodes[i];
if (child.id == 'members-list') {
observer.disconnect();
resolve();
return;
}
}
});
observer.observe(element, { childList: true });
});
}
function doSearch() {
if (currentlyRunning) {
console.warn('Already running, ignoring input');
return;
}
try {
currentlyRunning = true;
binarySearch();
} finally {
currentlyRunning = false;
}
}
async function binarySearch() {
const rawDate = dateEntryInput.value;
const date = new Date(Date.parse(rawDate));
if (isNaN(date)) {
error(`Invalid date entry: '${rawDate}'.`);
return;
}
const pageButton = document.querySelector('span.target:nth-last-child(2)');
const pageCount = parseInt(pageButton.innerText);
let steps = 0;
let startPage = 1;
let endPage = pageCount;
let membersThisPage, startDate, endDate, thisPage;
let lastStart = null;
let lastEnd = null;
while (true) {
const offset = Math.trunc((endPage - startPage) / 2);
thisPage = startPage + offset;
notice(`#${steps}: Trying page ${thisPage} [start ${startPage}, end ${endPage}]`);
await openMemberPage(thisPage);
membersThisPage = document.querySelectorAll('#all-members span.odate');
if (!membersThisPage.length) {
error('No members found on page?');
return;
}
startDate = parseOdate(membersThisPage[0]);
endDate = parseOdate(membersThisPage[membersThisPage.length - 1]);
// Adjust bounds based on binary search
if (startDate > date) {
// Date is before current page
endPage = thisPage;
} else if (endDate < date) {
// Date is after current page
startPage = thisPage;
} else if (dateInRange(date, startDate, endDate)) {
success(`Found date ${rawDate} on page ${thisPage} after ${steps} steps.`);
return;
}
// Safety valves in case of bugs, or if it's out of range
if (lastStart == startPage && lastEnd == endPage) {
error('Cannot find date, out of range.');
return;
}
lastStart = startPage;
lastEnd = endPage;
steps++;
if (steps > MAX_STEPS) {
error('BUG: Too many steps');
return;
}
}
}
async function main() {
// Wait until the member list is pulled up.
await new Promise(resolve => {
console.log('Creating observer, waiting for members list to be opened');
const element = document.querySelector('#sm-action-area');
const observer = new MutationObserver(() => {
for (let i = 0; i < element.childNodes.length; i++) {
const child = element.childNodes[i];
if (child.id == 'MembersTab') {
console.log('Opened members list, begin initialization');
observer.disconnect();
resolve();
return;
}
}
});
observer.observe(element, { childList: true });
});
// Check that we're on the member list page
const memberElement = document.querySelector('#all-members');
if (!memberElement) {
alert('No member list?');
return;
}
// Create the UI elements
const searchContainer = document.createElement('div');
dateEntryInput = document.createElement('input');
dateEntryInput.classList.add('findmember-input');
dateEntryInput.setAttribute('type', 'text');
dateEntryInput.setAttribute('size', '10');
dateEntryInput.setAttribute('style', 'margin-bottom: 0;');
dateEntryInput.setAttribute('placeholder', '12 May 2014');
searchContainer.appendChild(dateEntryInput);
const searchButton = document.createElement('button');
searchButton.innerText = 'Search';
searchButton.classList.add('findmember-button', 'btn', 'btn-xs')
searchButton.title = 'Search all member pages until the page with this date on it is found';
searchButton.addEventListener('click', () => doSearch());
searchContainer.appendChild(searchButton);
const membersTab = document.querySelector('#MembersTab');
membersTab.appendChild(searchContainer);
noticeElement = document.createElement('div');
noticeElement.classList.add('findmember-notice');
const actionArea = document.querySelector('#sm-action-area');
const tabContent = document.querySelector('.tab-content');
actionArea.insertBefore(noticeElement, tabContent);
const styleSheet = document.createElement('style');
styleSheet.innerText = CSS;
document.head.appendChild(styleSheet);
}
main();