Skip to content

Instantly share code, notes, and snippets.

@lowlyocean
Last active April 18, 2020 23:21
Show Gist options
  • Select an option

  • Save lowlyocean/b7018eb0a964a62a370c05fb0bc30496 to your computer and use it in GitHub Desktop.

Select an option

Save lowlyocean/b7018eb0a964a62a370c05fb0bc30496 to your computer and use it in GitHub Desktop.
iRacing Pro Driver Highlighter Tampermonkey userscript
// ==UserScript==
// @name iRacing Pro Driver Highlighter
// @require https://code.jquery.com/ui/1.7.3/jquery-ui.min.js
// @description Highlights the rows of entries in the iRacing event results table that are pro drivers
// @include *://members.iracing.com/membersite/member/EventResult.do*
// @grant GM_xmlhttpRequest
// @connect www.driverdb.com
// @author lowlyocean (based on fuzzwah's similar script)
// @license MIT
// ==/UserScript==
// the script overwrites the addExportButton function (which is called at the end of the populateResults function
// set up some variables to handle team race results
var teamRace = false;
var prevTeam = null;
var proColor = "#7CFC00";
var cache = {};
var subscribedRows = {};
function highlightRow(elmRow, highlightColor="#FFF3B3") {
var getContrast = function (hexcolor){
// If a leading # is provided, remove it
if (hexcolor.slice(0, 1) === '#') {
hexcolor = hexcolor.slice(1);
}
// If a three-character hexcode, make six-character
if (hexcolor.length === 3) {
hexcolor = hexcolor.split('').map(function (hex) {
return hex + hex;
}).join('');
}
// Convert to RGB value
var r = parseInt(hexcolor.substr(0,2),16);
var g = parseInt(hexcolor.substr(2,2),16);
var b = parseInt(hexcolor.substr(4,2),16);
// Get YIQ ratio
var yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
// Check contrast
return (yiq >= 128) ? '#333' : '#eee';
};
var j;
// set the color
elmRow.style.background = highlightColor;
for (j = 0; j < elmRow.cells.length; j++) {
elmRow.cells[j].style.color = getContrast(highlightColor);
var link = elmRow.cells[j].getElementsByClassName("stats_table_link");
if (link.length > 0) {
link[0].style.color = getContrast(highlightColor);
}
}
// if this was a team race....
if (teamRace == true) {
var k;
// also highlight the last team row we saw
prevTeam.style.background = highlightColor;
for (k = 0; k < prevTeam.cells.length; k++) {
prevTeam.cells[k].style.color = getContrast(highlightColor);
}
}
}
function driverdbCallback(driverName) {
for( var elmRow of subscribedRows[driverName] ) {
highlightRow(elmRow, proColor);
}
}
function queryProDriverStatus(driverName, elmRow) {
if( !(driverName in subscribedRows )) {
subscribedRows[driverName] = [];
}
subscribedRows[driverName].push(elmRow);
if(! (driverName in cache )) {
cache[driverName] = undefined;
GM_xmlhttpRequest({
method: "GET",
url: "https://www.driverdb.com/autocomp/?term="+driverName,
responseType: 'json',
onload: function(response) {
if(response.readyState == 4 && response.status == 200) {
if( response.response ) {
driverdbCallback(driverName);
}
}
}
});
}
};
function addExportButton(parent, ssId, ssNum) {
// so first we do the things that addExportButton normally does...
var csv_div = parent.appendChild(element("div", {}, {
position: "absolute",
top: "1px",
right: "21px"
}));
var csvimg_link = csv_div.appendChild(element("a", {
href: contextpath + "/member/GetEventResultsAsCSV?subsessionid=" + ssId + "&simsesnum=" + ssNum + "&includeSummary=1",
className: "outputcsv_label"
}, {
display: "block",
position: "absolute",
top: "5px",
right: "5px"
}));
imgpreload(imageserver + "/member_images/results/outputcsv.gif", csvimg_link, "outputcsv_label");
// and then we do the things needed to highlight the driver's row in the tables:
// spin through each row in the table
var custid = location.search.split('custid=').splice(1).join('').split('&')[0];
var trs = document.getElementsByTagName('tr');
for (var i = 0; i < trs.length; i++) {
var elmRow = trs[i];
// if any of these rows have the class "team_parent_row", results are from a team race
if( elmRow.id.indexOf(custid) !== -1) {
document.highlightRow(elmRow);
}
if(elmRow.classList.contains("team_parent_row")) {
teamRace = true;
// hold onto the last team we saw
prevTeam = elmRow;
}
else if(elmRow.id.indexOf("race_row") !== -1) {
var driverName = elmRow.getElementsByTagName("a")[0].innerText.replace(/[^A-Za-zÀ-ž ]/g, "");
document.queryProDriverStatus(driverName, elmRow);
}
}
}
// the function below injects our updated addExportButton function into the page
addJS_Node(addExportButton);
function addJS_Node(text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement('script');
D.queryProDriverStatus = queryProDriverStatus;
D.highlightRow = highlightRow;
if (runOnLoad) {
scriptNode.addEventListener("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
targ.appendChild(scriptNode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment