Bandcamp Current Title

Shows currently playing track in the page/window/tab title

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Bandcamp Current Title
// @version     1.3
// @author      raina
// @namespace   raina
// @description Shows currently playing track in the page/window/tab title
// @license     GPLv3
// @include     /^https?:\/\//
// @grant       none
// ==/UserScript==

window.self === window.top && window.siteroot && "https://bandcamp.com" == window.siteroot && (function() {

	const title = document.head.querySelector(`title`);
	const album = document.body.querySelector(`#name-section h2.trackTitle`).textContent.trim();
	const albumArtist = document.body.querySelector(`#name-section h3 span a`).textContent.trim();
	const currentTrack = document.body.querySelector('.inline_player .title');
	const year = document.body.querySelector(`.tralbum-credits`).textContent.match(/(?<=released.*)(\d{4})(?=\n)/)[0];
	let artist, track;

	let llamaTime;
	const llamaScroll = () => {
		observer.disconnect();
		title.textContent = title.textContent.slice(0,3) + title.textContent.slice(4) + title.textContent.slice(3,4);
		observer.observe(title, {childList:true});
	}

	const observer = new MutationObserver((mutationList, observer) => {

		let titleString = currentTrack.textContent;

		observer.disconnect();

		if (mutationList[0].addedNodes[0].data.includes("▶︎")) {
			if (track = titleString.split(" - ")[1]) {
				artist = titleString[0];
			} else {
				artist = albumArtist;
				track = currentTrack.textContent.trim();
			}
			title.textContent = `▶︎ ${album}, ${year})   ${artist} – ${track} (`.replace(/ /g, "\xa0");
			llamaTime = setInterval(llamaScroll, 250);
		} else {
			clearInterval(llamaTime);
			title.textContent = title.dataset.originalTitle;
		}

		observer.observe(title, {childList:true});

	});

	title.dataset.originalTitle = title.textContent.trim();
	observer.observe(title, {childList:true});

}());