// ==UserScript==
// @name Stig's Art Grabr
// @namespace dk.rockland.userscript.misc.artgrab
// @description Grabbing big high resolution album cover-art from various sites
// @version 2016.06.20.0
// @include /45cat\./
// @include /allmusic\./
// @include /amazon\./
// @include /bandcamp\./
// @include /cdbaby\./
// @include /itunes\.apple\./
// @include /jamendo\./
// @include /last(fm)?\.[a-z]{2,3}/
// @include /magnatune\./
// @include /fnd\.io/
// @include /labs\.stephenou\.com\/itunes/
// @include /soundcloud\./
// @include /play\.google\.com/
// @author Stig Nygaard, http://www.rockland.dk
// @homepageURL http://www.rockland.dk/userscript/misc/artgrab/
// @supportURL http://www.rockland.dk/userscript/misc/artgrab/
// @grant GM_registerMenuCommand
// @noframes
// ==/UserScript==
// Partly based on tips at http://wiki.musicbrainz.org/User:Nikki/CAA
// CHANGELOG - The most important updates/versions:
var changelog = [
{version: '2016.06.20.0', description: '1st official release version.'},
{version: '2016.06.19.1', description: 'Updated/fixed support for last.fm and bandcamp. Musicdinner is gone, but support for fnd.io and labs.stephenou.com/itunes added instead.'},
{version: '2016.06.19.0', description: 'First userscript version (Converted from my old BCA bookmarklet).'}
];
function runGrabr() {
var DEBUG = true;
var log = function(s) {
if (DEBUG && window.console) {
window.console.log(s);
}
};
// [ page pattern, search for img patterns, replace this, with this ]
var a = [[/45cat\./, /-s\.jpg/i, /-s\.jpg/gi, ".jpg"],
[/45cat\./, /-s\.png/i, /-s\.png/gi, ".png"],
[/allmusic\./, /\/JPG_\d{3}\//i, /\/JPG_\d{3}\//gi, "/JPG_1080/"],
[/amazon\./, /\._[A-Z]{2}\d{3}_[\w_,-]*\.jpg/i, /\._[A-Z]{2}\d{3}_[\w_,-]*\.jpg/gi, ".jpg"],
[/amazon\./, /\._[A-Z]{2}\d{3}_[\w_,-]*\.png/i, /\._[A-Z]{2}\d{3}_[\w_,-]*\.png/gi, ".png"],
[/bandcamp\./, /_\d{1,2}\.jpg/i, /_\d{1,2}\.jpg/gi, "_0.jpg"],
[/bandcamp\./, /_\d{1,2}\.png/i, /_\d{1,2}\.png/gi, "_0.png"],
[/cdbaby\./, /images\..*_small\.[jpgn]{3}/i, /_small\./gi, "."],
[/cdbaby\./, /images\..*\.jpg/i, /\.jpg/gi, "_large.jpg"],
[/cdbaby\./, /images\..*\.png/i, /\.png/gi, "_large.png"],
[/itunes\.apple\./, /1\d0x1\d0\./i, /1\d0x1\d0\./gi, "1200x1200."],
[/jamendo\./, /1\.\d00\.jpg/i, /1\.\d00\.jpg/gi, "1.0.jpg"],
[/jamendo\./, /1\.\d00\.png/i, /1\.\d00\.png/gi, "1.0.png"],
[/last(fm)?\.[a-z]{2,3}/, /\.lst\.fm\/i\/u\/[a-zA-Z]*\d{2,}\w*\//i, /\.lst\.fm\/i\/u\/[a-z]*\d{2,}\w*\//gi, ".lst.fm/i/u/"],
[/magnatune\./, /cover_\d{2,3}\./i, /cover_\d{2,3}\./gi, "cover."],
[/musicbrainz\.org/, /_thumb\d{3}\./i, /_thumb\d{3}\./gi, "."],
[/musicbrainz\.org/, /-\d{3}\.jpg/i, /-\d{3}\.jpg/gi, ".jpg"],
[/musicbrainz\.org/, /-\d{3}\.png/i, /-\d{3}\.png/gi, ".png"],
[/fnd\.io/, /\/\d{2,}x\d{2,}bb/i, /\/\d{2,}x\d{2,}bb/gi, "/1200x1200bb"],
[/labs\.stephenou\.com/, /\/\d{2,3}x\d{2,3}bb/i, /\/\d{2,3}x\d{2,3}bb/gi, "/1200x1200bb"],
/* https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/ */
[/soundcloud\./, /t\d\d0x\d\d0\./i, /t\d\d0x\d\d0\./gi, "original."],
[/play\.google\.com/, /googleusercontent\.com.*\=w\d{3}/, /\=w\d{3}$/, "=w1200"]];
var aEv = function (e,ev,f,c) {
c=(c)?c:false;
if(e.addEventListener) {
e.addEventListener(ev,f,c)
} else if(e.attachEvent) {
e.attachEvent("on"+ev,f)
} else {
e["on"+ev]=f
}
};
var w = null, n = 0, m = 20, d = document;
if (d.location.hostname.search(/soundcloud\./) > -1) {
spans = document.querySelectorAll("span[style*=background-image]");
for (i = 0; i < spans.length; i++) {
if (spans[i].style.backgroundImage) {
imgsrc = spans[i].style.backgroundImage.match(/url[\(\"\u0027]+([^\"\u0027\)]*)[\)\"\u0027]+/)[1];
if ((spans[i].getElementsByTagName("img").length == 0) && (imgsrc.match(/t[\d]{3}x[\d]{3}\./) != null)) {
spans[i].innerHTML = "<img src=\u0027" + imgsrc + "\u0027 alt=\u0027\u0027 style=\u0027width:200px;height:200px;border:none\u0027 />";
}
}
}
}
log('Activated while on ' + d.location.hostname);
o:
for (var v = 0; v < a.length; v++) {
if (d.location.hostname.search(a[v][0]) > -1) {
log('Running on ' + d.location.hostname);
w = a[v];
var l = d.getElementsByTagName("img");
if (l) {
log('Found ' + l.length + ' image tags');
for (var i = 0; i < l.length; i++) {
if ((l[i].src).search(w[1]) > -1) {
l[i].style.border = "1px #FB0 solid";
if (l[i].naturalWidth) {
l[i].onmouseover = function () {
this.title = "" + this.naturalWidth + "x" + this.naturalHeight
};
}
aEv(l[i], "load", function () {
if (this.style) {
this.style.borderColor = "#F00";
if (this.naturalWidth && this.naturalWidth > 999) {
this.style.borderWidth = "2px"
}
}
});
aEv(l[i], "click", function () {
if (this.src) {
location = this.src
}
});
l[i].src = l[i].src.replace(w[2], w[3]);
n++;
if (n == m) {
if (confirm("" + n + " images requested. Continue?")) {
m = m + 20;
} else {
break o;
}
}
}
}
}
}
}
if (w == null) {
log('No hits found...');
}
return void(0);
}
//window.runGrabr = runGrabr; // (requires @grant none - but could open for bookmarklet version)
if (GM_registerMenuCommand) {
GM_registerMenuCommand("Search big size cover art", runGrabr, "a");
}