@@ -35,55 +35,70 @@ select "SCP-Wiki Staff Identification", and click Uninstall.
35
35
// ==/UserScript==
36
36
37
37
"use strict" ;
38
- var staff , doCount = 0 ;
38
+ var staff ,
39
+ doCount = 0 ;
40
+ var day = 1000 * 60 * 60 * 24 ;
39
41
40
42
// page is loaded, let's do this
41
43
getStaffList ( ) ;
42
44
43
45
// we also need to do this whenever the user changes page
44
- jQuery ( document ) . on ( "click" , ".pager .target a" , function ( e ) {
46
+ jQuery ( document ) . on ( "click" , ".pager .target a" , function ( ) {
45
47
doCount = 0 ;
46
48
setStaffIds ( staff ) ;
47
49
} ) ;
48
50
49
-
50
51
//the data should already be fetched, so we can skip the fetching step
51
52
52
53
// fetch the whole list of staff from 05command
53
54
function getStaffList ( ) {
54
- GM . xmlHttpRequest ( {
55
- method : "GET" ,
56
- url : "http://05command.wikidot.com/staff-list" ,
57
- /*headers: {
58
- "User-Agent": "Mozilla/5.0", // If not specified, navigator.userAgent will be used.
59
- },*/
60
- timeout : 10000 ,
61
- onload : function ( response ) {
62
- structureStaffList ( response . responseText ) ;
63
- } ,
64
- onerror : function ( response ) {
65
- console . error ( "An error occurred while fetching staff data" ) ;
66
- } ,
67
- ontimeout : function ( response ) {
68
- console . error ( "The request to fetch staff data timed out" ) ;
69
- }
70
- } ) ;
55
+ var lastFetchedTimestamp = localStorage . getItem ( "scp-staff-ids-timestamp" ) ;
56
+ var lastFetchedResponse = localStorage . getItem ( "scp-staff-ids-response" ) ;
57
+ var useCachedResponse =
58
+ lastFetchedTimestamp != null &&
59
+ lastFetchedResponse != null &&
60
+ new Date ( lastFetchedTimestamp ) . getTime ( ) + day > new Date ( ) . getTime ( ) ;
61
+
62
+ if ( useCachedResponse ) {
63
+ console . info ( "SCP Wiki Staff ID: Using cached staff list" ) ;
64
+ structureStaffList ( lastFetchedResponse ) ;
65
+ } else {
66
+ console . info ( "SCP Wiki Staff ID: Fetching new staff list" ) ;
67
+ GM . xmlHttpRequest ( {
68
+ method : "GET" ,
69
+ url : "http://05command.wikidot.com/staff-list" ,
70
+ timeout : 10000 ,
71
+ onload : function ( response ) {
72
+ localStorage . setItem ( "scp-staff-ids-timestamp" , new Date ( ) ) ;
73
+ localStorage . setItem ( "scp-staff-ids-response" , response . responseText ) ;
74
+ structureStaffList ( response . responseText ) ;
75
+ } ,
76
+ onerror : function ( ) {
77
+ console . error ( "An error occurred while fetching staff data" ) ;
78
+ } ,
79
+ ontimeout : function ( ) {
80
+ console . error ( "The request to fetch staff data timed out" ) ;
81
+ } ,
82
+ } ) ;
83
+ }
71
84
}
72
85
73
86
// rummage through the list of staff and twist it into a format that JS understands
74
87
function structureStaffList ( staffText ) {
75
88
var parser = new DOMParser ( ) ;
76
- var staffList = parser . parseFromString ( staffText , "application/xml" ) . getElementById ( 'page-content' ) ;
89
+ var staffList = parser
90
+ . parseFromString ( staffText , "application/xml" )
91
+ . getElementById ( "page-content" ) ;
77
92
// next thing to do is to compile a list of all of the staff members
78
93
staff = [ ] ;
79
94
var staffType = "Staff Member" ;
80
95
// 4 tables: admin, mod, opstaff, jstaff
81
96
82
- for ( let node = 0 ; node < staffList . childNodes . length ; node ++ ) {
97
+ for ( let node = 0 ; node < staffList . childNodes . length ; node ++ ) {
83
98
var currNode = staffList . childNodes [ node ] ;
84
99
85
100
// if the current node is not a table, we don't care about it, but if it's a title then we can use it to get the current staff type instead of hardcoding that
86
- switch ( currNode . nodeName . toLowerCase ( ) ) {
101
+ switch ( currNode . nodeName . toLowerCase ( ) ) {
87
102
case "table" :
88
103
break ;
89
104
case "h1" :
@@ -95,35 +110,44 @@ function structureStaffList(staffText) {
95
110
}
96
111
97
112
// if we got here, then we need to go deeper into the table
98
- for ( let i = 0 ; i < currNode . childNodes . length ; i ++ ) { // starting at 1 because the first tr is the title
113
+ for ( let i = 0 ; i < currNode . childNodes . length ; i ++ ) {
114
+ // starting at 1 because the first tr is the title
99
115
var tr = currNode . childNodes [ i ] ;
100
116
// there's a lot of empty text nodes for some reason, so we ignore these
101
- if ( tr . nodeName !== "tr" ) continue ;
117
+ if ( tr . nodeName !== "tr" ) continue ;
102
118
103
119
// iterate through the columns of the tr
104
- var td , columns = [ ] ;
105
- for ( let j = 0 ; j < tr . childNodes . length ; j ++ ) {
120
+ var td ,
121
+ columns = [ ] ;
122
+ for ( let j = 0 ; j < tr . childNodes . length ; j ++ ) {
106
123
td = tr . childNodes [ j ] ;
107
124
// there's a lot of empty text nodes for some reason, so we remove these
108
- if ( td . nodeName !== "td" ) continue ;
125
+ if ( td . nodeName !== "td" ) continue ;
109
126
// so each td is, in order: user | teams | timezone | activity | contact | captain
110
127
// 0 1 2 3 4 5
111
128
// for JS, only 0 and 1 exist
112
129
// now we shove each td into a clean array so we can iterate over it without the messy text nodes ruining life for everyone
113
130
columns . push ( td ) ;
114
131
}
115
132
116
- var staffmember = { username : "" , teams : [ ] , active : "Active" , captain : [ ] , type : staffType } ;
133
+ var staffmember = {
134
+ username : "" ,
135
+ teams : [ ] ,
136
+ active : "Active" ,
137
+ captain : [ ] ,
138
+ type : staffType ,
139
+ } ;
117
140
118
- for ( let j = 0 ; j < columns . length ; j ++ ) {
119
- switch ( j ) {
141
+ for ( let j = 0 ; j < columns . length ; j ++ ) {
142
+ switch ( j ) {
120
143
case 0 : // username
121
144
// extract the username from [[*user username]]
122
- staffmember . username = columns [ j ] . childNodes [ 0 ] . childNodes [ 1 ] . textContent ;
145
+ staffmember . username =
146
+ columns [ j ] . childNodes [ 0 ] . childNodes [ 1 ] . textContent ;
123
147
break ;
124
148
case 1 : // teams
125
149
staffmember . teams = columns [ j ] . textContent . split ( ", " ) ;
126
- if ( staffmember . teams [ 0 ] === "-" ) staffmember . teams = [ ] ;
150
+ if ( staffmember . teams [ 0 ] === "-" ) staffmember . teams = [ ] ;
127
151
break ;
128
152
case 3 : // activity
129
153
staffmember . active = columns [ j ] . textContent ;
@@ -134,7 +158,7 @@ function structureStaffList(staffText) {
134
158
}
135
159
}
136
160
// now let's do something incredibly lazy to drop this member if the tr is a title
137
- if ( staffmember . username === "" ) continue ;
161
+ if ( staffmember . username === "" ) continue ;
138
162
// push staff data into the staff list
139
163
staff . push ( staffmember ) ;
140
164
}
@@ -145,20 +169,22 @@ function structureStaffList(staffText) {
145
169
// run through the forum page and add the staff roles
146
170
function setStaffIds ( ) {
147
171
var container ;
148
- if ( document . getElementById ( ' thread-container' ) ) {
149
- container = document . getElementById ( ' thread-container' ) ;
172
+ if ( document . getElementById ( " thread-container" ) ) {
173
+ container = document . getElementById ( " thread-container" ) ;
150
174
} else {
151
- container = document . getElementsByClassName ( ' thread-container' ) [ 0 ] ;
175
+ container = document . getElementsByClassName ( " thread-container" ) [ 0 ] ;
152
176
}
153
177
154
- var infoSpans = container . getElementsByClassName ( ' info' ) ;
178
+ var infoSpans = container . getElementsByClassName ( " info" ) ;
155
179
var userName = "" ;
156
180
var staffName , staffId ;
157
181
158
182
for ( var x = 0 ; x < infoSpans . length ; x ++ ) {
159
183
try {
160
- userName = infoSpans [ x ] . getElementsByTagName ( 'span' ) [ 0 ] . getElementsByTagName ( 'a' ) [ 1 ] . innerHTML ;
161
- } catch ( error ) {
184
+ userName = infoSpans [ x ]
185
+ . getElementsByTagName ( "span" ) [ 0 ]
186
+ . getElementsByTagName ( "a" ) [ 1 ] . innerHTML ;
187
+ } catch ( error ) {
162
188
// so far as I can tell this only errors for a deleted account, so ignore it
163
189
continue ;
164
190
}
@@ -174,29 +200,32 @@ function setStaffIds() {
174
200
// I want to format this as "Administrator - Disciplinary" or "Junior Staff - Technical" or "Operational Staff (Inactive)"
175
201
staffId = "SCP Wiki - " + staff [ y ] . type ;
176
202
177
- if ( staff [ y ] . active . toLowerCase ( ) !== "active" ) staffId += " (" + staff [ y ] . active + ")" ;
203
+ if ( staff [ y ] . active . toLowerCase ( ) !== "active" )
204
+ staffId += " (" + staff [ y ] . active + ")" ;
178
205
179
- if ( staff [ y ] . captain . length > 0 ) {
180
- for ( let i = 0 ; i < staff [ y ] . captain . length ; i ++ ) {
181
- for ( let j = 0 ; j < staff [ y ] . teams . length ; j ++ ) {
182
- if ( staff [ y ] . captain [ i ] === staff [ y ] . teams [ j ] ) staff [ y ] . teams [ j ] += " (Captain)" ;
206
+ if ( staff [ y ] . captain . length > 0 ) {
207
+ for ( let i = 0 ; i < staff [ y ] . captain . length ; i ++ ) {
208
+ for ( let j = 0 ; j < staff [ y ] . teams . length ; j ++ ) {
209
+ if ( staff [ y ] . captain [ i ] === staff [ y ] . teams [ j ] )
210
+ staff [ y ] . teams [ j ] += " (Captain)" ;
183
211
}
184
212
}
185
213
}
186
- if ( staff [ y ] . teams . length > 0 ) staffId += " - " + staff [ y ] . teams . join ( ", " ) ;
214
+ if ( staff [ y ] . teams . length > 0 )
215
+ staffId += " - " + staff [ y ] . teams . join ( ", " ) ;
187
216
}
188
217
}
189
218
190
219
if ( staffId !== "" ) {
191
- var br = infoSpans [ x ] . getElementsByTagName ( 'br' ) [ 0 ] ;
192
- var staffSpan = document . createElement ( ' span' ) ;
220
+ var br = infoSpans [ x ] . getElementsByTagName ( "br" ) [ 0 ] ;
221
+ var staffSpan = document . createElement ( " span" ) ;
193
222
staffSpan . style . fontSize = "0.8em" ;
194
223
staffSpan . innerHTML = staffId + "<br>" ;
195
224
196
225
if ( br ) {
197
226
infoSpans [ x ] . insertBefore ( staffSpan , br . nextSibling ) ;
198
227
} else {
199
- br = document . createElement ( 'br' ) ;
228
+ br = document . createElement ( "br" ) ;
200
229
infoSpans [ x ] . appendChild ( br ) ;
201
230
infoSpans [ x ] . appendChild ( staffSpan ) ;
202
231
}
@@ -205,5 +234,8 @@ function setStaffIds() {
205
234
}
206
235
// repeat this a few times just so that we catch everything if the forum loads slowly
207
236
doCount ++ ;
208
- if ( doCount < 10 ) setTimeout ( function ( ) { setStaffIds ( ) } , 500 ) ;
237
+ if ( doCount < 10 )
238
+ setTimeout ( function ( ) {
239
+ setStaffIds ( ) ;
240
+ } , 500 ) ;
209
241
}
0 commit comments