Skip to content

Instantly share code, notes, and snippets.

@tsiemens
Last active January 24, 2022 21:05
Show Gist options
  • Select an option

  • Save tsiemens/0760e1712d8ae1e08ef52fb7dee09824 to your computer and use it in GitHub Desktop.

Select an option

Save tsiemens/0760e1712d8ae1e08ef52fb7dee09824 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GVOC Member IDs
// @namespace https://gist.github.com/tsiemens
// @version 0.1
// @description Make dealing with duplicate user accounts easier when entering course results.
// @author Trevor Siemens
// @match https://gvoc.whyjustrun.ca/*
// @grant none
// ==/UserScript==
/**
This Tampermonkey script (Greasemonkey doesn't appear to work correctly)
will insert the user ID of users in the "Add Competitor" dropdown on
whyjustrun event Edit Results page.
This makes it easier to deal with cases where someone has multiple accounts
with their name (occurs semi-frequently), and you want to select the one
they actually use.
Additionally, the number of participated-in events associated with the account
are also listed, since usually the account with the larger number of events is
the one to use.
*/
'use strict';
let mutationObserver = null;
async function getUserPageAsync(id) {
try{
let response = await fetch(`https://whyjustrun.ca/users/${id}`);
// console.log(`resp for ${id}:`, response);
response.text().then(b => {
let m = b.match(/Results and Registrations \(([0-9]+)\)/);
if (m) {
let eventCount = m[1];
console.log(`GM: Results and Registrations for ${id}: ${eventCount}`);
let regCountSpan = document.getElementById(`GM_registration_count_${id}`);
regCountSpan.innerText = `(Event count: ${eventCount})`;
}
// console.log(`body for ${id}:`, b);
});
} catch(err){
console.error(err);
// Handle errors here
}
}
function maybeAddDropdownListener() {
const selectElement = document.querySelectorAll('ul.dropdown-menu li');
selectElement.forEach(li => {
if (!li.GM_modified) {
li.GM_modified = true;
let valueJson = JSON.parse(li.dataset.value);
let innerA = li.children[0];
if (!innerA.innerText.startsWith("Create New")) {
innerA.innerHTML += ` (id: ${valueJson.id}) <span id="GM_registration_count_${valueJson.id}"></span>`;
getUserPageAsync(valueJson.id);
}
}
});
}
function maybeAddUserTester() {
let userLinkerField = document.getElementById('GM_userLinker');
if (userLinkerField) {
return;
}
let competitorAddField = document.getElementById('competitorResults');
console.log(competitorAddField);
let rightSideColDiv = competitorAddField.parentElement.parentElement;
rightSideColDiv.innerHTML += '<fieldset class="form-group"><br><input name="data[GM_userLinker]" placeholder="Competitor ID to view (monkey script)" type="text" id="GM_userLinker" class="form-control"><button id="GM_goButton">Go to profile</button></fieldset>';
let goButton = document.getElementById('GM_goButton').addEventListener('click', () => {
let userLinkerField = document.getElementById('GM_userLinker');
let idInt = Number.parseInt(userLinkerField.value);
if (idInt !== NaN) {
console.log("GM: Go to", userLinkerField.value);
window.open("https://whyjustrun.ca/users/" + idInt);
}
});
}
(function() {
console.log("GM init");
mutationObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
maybeAddDropdownListener();
maybeAddUserTester();
});
});
mutationObserver.observe(document.body, {childList: true, subtree: true});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment