Skip to content

Instantly share code, notes, and snippets.

@elliotchance
Last active March 21, 2023 04:11
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 elliotchance/6b9eb6ec83500fe14768c9ae125c10b9 to your computer and use it in GitHub Desktop.
Save elliotchance/6b9eb6ec83500fe14768c9ae125c10b9 to your computer and use it in GitHub Desktop.
Generate list index
// separator is what to put between each link. Examples:
// - New line: '\n'
// - Comma: ', '
// - Pipe: ' | '
const separator = '\n';
// The link for the URL. It must contain a slash at the end.
// You cannot use the [List123] links.
const listURL = 'https://rateyourmusic.com/list/echance/a-state-of-trance-1/';
// format controls the text of the link itself. Examples:
// - Title or artist name: $title
// - Release name: $subtitle
// - Page number: $page
// - Smallest/largest item numbers on page: $min or $max
// - Item contents: $body
// - Total number of pages: $pages
const format = '$title - $subtitle ($page)';
// You will need to set this to the page size of the list so the offsets are
// correct.
const pageSize = 50;
// Set to true if the list is reversed. This affects $min and $max but also puts
// the final result in reverse order to match the list.
const reversed = true;
const chunk = (array, chunkSize) => {
let result = [];
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}
return result;
}
const items = Array.from(document.getElementsByClassName('box')).map(x => x.innerText.trim().split('\n'));
const pages = reversed ? chunk(items.reverse(), pageSize) : chunk(items, pageSize);
let result = pages.map((x, idx) => `[${listURL}${idx+1}/,${format
.replace(/\$title/g, x[0][0])
.replace(/\$subtitle/g, x[0][1])
.replace(/\$pages/g, pages.length)
.replace(/\$page/g, idx+1)
.replace(/\$min/g, reversed ? Math.max(items.length-(pages.length-idx)*pageSize+1, 1) : (idx*pageSize)+1)
.replace(/\$max/g, reversed ? items.length-(pages.length-idx-1)*pageSize : (idx+1)*pageSize)
.replace(/\$body/g, x[0][2])
}]`);
result.join(separator);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment