Skip to content

Instantly share code, notes, and snippets.

@dominikkaegi
Last active January 19, 2022 16:04
Show Gist options
  • Select an option

  • Save dominikkaegi/a9d3262846bc846ace4f5dcba627928e to your computer and use it in GitHub Desktop.

Select an option

Save dominikkaegi/a9d3262846bc846ace4f5dcba627928e to your computer and use it in GitHub Desktop.
// A simple Bookmarklet which calculates the time you have worked at a given day
// 1. Go to https://zeitplan.hcweb.ch
// 2. Log in and select the day you want to evaluate
// 3. Click on the Bookmarklet you created with the code below
// To pulish, first minify / uglify it before you throw it into the book
// marklet create:
// 1. Uglify https://skalman.github.io/UglifyJS-online/
// 2. Bokkmarkify: https://mrcoles.com/bookmarklet/
(() => {
let table = document.querySelector('.polypoint')
.querySelectorAll('table')[1]
let stempel = Array.from(table.querySelectorAll('tr'))
.filter((item => item.className.includes('polypoint-zebra')))
.map(item => {
temp = item.querySelectorAll('td')
checkin = ((temp) => temp[0].innerHTML.includes('beginn'))(temp)
let [hour, minutes] = temp[2].innerText.split(':')
.map(item => parseInt(item))
let time = new Date()
time.setMinutes(minutes)
time.setHours(hour)
return {
checkin,
time
}
})
// Takes two dates and returns the milliseconds passed
// between them
function getTimeDiff(date1, date2) {
let timeDiff = Math.abs(date2.getTime() - date1.getTime());
return timeDiff
}
function TimeCounter() {
milliseconds = 0;
const millisecondsToHours = (1000 * 60 * 60)
const milliscondsToMinutes = (1000 * 60)
const getTime = () => milliseconds
const addTime = (addedMillisconds) => milliseconds += addedMillisconds
const getHours = () => Math.floor(milliseconds / millisecondsToHours)
const getMinutes = () => {
const millsecondsCountingToMinutes = milliseconds % millisecondsToHours
return Math.floor(millsecondsCountingToMinutes / milliscondsToMinutes)
}
return {
getTime,
addTime,
getHours,
getMinutes
}
}
timeWorked = TimeCounter()
for (let i = 0; i < stempel.length; i += 2) {
// Hat es ein Austempel zu diesem Einstempel?
if (stempel[i+1]) {
// Ja, nehme die Stempel Zeit
const diff = getTimeDiff(stempel[i].time, stempel[i+1].time)
timeWorked.addTime(diff)
} else {
// Nein, nimm die aktuelle Zeit
const diff = getTimeDiff(stempel[i].time, new Date())
timeWorked.addTime(diff)
}
}
const message = `You have worked ${timeWorked.getHours()} hours and ${timeWorked.getMinutes()} minutes`
console.log(message);
alert(message)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment