// ==UserScript==
// @name [GMT] Artist Chronology
// @version 1.05
// @description Browse artist's releases in chronological order directly from release page
// @match https://*/torrents.php?id=*
// @match https://*/torrents.php?page=*&id=*
// @run-at document-end
// @author Anakunda
// @namespace https://greasyfork.org/users/321857
// @copyright 2024, Anakunda (https://greasyfork.org/users/321857)
// @license GPL-3.0-or-later
// @grant GM_getValue
// @grant GM_setValue
// @require https://openuserjs.org/src/libs/Anakunda/libLocks.min.js
// @require https://openuserjs.org/src/libs/Anakunda/gazelleApiLib.min.js
// ==/UserScript==
'use strict';
const anchor = document.body.querySelector('div#content table#torrent_details');
// const anchor = document.body.querySelector('table#vote_matches')
// || document.body.querySelector('div.box.torrent_description');
if (anchor == null) return;
const searchParams = new URLSearchParams(document.location.search);
const groupId = parseInt(searchParams.get('id'));
console.assert(groupId > 0);
if (!(groupId > 0)) return;
let chronologyArtistId = parseInt(searchParams.get('chronology_id')) || undefined;
let releaseType = document.body.querySelector('div#content div.header > h2');
if (releaseType != null) releaseType = /\s+\[([^\[\]]+)\]$/.exec(releaseType.lastChild.textContent);
if (releaseType != null) releaseType = {
'Album': 1, 'Soundtrack': 3, 'EP': 5, 'Anthology': 6, 'Compilation': 7, 'Single': 9, 'Live album': 11,
'Remix': 13, 'Bootleg': 14, 'Interview': 15, 'Mixtape': 16, 'Demo': 17, 'Concert Recording': 18, 'DJ Mix': 19,
'Unknown': 21,
}[releaseType[1]];
const getArtistIds = className => Array.from(document.body.querySelectorAll(`ul#artist_list > li.${className} > a`),
a => parseInt((a = new URLSearchParams(a.search)).get('id'))).filter(artistId => artistId > 0);
const artistIds = {
DJ: getArtistIds('artists_dj'),
main: getArtistIds('artist_main'),
composer: getArtistIds('artists_composers'),
remixer: getArtistIds('artists_remix'),
producer: getArtistIds('artists_producer'),
};
const minifyHTML = html => html.replace(/\s*(?:\r?\n)+\s*/g, '');
const forward = (scaleX = 1) => minifyHTML(`
<svg height="20px" viewBox="0 0 82.66 82.66" transform="scale(${scaleX},1)" xmlns="http://www.w3.org/2000/svg">
<circle fill="#8888" cx="41" cy="41.33" r="41.04"/>
<polygon fill="white" points="46.27,22.55 59.12,31.94 71.97,41.33 59.12,50.72 46.27,60.12 46.27,47.41 38.39,53.17 22.19,65.02 22.19,41.33 22.19,17.65 38.39,29.49 46.27,35.26 "/>
</svg>
`);
const fastForward = (scaleX = 1) => minifyHTML(`
<svg height="20px" viewBox="0 0 93.33 93.33" transform="scale(${scaleX},1)" xmlns="http://www.w3.org/2000/svg">
<circle fill="#8888" cx="46.67" cy="46.67" r="46.67"/>
<path fill="white" d="M58.69 20.71l12.12 0 0 51.91 -12.12 0 0 -51.91zm-0.31 25.96l-17.93 12.97 -17.93 12.98 0 -25.95 0 -25.96 17.93 12.98 17.93 12.98z"/>
</svg>
`);
const box = Object.assign(document.createElement('div'), {
innerHTML: minifyHTML(`
<div class="head">
<a href="#">↑</a> <strong><a href="#" class="reload" title="Activate/refresh artists data">Artist chronology</a></strong>
<label style="float: right; margin-left: 2rem; user-select: none;">Lock to release type
<input name="releasetype_lock" type="checkbox" style="margin-left: 0.5rem;">
</label>
<a href="#" class="artist_lock" style="float: right; margin-left: 2rem;"></a>
</div>
<div class="body" style="padding: 0;"></div>
`),
className: 'box chronology',
});
const [body, reload, artistLock, lockState] =
['div.body', 'a.reload', 'a.artist_lock','input[type="checkbox"][name="releasetype_lock"]']
.map(Element.prototype.querySelector.bind(box));
console.assert(body != null && artistLock != null && lockState != null);
const acStorage = window[GM_getValue('default_storage', 'session') + 'Storage'];
if (!(acStorage instanceof Storage)) throw 'Invalid storage';
let artistChronology = 'artistChronology' in acStorage ? JSON.parse(acStorage.getItem('artistChronology')) : { };
const cacheLifespan = GM_getValue('cache_lifespan', 30 * 6e4);
function getArtistChronology(artistId) {
if (!(artistId > 0)) throw 'Invalid argument';
const expired = Object.keys(artistChronology).filter(artistId =>
!(artistChronology[artistId].timestamp + cacheLifespan > Date.now()));
if (expired.length > 0) for (let artistId of expired) delete artistChronology[artistId];
if (artistId in artistChronology) return Promise.resolve(artistChronology[artistId]);
return queryAjaxAPI('artist', { id: artistId }).then(function(artist) {
artistChronology[artistId] = {
name: artist.name,
torrentGroups: artist.torrentgroup.map(torrentGroup => ({
id: torrentGroup.groupId,
name: torrentGroup.groupName,
year: torrentGroup.groupYear,
releaseType: torrentGroup.releaseType,
})).sort((a, b) => (a.year || Infinity) - (b.year || Infinity)),
timestamp: Date.now(),
};
acStorage.setItem('artistChronology', JSON.stringify(artistChronology));
return artistChronology[artistId];
});
}
function flatArtistIds(lockedArtistId) {
let _artistIds = Object.values(artistIds);
if (lockedArtistId > 0) _artistIds = _artistIds.map(artistIds =>
artistIds.filter(artistId => artistId == lockedArtistId));
return Array.prototype.concat.apply([ ], _artistIds)
.filter((artistId, index, artistIds) => artistIds.indexOf(artistId) == index);
}
function loadChronology(releaseTypeLocked = true, lockedArtistId) {
while (body.lastChild != null) body.removeChild(body.lastChild);
flatArtistIds(lockedArtistId).forEach(function(artistId) {
const artistRow = document.createElement('div');
artistRow.className = `chronology_artist_${artistId}`;
getArtistChronology(artistId).then(function(artistChronology) {
function chronologyByRole(torrentGroups, role) {
function addNavigation(className, index, icon) {
if (torrentGroups[index]) roleNav.querySelectorAll('div.' + className).forEach(function(div) {
if (div.classList.contains('link')) {
const link = content => `<a href="/torrents.php?${new URLSearchParams({
id: torrentGroups[index].id,
chronology_id: artistId,
}).toString()}">${content}</a>`;
if (!div.classList.contains('icon'))
div.innerHTML = `${link(torrentGroups[index].name)}<br>(${torrentGroups[index].year})`;
else if (icon) {
div.innerHTML = link(icon);
for (let svg of div.getElementsByTagName('svg')) {
svg.style.transition = 'filter 200ms';
svg.onmouseenter = svg.onmouseleave = evt =>
{ evt.currentTarget.style.filter = evt.type == 'mouseenter' ? 'drop-shadow(0 0 3px darkorange)' : 'none' };
}
}
} else {
if (!div.classList.contains('icon'))
div.innerHTML = `<span>${torrentGroups[index].name}</span><br>(${torrentGroups[index].year})`;
else if (icon) div.innerHTML = icon;
}
});
}
torrentGroups = torrentGroups.filter((tg1, index, torrentGroups) =>
torrentGroups.findIndex(tg2 => tg2.id == tg1.id) == index);
const groupIndex = torrentGroups.findIndex(torrentGroup => torrentGroup.id == groupId);
if (groupIndex < 0) return;
const roleNav = Object.assign(document.createElement('div'), {
className: `as_${role}`,
innerHTML: minifyHTML(`
<div style="text-align: center; padding: 5px 10px; background-color: #b0c4de66;">
<a href="/artist.php?id=${artistId}">${artistChronology.name}</a>'s chronology (as ${role}) [${groupIndex + 1}/${torrentGroups.length}]
</div>
<div style="display: flex; flex-flow: row nowrap; padding: 5px 10px; justify-content: space-between; column-gap: 1rem;">
<div class="first icon link"></div>
<div class="previous link" style="text-align: center;"></div>
<div class="previous icon link"></div>
<div class="current" style="text-align: center;"></div>
<div class="next icon link"></div>
<div class="next link" style="text-align: center;"></div>
<div class="last icon link"></div>
</div>
`), });
if (groupIndex > 0) {
addNavigation('first', 0, fastForward(-1));
addNavigation('previous', groupIndex - 1, forward(-1));
}
addNavigation('current', groupIndex);
if (groupIndex < torrentGroups.length - 1) {
addNavigation('next', groupIndex + 1, forward(1));
addNavigation('last', torrentGroups.length - 1, fastForward(1));
}
artistRow.append(roleNav);
}
let torrentGroups = artistChronology.torrentGroups;
if (releaseTypeLocked && releaseType > 0) torrentGroups = torrentGroups.filter(torrentGroup =>
torrentGroup.releaseType >= 1000 || torrentGroup.releaseType == releaseType);
chronologyByRole(torrentGroups.filter(torrentGroup => torrentGroup.releaseType < 1000), 'main');
chronologyByRole(torrentGroups.filter(torrentGroup => torrentGroup.releaseType == 1021), 'producer');
chronologyByRole(torrentGroups.filter(torrentGroup => torrentGroup.releaseType == 1022), 'composer');
chronologyByRole(torrentGroups.filter(torrentGroup => torrentGroup.releaseType == 1023), 'remixer');
}, alert);
body.append(artistRow);
});
}
function _reload(evt) {
artistChronology = { };
loadChronology(lockState.checked, chronologyArtistId);
return false;
}
if (chronologyArtistId > 0) Object.assign(artistLock, {
innerHTML: '<span class="artist_lock_state">Release</span> artist ID',
onclick: function(evt) {
chronologyArtistId = chronologyArtistId > 0 ? undefined : parseInt(searchParams.get('chronology_id'));
loadChronology(lockState.checked, chronologyArtistId);
evt.currentTarget.querySelector('span.artist_lock_state').textContent =
chronologyArtistId > 0 ? 'Release' : 'Relock to';
return false;
},
}); else artistLock.remove();
Object.assign(lockState, {
checked: 'chronologyLock' in sessionStorage ? Boolean(Number(sessionStorage.getItem('chronologyLock'))) : true,
onchange: function(evt) {
sessionStorage.setItem('chronologyLock', Number(evt.currentTarget.checked));
loadChronology(evt.currentTarget.checked, chronologyArtistId);
},
});
if (!chronologyArtistId && (flatArtistIds().length > 5 || !GM_getValue('autoload', true))) {
reload.onclick = function(evt) {
loadChronology(lockState.checked);
evt.currentTarget.onclick = _reload;
return lockState.disabled = false;
};
lockState.disabled = true;
} else {
loadChronology(lockState.checked, chronologyArtistId);
reload.onclick = _reload;
}
anchor.after(box);