Skip to content

Instantly share code, notes, and snippets.

@Kefta
Created February 21, 2020 02:44
Show Gist options
  • Save Kefta/b4aa33de8ca90532254de51ef68e26c2 to your computer and use it in GitHub Desktop.
Save Kefta/b4aa33de8ca90532254de51ef68e26c2 to your computer and use it in GitHub Desktop.
RateYourMusic Collection Buttons
// ==UserScript==
// @name RateYourMusic Collection Buttons
// @include *://rateyourmusic.com/collection/*
// @grant metadata
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// ==/UserScript==
const tReleaseTypes = [["typs", "albums"],
["type", "eps"],
["typi", "singles"],
["typm", "mixtapes"],
["typj", "dj mixes"],
["typc", "compilations"],
["typd", "videos"],
["typb", "bootlegs"],
["", "everything"]];
if (tReleaseTypes.length == 0 || document.getElementById('breadcrumb') === null) return;
const sURL = window.location.toString(); // This will probably be changed later, but is temporary storage for the whole URL
var tURL = sURL.split('/');
var sPrefix, sSuffix;
// "collection" must be at least the second-to-last element with the last being the username
// Our target is the element afterwards, the collection modifiers, which might or might not exist
var u = tURL.length - 1;
// If there are no current collection modifiers
if (tURL[u] === "collection")
{
// Prefix is already set, but make sure the last character is a slash
if (sPrefix[sPrefix.length] === '/')
{
sPrefix = sURL.trimEnd();
}
else
{
sPrefix = sURL.trimEnd() + '/';
}
sSuffix = "";
}
// if "collection" appears before the second-to-last element
else
{
// FIXME: Clear multiple type modifiers in the URL
while (--u !== 0)
{
if (tURL[u] !== "collection") continue;
const sModifiers = tURL[u + 2];
// If the modifiers are the last element, don't add an extra slash
sSuffix = (u + 2 === tURL.length) ? "" : ('/' + tURL.slice(u + 3).join('/'));
// Append everything before the modifiers
tURL.length = u + 2;
sPrefix = tURL.join('/') + '/';
// If it's an empty string, the prefix and suffix can just be appended to the type modifier
if (sModifiers !== "")
{
// Test if the only modifier is a type
if (sModifiers.length === 4)
{
// If there is no type modifier, append to the end of the current properties
if (sModifiers.match(/typ\w/) === null)
{
sPrefix += sModifiers + ',';
}
// If there is a type modifier, it can be simply left off and the new one will be appended
}
else
{
const tMatch = sModifiers.match(/(^|,)typ\w($|,)/);
// If there is no type modifier, add it to the end of the current modifiers
if (tMatch === null)
{
sPrefix += sModifiers + ',';
}
// If the type modifier is at the beginning, add the rest of the modifiers to the suffix
else if (tMatch.index === 0)
{
sSuffix = sModifiers.substring(4) + sSuffix;
}
// If the type modifier is at the end, add the rest of the modifiers to the prefix
else if (tMatch.index === tURL.length - 5)
{
// Include the comma
sPrefix += sModifiers.substring(0, tURL.length - 4);
}
// Add everything surrounding the type modifier to the prefix and suffix
else
{
// Include the comma on both ends
sPrefix += sModifiers.substring(0, tMatch.index + 1);
sSuffix = sModifiers.substring(tMatch.index + 5) + sSuffix;
}
}
}
break;
}
// Not on a collection page?
if (u === 0) return;
}
// Iterate in reverse as buttons are appended from the head
u = tReleaseTypes.length;
do
{
const tReleaseType = tReleaseTypes[--u];
$('#breadcrumb').after("<a id=\""
+ tReleaseType[1] + "CollectionBtn\" href=\"javascript:void(0);\" class=\"printbutton\" title=\"limit to "
+ tReleaseType[1] + " only\">" + tReleaseType[1] + "</a>");
$("#" + tReleaseType[1] + "CollectionBtn").attr("href",
// Edge-case: if we're appending nothing and our suffix is only a slash
// don't add it to prevent double slashes
tReleaseType[0] === "" && sSuffix.trimEnd() == '/' ? sPrefix
: (sPrefix + tReleaseType[0] + sSuffix));
} while (u !== 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment