NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name Amazon Alt Images
// @namespace spoonium@gmx.net
// @description Show Amazon's alternative images, even if the scripts from images-amazon.com are blocked
// @include /^https?://www\.amazon\..*$/
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @version 0.1
// @grant none
// ==/UserScript==
(function($) {
console.info("Amazon Alt Images Loading.");
var landingImage = $("#landingImage")
, altImages = $("#altImages")
, thumbs = altImages.find("img").map(function(i, ele) {
return $(ele).attr("src");
})
, scripts = $(document).find("script")
, regexThumbs = new RegExp(thumbs[0], "gi")
, regexData = new RegExp(/var\s+data\s*=\s*({[\s\S]+});/, "mgi")
, regexThumb = new RegExp(/(.*\/I\/.*)\._S\w+\d{2},\d{2}_\.jpg$/, "gi")
, dataResult = []
, dataStr = ""
, dataObj = {}
, imagesRaw = []
, images = {}
;
// Go through all the scripts and search for the thumbnails
scripts.each(function() {
// Check if the script source code contains the thumbnails
if ( this.innerHTML == "" || !regexThumbs.test(this.innerHTML) ) {
return true;
}
// console.log(this);
// console.log(this.innerHTML);
// console.log(this.innerHTML.match(regexData));
// console.log(regexData.exec(this.innerHTML));
dataResult = regexData.exec(this.innerHTML);
// console.log("dataResult: ", dataResult);
dataStr = dataResult[1] || "{}";
// console.log("dataStr1: ", dataStr);
// Replace invalid characters
dataStr = dataStr.replace(/'/g, '"');
// console.log("dataStr2: ", dataStr);
dataObj = $.parseJSON(dataStr);
// console.log("JSON: ", dataObj);
imagesRaw = dataObj.colorImages.initial;
return false;
});
// Build the correct images array
$.each(thumbs, function(i, thumb) {
$.each(imagesRaw, function(k, obj) {
if ( obj.thumb == thumb ) {
if ( typeof(obj.large) == "undefined" || !obj.large ) {
obj.large = thumb.replace(regexThumb, "$1.jpg");
}
images[i] = obj;
return false;
}
});
});
// console.log("thumbs: ", thumbs);
// console.log("images: ", images);
altImages.on("mouseover click", function(event) {
var li, index, newSrc, thumbSrc;
event.preventDefault();
li = $(event.target).closest("li");
index = li.index();
// Must be clicked on an LI
if ( index > -1 ) {
// Hires image
if ( typeof(images[index]) != "undefined" && typeof(images[index].hiRes) != "undefined" && images[index].hiRes ) {
newSrc = images[index].hiRes;
}
// Large image
else if ( typeof(images[index]) != "undefined" && typeof(images[index].large) != "undefined" && images[index].large ) {
newSrc = images[index].large;
}
landingImage.attr("src", newSrc);
altImages.find(".a-button-selected").removeClass("a-button-selected");
li.find(".a-button").addClass("a-button-selected");
}
});
// Replace the original image
if ( typeof(images[0]) != "undefined" && typeof(images[0].hiRes) != "undefined" && images[0].hiRes ) {
landingImage.attr("src", images[0].hiRes);
}
// Large image
else if ( typeof(images[0]) != "undefined" && typeof(images[0].large) != "undefined" && images[0].large ) {
landingImage.attr("src", images[0].large);
}
// Reset the landing image CSS
landingImage[0].style.setProperty("width", "auto", "important");
landingImage[0].style.setProperty("height", "auto", "important");
console.info("Amazon Alt Images Loaded.");
})(jQuery);