Skip to content

Instantly share code, notes, and snippets.

@tunalad
Last active January 26, 2024 19:59
Show Gist options
  • Save tunalad/0fe0bcf675207f29a50109689bf5470c to your computer and use it in GitHub Desktop.
Save tunalad/0fe0bcf675207f29a50109689bf5470c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Bandcamp Album Length
// @version 1.1
// @description adds text below the tracklist with the album's total length
// @author tunalad
// @match *://*.bandcamp.com/album/*
// @exclude *://bandcamp.com/
// @grant none
// ==/UserScript==
(function() {
'use strict';
const trackTable = document.getElementById('track_table');
if (trackTable) {
// Create a new table row for the new element
const newRow = document.createElement('tr');
newRow.className = 'track_row_view linked';
// Create a new table cell for the new element
const newCell = document.createElement('td');
newCell.colSpan = '5'; // Adjust the colspan based on the number of columns in your table
// Create the new element
const newElement = document.createElement('div');
newElement.className = 'custom-element';
// Calculate the total album length
const timeElements = document.querySelectorAll('#track_table .time.secondaryText');
let totalSeconds = 0;
timeElements.forEach((timeElement) => {
const timeParts = timeElement.textContent.trim().split(':');
if (timeParts.length === 2) {
const minutes = parseInt(timeParts[0], 10);
const seconds = parseInt(timeParts[1], 10);
totalSeconds += minutes * 60 + seconds;
}
});
// Convert totalSeconds back to minutes:seconds format
const totalMinutes = Math.floor(totalSeconds / 60);
const remainingSeconds = totalSeconds % 60;
// Check if the total duration exceeds one hour
const isOverOneHour = totalMinutes >= 60;
// Format the time accordingly
const formattedTime = isOverOneHour ?
`${Math.floor(totalMinutes / 60)}:${totalMinutes % 60}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}` :
`${totalMinutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
// Customize the content based on your needs
newElement.textContent = `Album length: ${formattedTime}`;
// Append the new element to its cell
newCell.appendChild(newElement);
// Append the cell to the row
newRow.appendChild(newCell);
// Append the new row to the table's tbody
const tbody = trackTable.querySelector('tbody');
//tbody.insertBefore(newRow, tbody.firstChild); // Adjust the insertion point based on your layout
tbody.appendChild(newRow); // Append to the bottom of the tracklist
}
})();
// ==UserScript==
// @name Bandcamp Album Length
// @version 1.1
// @description adds text below the tracklist with the album's total length
// @author tunalad
// @match *://*.bandcamp.com/album/*
// @exclude *://bandcamp.com/
// @grant none
// ==/UserScript==
(function($) {
'use strict';
$(document).ready(function() {
const trackTable = $('#track_table');
if (trackTable.length) {
const newRow = $('<tr class="track_row_view linked"></tr>');
const newCell = $('<td colspan="5"></td>');
const newElement = $('<div class="custom-element"></div>');
let totalSeconds = 0;
$('#track_table .time.secondaryText').each(function() {
const timeParts = $(this).text().trim().split(':');
if (timeParts.length === 2) {
const minutes = parseInt(timeParts[0], 10);
const seconds = parseInt(timeParts[1], 10);
totalSeconds += minutes * 60 + seconds;
}
});
const totalMinutes = Math.floor(totalSeconds / 60);
const remainingSeconds = totalSeconds % 60;
// Check if the total duration exceeds one hour
const isOverOneHour = totalMinutes >= 60;
// Format the time accordingly
const formattedTime = isOverOneHour ?
`${Math.floor(totalMinutes / 60)}:${totalMinutes % 60}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}` :
`${totalMinutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
newElement.text(`Album length: ${formattedTime}`);
newCell.append(newElement);
newRow.append(newCell);
const tbody = trackTable.find('tbody');
tbody.append(newRow); // Append to the bottom of the tracklist
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment