Skip to content

Instantly share code, notes, and snippets.

@youpy
Created December 21, 2009 21:29
Show Gist options
  • Select an option

  • Save youpy/261262 to your computer and use it in GitHub Desktop.

Select an option

Save youpy/261262 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Discogs.com: show thumbnail
// @namespace http://d.hatena.ne.jp/youpy/
// @include http://www.discogs.com/*
// @require http://gist.github.com/3242.txt
// @require http://github.com/cho45/jsdeferred/raw/master/jsdeferred.userscript.js
// ==/UserScript==
// API usage is limited to 5,000 requests per 24-hour period, per IP address.
with(D()) {
(function() {
var Storage = function() {
this.apiRoot = 'http://globalstorage.jgate.de/';
}
Storage.prototype.get = function(key) {
var url = this.apiRoot + 'get?key=' + encodeURIComponent(key);
return xhttp.get(url).
next(function(res) {
var json = eval('(' + res.responseText + ')');
return json.value;
});
};
Storage.prototype.set = function(key, value) {
var url = this.apiRoot + 'set?key=' + encodeURIComponent(key) + '&value=' + encodeURIComponent(value);
return xhttp.get(url).
next(function() {
return value;
});
};
var API_KEY = 'be37d50363';
var NOT_AVAILABLE_IMAGE_URL = 'http://buycheapviagraonlinenow.com/images/image_not_available.jpg';
var LOADING_IMAGE_URL = 'http://buycheapviagraonlinenow.com/images/loading.gif';
var KEY_PREFIX = 'discogs_com_thumbnail_url_';
var storage = new Storage();
$X('//tr[starts-with(@class, "r_tr")]').forEach(function(tr) {
var matches, releaseId;
if(!(matches = tr.className.match(/r(\d+)/)))
return;
releaseId = matches[1];
var image = new Image();
image.src = LOADING_IMAGE_URL;
image.style.marginRight = '5px';
tr.childNodes[3].insertBefore(image, tr.childNodes[3].childNodes[0]);
getThumbnail(releaseId).
next(function(imageUrl) {
if(imageUrl) {
image.src = imageUrl;
} else {
image.src = NOT_AVAILABLE_IMAGE_URL;
image.style.padding = '49px';
image.style.border = '1px solid #ccc';
}
});
});
function getThumbnail(releaseId) {
var url = 'http://www.discogs.com/release/' + releaseId + '?f=xml&api_key=' + API_KEY;
return storage.
get(KEY_PREFIX + releaseId).
next(function(value) {
if(value) {
return next(function() {
return value;
});
} else {
return xhttp.get(url).
next(function(res) {
var xml = new XML(res.responseText);
if(xml..image.length() != 0) {
return storage.set(KEY_PREFIX + releaseId, xml..image[0].@uri150);
} else {
return next(function() { return null; });
}
});
}
});
};
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment