Greasy Fork

Greasy Fork is available in English.

MB Auto Track Lengths

Auto sets track lengths for media by unique attached disc id.

当前为 2024-05-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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    http://greasyfork.icu/users/321857
// @copyright    2024, Anakunda (http://greasyfork.icu/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
// ==/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) return;
	let unsetDiscId = section.filter(Boolean);
	if (unsetDiscId.length == 1) unsetDiscId = unsetDiscId[0]; else return;
	const postData = new URLSearchParams({ 'confirm.edit_note': '' });
	//postData.set('make_votable', 1);
	localXHR(unsetDiscId.href, { responseType: null }, postData).then(function(statusCode) {
		unsetDiscId.replaceWith(Object.assign(document.createElement('span'), {
			textContent: 'Track lengths successfully set',
			style: 'color: green;',
			title: 'Status code: ' + statusCode,
		}));
	}, function(reason) {
		unsetDiscId.replaceWith(Object.assign(document.createElement('span'), {
			textContent: 'Error setting track lengths',
			style: 'color: red;',
			title: reason,
		}));
	});
});