Skip to content

Instantly share code, notes, and snippets.

@khromov
Last active August 12, 2022 12:48
Show Gist options
  • Save khromov/b58633199b20ad33a83c5a679870033f to your computer and use it in GitHub Desktop.
Save khromov/b58633199b20ad33a83c5a679870033f to your computer and use it in GitHub Desktop.
Tampermonkey: Find driving slot Trafikverket
// ==UserScript==
// @name Find slot
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Book a Förarprov with the place and type (manual/auto) that you want, then go to Boka Prov > Personbil B > Bokade Prov > Press the edit icon for the existing test. Enable the script and reload the page. The script will look for a better time in the same configuration of auto/manual and location.
// @author You
// @match https://fp.trafikverket.se/boka/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=trafikverket.se
// @require https://cdnjs.cloudflare.com/ajax/libs/ion-sound/3.0.7/js/ion.sound.min.js
// @grant none
// ==/UserScript==
(async function() {
'use strict';
var jQuery = window.jQuery;
var ion = window.ion;
log('Set constants');
var dateFrom = '2022-08-22';
var dateTo = '2022-08-30';
var autoConfirm = true;
var acceptOffice = '';
document.body.addEventListener('click', () => {
console.log("Init sound context after click");
// http://ionden.com/a/plugins/ion.sound/en.html
ion.sound({
sounds: [
{name: 'bell_ring'}
],
// main config
path: "https://cdnjs.cloudflare.com/ajax/libs/ion-sound/3.0.7/sounds/",
preload: true,
multiplay: true,
volume: 0.9
});
}, true);
let foundSlots = [];
for(let tries = 0; tries < 10; tries++) {
foundSlots = [...document.querySelectorAll('.col-sm-9 .col-xs-6 strong')].map((slot) => new Date(slot.innerText));
await new Promise(r => setTimeout(r, 2000));
log("Looking for slots...");
if(foundSlots.length > 0) {
log("Found list of slots, trying to find an eligible one");
break;
}
}
const eligibleSlot = foundSlots.find((d) => (d <= new Date(dateTo) && d > new Date(dateFrom)));
if(eligibleSlot) {
console.warn("FOUND ELIGIBLE SLOT", eligibleSlot);
playSound('bell_ring');
alert(`Found slot: ${eligibleSlot}`);
} else {
log("No slots or no eligible found, reloading in a few seconds.");
await new Promise(r => setTimeout(r, 3000));
location.reload();
}
/* Helpers */
function log(log) {
console.log(log);
}
function playSound() {
// play sound
ion.sound.play('bell_ring');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment