Skip to content

Instantly share code, notes, and snippets.

@Wind010
Last active December 11, 2024 20:55
Show Gist options
  • Select an option

  • Save Wind010/c30aa873021dd1fd06ba8a39099fdaf7 to your computer and use it in GitHub Desktop.

Select an option

Save Wind010/c30aa873021dd1fd06ba8a39099fdaf7 to your computer and use it in GitHub Desktop.
Just some fun with the Advent of Code Leaderboard UI
function openAllDates() {
const parentElement = document.querySelector('.privboard-days');
const spans = parentElement.querySelectorAll('span');
spans.forEach(span => {
const anchor = document.createElement('a');
const dayText = span.textContent.replace(/\n/g, '');
anchor.href = `/2024/day/${dayText.replace(/<br>/g, '')}`; // Remove <br> for href
anchor.innerHTML = span.innerHTML;
parentElement.replaceChild(anchor, span);
});
}
function giveAllStarsForUser(name) {
const rows = document.querySelectorAll('.privboard-row');
let targetRow = null;
rows.forEach((row, index) => {
const nameSpan = row.querySelector('span.privboard-name');
if (nameSpan && nameSpan.textContent.trim() === name) {
targetRow = row;
return;
}
});
if (!targetRow) {
console.error(`No row found with the string: ${name}`);
return;
}
const existingClasses = ['privboard-star-locked', 'privboard-star-unlocked', 'privboard-star-firstonly'];
const spans = targetRow.querySelectorAll('span');
spans.forEach(span => {
// Remove the old class and add the new class
existingClasses.forEach(className => {
span.classList.remove(className);
});
span.classList.add('privboard-star-both');
});
}
function sameUsername(newName) {
const nameSpans = document.querySelectorAll('span.privboard-name');
nameSpans.forEach(span => {
span.textContent = newName;
});
}
function updateUsername(oldName, newName) {
const nameSpans = document.querySelectorAll('span.privboard-name');
nameSpans.forEach(span => {
if (span.textContent.trim() === oldName) {
span.textContent = newName;
}
});
}
function updateSpanWithSupporterBadge(name) {
const nameSpans = document.querySelectorAll('span.privboard-name');
nameSpans.forEach(span => {
if (span.textContent.trim() === name) {
const supporterBadge = document.createElement('a');
supporterBadge.href = "/2024/support";
supporterBadge.className = "supporter-badge";
supporterBadge.title = "Advent of Code Supporter";
supporterBadge.textContent = " (AoC++)";
const existingLink = span.querySelector('a');
let nameContent;
if (existingLink) {
nameContent = existingLink;
} else {
nameContent = document.createTextNode(name);
}
// Clear the span and append the name content and supporter badge
span.innerHTML = '';
span.appendChild(nameContent);
span.appendChild(supporterBadge);
}
});
}
function giveAllStarsForUser(name) {
const targetRow = Array.from(document.querySelectorAll('.privboard-row')).find(row => {
const nameSpan = row.querySelector('span.privboard-name');
return nameSpan && nameSpan.textContent.trim() === name;
});
if (!targetRow) {
console.error(`No row found with the string: ${name}`);
return;
}
const existingClasses = ['privboard-star-locked', 'privboard-star-unlocked', 'privboard-star-firstonly'];
const spans = targetRow.querySelectorAll('span');
spans.forEach(span => {
if (span.classList.contains('privboard-position')) {
return;
}
existingClasses.forEach(className => {
span.classList.remove(className);
});
span.classList.add('privboard-star-both');
// Handle the span.privboard-name specifically
if (span.classList.contains('privboard-name')) {
const existingLink = span.querySelector('a');
let nameContent;
if (existingLink) {
nameContent = existingLink;
} else {
nameContent = document.createTextNode(span.textContent.trim());
}
span.innerHTML = '';
span.appendChild(nameContent);
}
});
}
function changeScore(oldScore, newScore, username) {
// TODO: Automatically get old score based off username.
const parentDivs = document.querySelectorAll('.privboard-row');
if (parentDivs.length === 0) {
console.error('No elements with class "privboard-row" found.');
return;
}
parentDivs.forEach(parentDiv => {
const nameSpan = parentDiv.querySelector('.privboard-name');
if (!nameSpan) {
console.warn('Element with class "privboard-name" not found within a "privboard-row". Skipping this element.');
return;
}
if (!nameSpan.textContent.includes(username)) {
console.warn(`Element with class "privboard-name" does not include "${username}". Skipping this element.`);
return;
}
// Find the target number next to the privboard-position. Tricky...
const positionSpan = parentDiv.querySelector('.privboard-position');
if (!positionSpan) {
console.warn('Element with class "privboard-position" not found within a "privboard-row". Skipping this element.');
return;
}
const regex = new RegExp(`(<span class="privboard-position">.*?</span>)(\\s*)(${oldScore})(\\s*)`);
parentDiv.innerHTML = parentDiv.innerHTML.replace(regex, `$1$2${newScore}$4`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment