Created
June 7, 2021 14:37
-
-
Save mattogodoy/d30ec669ca63d2839af5c2545136fdad to your computer and use it in GitHub Desktop.
Violent Monkey script to automatically fill times for the entire week in TimeTool
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
| // ==UserScript== | |
| // @name Fill week time - TimeTool | |
| // @namespace Violentmonkey Scripts | |
| // @match https://www.ttcloud.ch/timetool/db_admin/start.html | |
| // @grant none | |
| // @version 1.0 | |
| // @author - | |
| // @description 04/06/2021, 13:49:17 | |
| // ==/UserScript== | |
| var timeEntriesMonThu = ['8:00', '13:30', '14:30', '17:30']; // With 1 hour break and 30 extra minutes per day | |
| var timeEntriesFri = ['8:00', '14:00']; // 6 hours with no break | |
| // Adds the wizard button | |
| function pwnAddButton(){ | |
| var toolBarEl = document.querySelector('.dhxtoolbar_float_right'); | |
| var button = '<a id="magic" href="javascript:void(0);">π§π»ββοΈ</a>'; | |
| var el = document.createElement('div'); | |
| el.className = 'pwn-magic dhx_toolbar_btn'; | |
| el.title = 'Do the magic!'; | |
| el.innerHTML = button; | |
| el.addEventListener('click', pwnAutoFillTimes); | |
| toolBarEl.appendChild(el); | |
| } | |
| // Gets the calendar iFrame | |
| function getIFrame(){ | |
| var iframe = document.querySelector('iframe'); | |
| if(!iframe){ | |
| console.log('ERROR: Not in the correct page - No iFrame found.'); | |
| return null; | |
| } | |
| var innerDoc = iframe.contentDocument || iframe.contentWindow.document; | |
| return innerDoc; | |
| } | |
| // Obtains the year of the week currently being shown | |
| function getYear(){ | |
| var iframeContents = getIFrame(); | |
| if(!iframeContents){ | |
| console.log('ERROR: Not in the correct page - No iFrame found.'); | |
| return null; | |
| } | |
| var yearEl = iframeContents.querySelector('.dhx_cal_date'); | |
| if(yearEl){ | |
| var currentYear = yearEl.innerText.split(' ')[6]; | |
| return currentYear; | |
| } else { | |
| console.log('ERROR: Could not get year'); | |
| return null; | |
| } | |
| } | |
| // Obtains and formats the days of the week currently being shown | |
| function getDates(){ | |
| var currentYear = getYear(); | |
| if(!currentYear){ | |
| console.log('ERROR: Not going any further.'); | |
| return null; | |
| } | |
| var iframeContents = getIFrame(); | |
| var dateElements = iframeContents.querySelectorAll('.dhx_scale_bar'); | |
| var dates = []; | |
| var months = ['NONE', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'Agust', 'September', 'October', 'November', 'December']; | |
| if(!currentYear || !dateElements){ | |
| console.log('ERROR: Missing elements'); | |
| return null; | |
| } | |
| for(i=0; i<=4; i++){ | |
| var date = dateElements[i].innerText.split(' '); | |
| var dayOfWeek = date[0].replace(',', ''); | |
| var currentMonth = String(months.indexOf(date[1])).padStart(2, '0'); | |
| var currentDay = String(date[2]).padStart(2, '0'); | |
| var formattedDate = currentYear + currentMonth + currentDay; | |
| var dateObj = { | |
| 'dow': dayOfWeek, | |
| 'value': formattedDate | |
| } | |
| dates.push(dateObj); | |
| } | |
| return dates; | |
| } | |
| // Obtainse the session ID | |
| function getSessionID(){ | |
| var iframe = document.querySelector('iframe'); | |
| if(!iframe){ | |
| console.log('ERROR: Not in the correct page - No iFrame found.'); | |
| return null; | |
| } | |
| var queryParams = iframe.src.split('&'); | |
| var sessionID = queryParams[1].split('=')[1]; | |
| //var dhxr = queryParams[2].split('=')[0]; | |
| //var dhxrValue = queryParams[2].split('=')[1]; | |
| //var dhxrNumber = dhxr.replace('dhxr', ''); | |
| return sessionID; | |
| } | |
| // Obtains the username | |
| function getUserName(){ | |
| var username = document.querySelector('.dhx_toolbar_text').innerText.split(' : ')[0]; | |
| return username; | |
| } | |
| // Generates the register URL | |
| function generateUrl(username, session, date, time){ | |
| var url = 'https://www.ttcloud.ch/cgi-bin/dhtml_appl_admin.cgi'; | |
| var parameters = [ | |
| 'cmd=addregi', | |
| 'login=' + username, | |
| 'session=' + session, | |
| 'date=' + date, | |
| 'time=' + time, | |
| 'stat=1', | |
| 'ucat=0' | |
| ]; | |
| var fullUrl = url + '?' + parameters.join('&'); | |
| return fullUrl; | |
| } | |
| // Generates an array that contains the register URLs for all of the times in all of the days of the week | |
| function generateUrlList(){ | |
| var dates = getDates(); | |
| var sessionID = getSessionID(); | |
| var username = getUserName(); | |
| var urlList = []; | |
| if(!dates || !sessionID || !username){ | |
| console.log('ERROR: Missing data.'); | |
| return null; | |
| } | |
| dates.forEach(function(date){ | |
| if(date['dow'] == 'Fri'){ | |
| timeEntriesFri.forEach(function(time){ | |
| urlList.push(generateUrl(username, sessionID, date['value'], time)); | |
| }); | |
| } else { | |
| timeEntriesMonThu.forEach(function(time){ | |
| urlList.push(generateUrl(username, sessionID, date['value'], time)); | |
| }); | |
| } | |
| }); | |
| return urlList; | |
| } | |
| // Makes the GET requests for the registration | |
| function makeGetRequest(url){ | |
| var request = new XMLHttpRequest(); | |
| request.open('GET', url, true); | |
| request.send(); | |
| request.onreadystatechange = function() { | |
| console.log(request.status, request.statusText); | |
| } | |
| } | |
| // Blocking delay | |
| function sleep(delay) { | |
| var start = new Date().getTime(); | |
| while (new Date().getTime() < start + delay); | |
| } | |
| // Non-blocking delay | |
| function pwnSleep(ms) { | |
| return new Promise(resolve => setTimeout(resolve, ms)); | |
| } | |
| // Registers times for all days in the week | |
| function registerAllWeekTimes(urlList){ | |
| urlList.forEach(function(url){ | |
| console.log('Registering URL:', url); | |
| makeGetRequest(url); | |
| sleep(500); | |
| }); | |
| } | |
| // Changes the emoji displayed in the button | |
| function changeButtonIcon(emoji){ | |
| var button = document.querySelector('#magic'); | |
| button.innerText = emoji; | |
| } | |
| // Main function for the button | |
| function pwnAutoFillTimes(){ | |
| var urlList = generateUrlList(); | |
| if(!urlList){ | |
| console.log('ERROR: No URL list'); | |
| changeButtonIcon('β'); | |
| return null; | |
| } | |
| console.log('Auto-filling times... πͺ'); | |
| changeButtonIcon('β³'); | |
| // Wait for the icon to update before beginning the blocking register function | |
| pwnSleep(100).then(() => { | |
| registerAllWeekTimes(urlList); | |
| console.log('Done! β¨'); | |
| changeButtonIcon('β'); | |
| alert('Done! π§π»ββοΈ πͺ'); | |
| }); | |
| } | |
| // Wait for the page to finish loading with a non-blocking delay | |
| pwnSleep(1000).then(() => { | |
| console.log("Initializing magic..."); | |
| pwnAddButton(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment