User:Lectrician1/musica.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*

	This script shows cover art of music albums next to the name of the item at the top of a page.
	
	It is retreived from MusicBrainz release group and release statements.
	
	To use it, add the following line to your common.js:
	mw.loader.load("//www.wikidata.org/w/index.php?title=User:Lectrician1/musica.js&action=raw&ctype=text/javascript");
	
*/

(function () {
	"use strict";
	"esversion: 9";


	// Get cover art from MusicBrainz and display it above the heading of the item
	function getCoverArt(entity) {

		// Release group
		if ("P436" in entity.claims) {
			
			$("#firstHeading")
				.replaceWith(`
					<div id="firstHeading" class="firstHeading" style="display: flex;align-items: center;">
						<img src="https://coverartarchive.org/release-group/${entity.claims.P436[0].mainsnak.datavalue.value}/front-250" width="150" style="padding: 0px 20px;">
						<div style="width: 100%;">
							<h1 class="wikibase-title">
								${$(".wikibase-title-label").prop('outerHTML')}
								${$(".wikibase-title-id").prop('outerHTML')}
							</h1>
							<div class="vector-body">
								${$(".wikibase-entitytermsview-heading-description").prop('outerHTML')}
								${$(".wikibase-entitytermsview-heading-aliases ").prop('outerHTML')}
							</div>
						</div
					</div>
				`);
		
			$(".wikibase-entitytermsview-heading-description:eq(1), .wikibase-entitytermsview-heading-aliases:eq(1)").remove();
			
		}

		// Release
		else if ("P5813" in entity.claims) {
			
			$("#firstHeading")
				.replaceWith(`
					<div id="firstHeading" class="firstHeading" style="display: flex;align-items: center;">
						<img src="https://coverartarchive.org/release/${entity.claims.P5813[0].mainsnak.datavalue.value}/front-250" width="150" style="padding: 0px 20px;">
						<div style="width: 100%;">
							<h1 class="wikibase-title">
								${$(".wikibase-title-label").prop('outerHTML')}
							</h1>
							<div class="vector-body mw-body-content mw-content-ltr wikibase-entityview wb-item wikibase-entityview-main ui-droppable wikibase-entitytermsview wikibase-toolbar-item wikibase-entitytermsview-heading">
								${$(".wikibase-entitytermsview-heading-description").prop('outerHTML')}
								${$(".wikibase-entitytermsview-heading-aliases ").prop('outerHTML')}
							</div>
						</div
					</div>
				`);
		
			$(".wikibase-entitytermsview-heading-description:eq(1), .wikibase-entitytermsview-heading-aliases:eq(1)").remove();
			
		}

	}

	// When the page has rendered
	mw.hook("wikibase.entityPage.entityView.rendered").add(function () {
		
		// When the entity has rendered
		mw.hook("wikibase.entityPage.entityLoaded").add(function (entity) {

			getCoverArt(entity);
			
		});
		
	});

})();