Skip to content

Commit f40f1ed

Browse files
committed
Cache staff list if downloaded < a day ago
1 parent e5ace5f commit f40f1ed

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

scpwiki-staff-ids.user.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ select "SCP-Wiki Staff Identification", and click Uninstall.
3737
"use strict";
3838
var staff,
3939
doCount = 0;
40+
var day = 1000 * 60 * 60 * 24;
4041

4142
// page is loaded, let's do this
4243
getStaffList();
@@ -51,23 +52,35 @@ jQuery(document).on("click", ".pager .target a", function () {
5152

5253
// fetch the whole list of staff from 05command
5354
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 () {
65-
console.error("An error occurred while fetching staff data");
66-
},
67-
ontimeout: function () {
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

0 commit comments

Comments
 (0)