@@ -35,55 +35,70 @@ select "SCP-Wiki Staff Identification", and click Uninstall.
3535// ==/UserScript==
3636
3737"use strict" ;
38- var staff , doCount = 0 ;
38+ var staff ,
39+ doCount = 0 ;
40+ var day = 1000 * 60 * 60 * 24 ;
3941
4042// page is loaded, let's do this
4143getStaffList ( ) ;
4244
4345// 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 ( ) {
4547 doCount = 0 ;
4648 setStaffIds ( staff ) ;
4749} ) ;
4850
49-
5051//the data should already be fetched, so we can skip the fetching step
5152
5253// fetch the whole list of staff from 05command
5354function 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+ }
7184}
7285
7386// rummage through the list of staff and twist it into a format that JS understands
7487function structureStaffList ( staffText ) {
7588 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" ) ;
7792 // next thing to do is to compile a list of all of the staff members
7893 staff = [ ] ;
7994 var staffType = "Staff Member" ;
8095 // 4 tables: admin, mod, opstaff, jstaff
8196
82- for ( let node = 0 ; node < staffList . childNodes . length ; node ++ ) {
97+ for ( let node = 0 ; node < staffList . childNodes . length ; node ++ ) {
8398 var currNode = staffList . childNodes [ node ] ;
8499
85100 // 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 ( ) ) {
87102 case "table" :
88103 break ;
89104 case "h1" :
@@ -95,35 +110,44 @@ function structureStaffList(staffText) {
95110 }
96111
97112 // 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
99115 var tr = currNode . childNodes [ i ] ;
100116 // 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 ;
102118
103119 // 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 ++ ) {
106123 td = tr . childNodes [ j ] ;
107124 // 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 ;
109126 // so each td is, in order: user | teams | timezone | activity | contact | captain
110127 // 0 1 2 3 4 5
111128 // for JS, only 0 and 1 exist
112129 // now we shove each td into a clean array so we can iterate over it without the messy text nodes ruining life for everyone
113130 columns . push ( td ) ;
114131 }
115132
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+ } ;
117140
118- for ( let j = 0 ; j < columns . length ; j ++ ) {
119- switch ( j ) {
141+ for ( let j = 0 ; j < columns . length ; j ++ ) {
142+ switch ( j ) {
120143 case 0 : // username
121144 // 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 ;
123147 break ;
124148 case 1 : // teams
125149 staffmember . teams = columns [ j ] . textContent . split ( ", " ) ;
126- if ( staffmember . teams [ 0 ] === "-" ) staffmember . teams = [ ] ;
150+ if ( staffmember . teams [ 0 ] === "-" ) staffmember . teams = [ ] ;
127151 break ;
128152 case 3 : // activity
129153 staffmember . active = columns [ j ] . textContent ;
@@ -134,7 +158,7 @@ function structureStaffList(staffText) {
134158 }
135159 }
136160 // 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 ;
138162 // push staff data into the staff list
139163 staff . push ( staffmember ) ;
140164 }
@@ -145,20 +169,22 @@ function structureStaffList(staffText) {
145169// run through the forum page and add the staff roles
146170function setStaffIds ( ) {
147171 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" ) ;
150174 } else {
151- container = document . getElementsByClassName ( ' thread-container' ) [ 0 ] ;
175+ container = document . getElementsByClassName ( " thread-container" ) [ 0 ] ;
152176 }
153177
154- var infoSpans = container . getElementsByClassName ( ' info' ) ;
178+ var infoSpans = container . getElementsByClassName ( " info" ) ;
155179 var userName = "" ;
156180 var staffName , staffId ;
157181
158182 for ( var x = 0 ; x < infoSpans . length ; x ++ ) {
159183 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 ) {
162188 // so far as I can tell this only errors for a deleted account, so ignore it
163189 continue ;
164190 }
@@ -174,29 +200,32 @@ function setStaffIds() {
174200 // I want to format this as "Administrator - Disciplinary" or "Junior Staff - Technical" or "Operational Staff (Inactive)"
175201 staffId = "SCP Wiki - " + staff [ y ] . type ;
176202
177- if ( staff [ y ] . active . toLowerCase ( ) !== "active" ) staffId += " (" + staff [ y ] . active + ")" ;
203+ if ( staff [ y ] . active . toLowerCase ( ) !== "active" )
204+ staffId += " (" + staff [ y ] . active + ")" ;
178205
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)" ;
183211 }
184212 }
185213 }
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 ( ", " ) ;
187216 }
188217 }
189218
190219 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" ) ;
193222 staffSpan . style . fontSize = "0.8em" ;
194223 staffSpan . innerHTML = staffId + "<br>" ;
195224
196225 if ( br ) {
197226 infoSpans [ x ] . insertBefore ( staffSpan , br . nextSibling ) ;
198227 } else {
199- br = document . createElement ( 'br' ) ;
228+ br = document . createElement ( "br" ) ;
200229 infoSpans [ x ] . appendChild ( br ) ;
201230 infoSpans [ x ] . appendChild ( staffSpan ) ;
202231 }
@@ -205,5 +234,8 @@ function setStaffIds() {
205234 }
206235 // repeat this a few times just so that we catch everything if the forum loads slowly
207236 doCount ++ ;
208- if ( doCount < 10 ) setTimeout ( function ( ) { setStaffIds ( ) } , 500 ) ;
237+ if ( doCount < 10 )
238+ setTimeout ( function ( ) {
239+ setStaffIds ( ) ;
240+ } , 500 ) ;
209241}
0 commit comments