Created
December 19, 2022 13:42
-
-
Save samthecodingman/ffab88d955a7ed4e3f47a700be470297 to your computer and use it in GitHub Desktop.
Discover your longest StackOverflow/StackExchange login streak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! stackexchange-max-login-streak.js | Samuel Jones 2022 | MIT License | gist.github.com/samthecodingman */ | |
/* | |
* == USAGE == | |
* 1. Review and vet the below code | |
* 2. Open your profile page on the relevant SE site (e.g. https://stackoverflow.com/users/3068190/samthecodingman) | |
* 3. Open your Browser's JavaScript console (usually Ctrl+Shift+J or F12) | |
* 4. Paste and execute this helper script | |
* 5. Take note of the stats logged to the console | |
*/ | |
if (typeof visited === 'undefined') { | |
// if visited isn't present: open, load, and close the daily access calendar widget | |
const btnDailyAccessCal = document.querySelector(".js-daily-access-calendar-toggle"); | |
btnDailyAccessCal.click(); | |
await new Promise(res => setTimeout(res, 1000)); | |
btnDailyAccessCal.click(); | |
} | |
// init vars | |
let vistedDates = [], | |
currentStreakDays = 0, | |
maxStreakDays = 0, | |
currentStreakStartDateStr = null, | |
checkDateStr = null, | |
nextDateStr = null, | |
maxStreakDetails = null; | |
// parse global "visited" object into ordered array of dates "vistedDates" | |
for (let yearStr in visited) { | |
for (let monthStr in visited[yearStr]) { | |
for (let dayStr in visited[yearStr][monthStr]) { | |
if (visited[yearStr][monthStr][dayStr]) { | |
vistedDates.push(`${yearStr}-${("0" + monthStr).slice(-2)}-${("0" + dayStr).slice(-2)}`); | |
} | |
} | |
} | |
} | |
// for each date, check if a streak occurred | |
for (let dateStr of vistedDates) { | |
checkDateStr = nextDateStr; | |
nextDateStr = new Date((new Date(dateStr)).getTime() + 86410000).toISOString().slice(0,10); // gets YYYY-MM-DD of the day after dateStr | |
// current streak continues? | |
if (checkDateStr === dateStr) { | |
currentStreakDays++; | |
continue; | |
} | |
// streak was broken. Was this streak the best so far? | |
if (currentStreakDays > maxStreakDays) { | |
maxStreakDetails = { | |
startDateStr: currentStreakStartDateStr, | |
endDateStr: new Date((new Date(checkDateStr)).getTime() - 3600000).toISOString().slice(0,10), // gets YYYY-MM-DD of the day before checkDateStr | |
days: currentStreakDays | |
} | |
maxStreakDays = currentStreakDays | |
} | |
// reset tracked streak | |
currentStreakDays = 1; | |
currentStreakStartDateStr = dateStr; | |
} | |
// output results | |
console.log([ | |
"You have logged in on " + vistedDates.length + " different days, with a current streak of " + currentStreakDays + " days (starting on " + currentStreakStartDateStr + " UTC)", | |
maxStreakDetails | |
? "Your best login streak was " + maxStreakDetails.days + " days (" + (maxStreakDetails.days == 1 ? "first on " + maxStreakDetails.startDateStr : maxStreakDetails.startDateStr + " to " + maxStreakDetails.endDateStr) + " UTC)." | |
: "You are currently on your best login streak." | |
].join("\n")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment