Skip to content

Instantly share code, notes, and snippets.

@stikkx
Created May 26, 2022 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stikkx/06a368c1f589d2a7dd0a03fef31afaf3 to your computer and use it in GitHub Desktop.
Save stikkx/06a368c1f589d2a7dd0a03fef31afaf3 to your computer and use it in GitHub Desktop.
Add fastes lap marking and beep on new fastes lap
// ==UserScript==
// @name Leaderboard Wige Solution Painting
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Style your Leaderboard on wige solutions by MSFS
// @author Stikkx
// @match https://livetiming.azurewebsites.net
// @icon https://www.google.com/s2/favicons?domain=tampermonkey.net
// @grant none
// ==/UserScript==
// sessionStorage.clear('fastesLap');
// new fastLap beep on ?
const beeper = true;
// fallback refreshInterval in ms
const refreshInterval = 5000;
let fallbackInterval;
let a=new AudioContext()
function beep(vol, freq, duration){
let v=a.createOscillator()
let u=a.createGain()
v.connect(u)
v.frequency.value=freq
v.type="square"
u.connect(a.destination)
u.gain.value=vol*0.01
v.start(a.currentTime)
v.stop(a.currentTime+duration*0.001)
}
if(beeper){
//beep on startup
beep(100,500,300);
}
(new MutationObserver(check)).observe(document, {childList: true, subtree: true});
function check(changes, observer) {
fallbackInterval(fallbackInterval);
if(document.querySelector('td.tc.tc-sector1Time')) {
let fastesSectorTimes = [];
let sectorCount = 9;
let firstSectorTime = [1337,999999999.0]
for (let i = 0; i < sectorCount; ++i) {
fastesSectorTimes.push(firstSectorTime);
}
// Fallback... sometimes MutationsObserver seems not to fire
fallbackInterval = setInterval(function(){
fastesSectorTimes.forEach(function(fastesSectorTime, i){
let sectorTimes = document.querySelectorAll('td.tc.tc-sector'+(i+1)+'Time');
observer.disconnect();
let currentSectorTime = 0.0;
sectorTimes.forEach(function(sectorTime, index){
if(sectorTime.innerHTML != "")
{
currentSectorTime = sectorTime.innerHTML.split(":");
if(currentSectorTime.length > 1)
{
currentSectorTime = parseFloat(currentSectorTime[0]*60+currentSectorTime[1]);
}else{
currentSectorTime = parseFloat(currentSectorTime[0])
}
if(parseFloat(fastesSectorTime[1]) > currentSectorTime && currentSectorTime != 0)
{
fastesSectorTimes[i] = [index+1,currentSectorTime];
}
// index is count at zero nth-child started at 1
index++;
let sectorTimeCSS = document.querySelector('tr:nth-child('+index+') > td.tc.tc-sector'+(i+1)+'Time');
sectorTimeCSS.style.backgroundColor = "#fff";
}
});
let sectorTimeCSS = document.querySelector('tr:nth-child('+fastesSectorTime[0]+') > td.tc.tc-sector'+(i+1)+'Time');
sectorTimeCSS.style.backgroundColor = '#aaffaa';
});
//Fastes Lap
let fastesLapTime = [1337,999999999.0]
let currentLap = 0.0;
let fastesLaps = document.querySelectorAll('.tc-fastestLap');
fastesLaps.forEach(function(fastesLap, index){
if(fastesLap.innerHTML != "")
{
currentLap = fastesLap.innerHTML.split(":");
if(currentLap.length > 1)
{
currentLap = parseFloat(currentLap[0]*60+currentLap[1]);
}else{
currentLap = parseFloat(currentLap[0])
}
if(parseFloat(fastesLapTime[1]) > currentLap && currentLap != 0)
{
fastesLapTime = [index+1,currentLap];
}}
});
let sectorTimeCSS = document.querySelector('tr:nth-child('+fastesLapTime[0]+') > td.tc.tc-fastestLap');
sectorTimeCSS.style.backgroundColor = '#ff22ff';
if (sessionStorage.getItem('fastesLap')) {
let laptime = sessionStorage.getItem('fastesLap');
if(laptime > fastesLapTime[1]){
// Set new fastesLap
sessionStorage.setItem('fastesLap', fastesLapTime[1]);
if(beeper){
beep(100,400,100);
beep(100,400,100);
beep(100,400,100);
beep(100,400,100);
beep(100,400,100);
}
}
}else{
// Set first fastesLap
sessionStorage.setItem('fastesLap', fastesLapTime[1]);
beep(100,500,300);
}
}, refreshInterval);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment