Last active
December 18, 2015 00:29
-
-
Save jesus2099/5697130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name mb. RANDOM RG for cats | |
// @version 2013.0605.1111 | |
// @description musicbrainz.org: provides a random release group link from an artist page | |
// @namespace https://gist.github.com/jesus2099/5697130 | |
// @author Tristan DANIEL (PATATE12 aka. jesus2099/shamo) | |
// @licence CC BY-NC-SA 3.0 FR (http://creativecommons.org/licenses/by-nc-sa/3.0/fr/) | |
// @requester ZaphodBeeblebrox | |
// @since 2013.6.3. | |
// @grant none | |
// @match *://*.musicbrainz.org/artist/* | |
// @run-at document-end | |
// ==/UserScript== | |
if(self.location.href.match(/^https?:\/\/([^.]+\.)?musicbrainz\.org\/artist\/.+$/)){/*@matchopera*/(function(){"use strict"; | |
var RE_GUID = "[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"; | |
var MBS = self.location.protocol+"//"+self.location.host; | |
var art = document.querySelector("div.artistheader h1 a[href^='"+MBS+"/artist/']"); | |
var tgt = document.querySelector("div.artistheader p.subheader"); | |
var mbid; | |
if (art && tgt && (mbid = art.getAttribute("href").match(new RegExp("/artist/("+RE_GUID+")$")))) { | |
randomRG(); | |
} | |
function randomRG() { | |
var xhr = new XMLHttpRequest(); | |
xhr.addEventListener("load", function(e) { | |
var artist = JSON.parse(this.responseText); | |
var rg = artist["release-group"][Math.floor(Math.random()*artist["release-group"].length)]; | |
var cont = tgt.appendChild(createTag("span", null, [ | |
" 〜 ", | |
createTag("a", {"a":{"title":"re-randomize among "+artist["release-group"].length+" release groups"},"e":{"click":function(e){ | |
del(getParent(this, "span")); | |
randomRG(); | |
}}}, "Random"), | |
" release-group: ", | |
createTag("a", {"a":{"href":"/release-group/"+rg.id}}, [ | |
createTag("img", {"a":{"src":"//coverartarchive.org/release-group/"+rg.id+"/front-250","alt":"♥"},"s":{"display":"none","vertical-align":"text-top","max-height":self.getComputedStyle(tgt).getPropertyValue("height")},"e":{"load":function(e){del(this.style.setProperty("display","inline"));}}}), | |
" "+rg.title | |
]) | |
])); | |
if (rg.disambiguation.length > 0) { | |
cont.appendChild(document.createTextNode(" ("+rg.disambiguation+")")); | |
} | |
}, false); | |
xhr.open("get", "/ws/2/release-group?query=arid:"+mbid[1]+"&limit=100&fmt=json"); | |
xhr.send(null); | |
} | |
function createTag(tag, gadgets, children) { | |
var t = document.createElement(tag); | |
if(t.tagName) { | |
if (gadgets) { | |
for (var attri in gadgets.a) { if (gadgets.a.hasOwnProperty(attri)) { t.setAttribute(attri, gadgets.a[attri]); } } | |
for (var style in gadgets.s) { if (gadgets.s.hasOwnProperty(style)) { t.style.setProperty(style.replace(/!/,""), gadgets.s[style], style.match(/!/)?"important":""); } } | |
for (var event in gadgets.e) { if (gadgets.e.hasOwnProperty(event)) { t.addEventListener(event, gadgets.e[event], false); } } | |
} | |
if (t.tagName == "A" && !t.getAttribute("href") && !t.style.getPropertyValue("cursor")) { t.style.setProperty("cursor", "pointer"); } | |
if (children) { var chldrn = children; if (typeof chldrn == "string" || chldrn.tagName) { chldrn = [chldrn]; } for(var child=0; child<chldrn.length; child++) { t.appendChild(typeof chldrn[child]=="string"?document.createTextNode(chldrn[child]):chldrn[child]); } t.normalize(); } | |
} | |
return t; | |
} | |
function del(o) { | |
return o.parentNode.removeChild(o); | |
} | |
function getParent(obj, tag, cls) { | |
var cur = obj; | |
if (cur.parentNode) { | |
cur = cur.parentNode; | |
if (cur.tagName.toUpperCase() == tag.toUpperCase() && (!cls || cls && cur.className.match(new RegExp("\\W*"+cls+"\\W*")))) { | |
return cur; | |
} else { | |
return getParent(cur, tag, cls); | |
} | |
} else { | |
return null; | |
} | |
} | |
})();} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment