// ==UserScript== // @name MB Auto Track Lengths // @version 1.00 // @match https://musicbrainz.org/release/*/discids // @match https://beta.musicbrainz.org/release/*/discids // @match https://musicbrainz.org/cdtoc/* // @match https://beta.musicbrainz.org/cdtoc/* // @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 // @description Auto sets track lengths for media by unique attached disc id. // @require https://openuserjs.org/src/libs/Anakunda/xhrLib.js // @downloadURL none // ==/UserScript== 'use strict'; function getTrackLengths(tr) { const setTrackLengths = Array.prototype.find.call(tr.querySelectorAll('td a'), a => a.textContent.trim() == 'Set track lengths'); return setTrackLengths || null; } const isDiscIdRow = tr => ['odd', 'even'].some(cls => tr.classList.contains(cls)); const sections = document.location.pathname.startsWith('/release/') ? Array.from(document.body.querySelectorAll('table.tbl > tbody > tr.subh'), function(subh) { const discIds = [ ]; for (let tr = subh.nextElementSibling; tr != null && isDiscIdRow(tr); tr = tr.nextElementSibling) discIds.push(getTrackLengths(tr)); return discIds; }) : document.location.pathname.startsWith('/cdtoc/') ? Array.prototype.filter.call(document.body.querySelectorAll('table.tbl > tbody > tr'), isDiscIdRow) .map(getTrackLengths) : null; if (sections != null) sections.forEach(function(section) { if (section.length == 1) section = section.filter(Boolean); else return; if (section.length == 1) section = section[0]; else return; const postData = new URLSearchParams({ 'confirm.edit_note': '' }); //postData.set('confirm.make_votable', 1); localXHR(section.href, { responseType: null }, postData).then(function(statusCode) { section.replaceWith(Object.assign(document.createElement('span'), { textContent: 'Track lengths successfully set', style: 'color: green;', title: 'Status code: ' + statusCode, })); }, function(reason) { section.replaceWith(Object.assign(document.createElement('span'), { textContent: 'Error setting track lengths', style: 'color: red;', title: reason, })); }); });