Skip to content

Instantly share code, notes, and snippets.

@maxpatiiuk
Last active May 1, 2024 15:30
Show Gist options
  • Select an option

  • Save maxpatiiuk/f315cd3cdafa4a68cdd9838a4d70e05d to your computer and use it in GitHub Desktop.

Select an option

Save maxpatiiuk/f315cd3cdafa4a68cdd9838a4d70e05d to your computer and use it in GitHub Desktop.
Calculate the total number of time spent on a given event in a given week
/*
* This sript is now deprecated as it is integrated into my Google Chrome extension,
* along with many Google Calendar power user features:
* https://chrome.google.com/webstore/detail/calendar-plus/kgbbebdcmdgkbopcffmpgkgcmcoomhmh
*/
/*
* Open Google Calendar in a Week view
* Open DevTools
* Paste this script
* Adjust the parameters below
* Run it
*
* NOTE: If event begins the previous day, it is only counted once, on the day it begun
*/
((
eventNames = ["Max available", "Max WFH"], // Change this to your value
calendarName = "Informatics Student Schedules", // Change this to your value
) =>
Array.from(document.querySelectorAll('[role="gridcell"][data-column-index]'), (node) =>
Array.from(
node.querySelectorAll('[role="button"][tabindex="0"] > div:not([aria-hidden])'),
(event) =>
event.textContent.split(", "),
)
.filter(
(eventData, index) =>
eventNames.some((eventName) => eventData.includes(eventName)) &&
eventData.includes(`Calendar: ${calendarName}`) &&
(eventData.length === 6 || index !== 0),
)
.map((eventData) => {
const times = eventData[0].match(/\d\d:\d\d/g);
const resolvedTimes =
times?.length === 2
? times
: [
eventData[1].match(/\d\d:\d\d/)[0],
eventData[2].match(/\d\d:\d\d/)[0],
];
return resolvedTimes.map((time) =>
time
.split(":")
.reduce(
(total, number, index) =>
total + parseInt(number) / (index ? 60 : 1),
0,
),
);
})
.map(
([eventStart, eventEnd]) =>
eventEnd - eventStart + (eventStart > eventEnd ? 24 : 0),
)
.reduce((dayTotal, eventTotal) => dayTotal + eventTotal, 0),
))()//.reduce((total, value) => total + value, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment