-
-
Save elliotchance/6b9eb6ec83500fe14768c9ae125c10b9 to your computer and use it in GitHub Desktop.
Generate list index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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