freecog.net

amazonlargeimages.user.js

Also see Install.

   1 // This user script makes product images on Amazon.com as large as possible.
   2 // This is primarily useful for gathering album art for your music collection, 
   3 // but is also effective eye candy.  As a side effect all those tacky "search
   4 // inside this book" tags are eliminated.  If you like, you can apply a custom
   5 // formatting string, as described at http://aaugh.com/imageabuse.html.  Just 
   6 // set the "format" pref for this script in about:config to your format string.
   7 // This might be useful if you have a small monitor and want to limit the size
   8 // of the images (now that would be a bit ironic, wouldn't it ;-p).
   9 //
  10 // This script has been tested on amazon.com, amazon.co.uk, and amazon.co.jp.
  11 
  12 // ==UserScript==
  13 // @name           Amazon Large Images
  14 // @version        2
  15 // @namespace      http://freecog.net/2006/
  16 // @description    Makes product images on Amazon.com as large as possible.
  17 // @include        *amazon.tld/*
  18 // ==/UserScript==
  19 
  20 
  21 /* Changelog:
  22 
  23 Version 2, 27 Feb 2007:
  24 * Fixed a bug that caused certain customer images to not be resized.
  25 * Fixes the issue with the text overlapping a large image.
  26 
  27 Version 1: Initial public release
  28 
  29 */
  30 
  31 const ali_DEBUG = GM_getValue("debug", false);
  32 
  33 // If you want to set a custom format, set this with about:config.  The default
  34 // simply shows the largest image available.  For more on these codes go to 
  35 // http://aaugh.com/imageabuse.html
  36 var ali_format = GM_getValue("format", "_SCLZZZZZZZ_");
  37 
  38 // Function to replace an image URL's format specifier with `ali_format`.
  39 function src_to_large(src) {
  40     var replacement = "." + ali_format + "$1.$2$3";
  41     return src.replace(/\._[^\.]+?_(\.L)?\.(jpe?g|gif|png)("|'|$)/, replacement);
  42 }
  43 
  44 // Find the product image and change it's size.
  45 var img = document.getElementById('prodImage');
  46 if (img) {
  47     img.removeAttribute('width');
  48     img.removeAttribute('height');
  49     img.src = src_to_large(img.src);
  50     if (ali_DEBUG) GM_log("Image fixed.");
  51 } else if (ali_DEBUG) {
  52     GM_log("Product image not found.");
  53 }
  54 
  55 
  56 // A function to eval JS in the document's untrusted scope.
  57 function window_eval(js) {
  58     window.location = "javascript:" + encodeURIComponent(js + "; void null;");
  59 }
  60 
  61 // If there are alternate images, large amounts of shifting can occur 
  62 // when they are switched between.  So we traverse the registeredImages
  63 // object, which contains all of the alternate images and changing it to suit
  64 // our fancy and doing some preloading.  As the images load, we change the 
  65 // size of the container to accomidate the largest among them.
  66 
  67 // Export ali_DEBUG and ali_format
  68 window_eval("ali_DEBUG = " + ali_DEBUG);
  69 window_eval("ali_format = " + uneval(ali_format));
  70 
  71 // Export src_to_large
  72 window_eval("src_to_large = " + uneval(src_to_large));
  73 
  74 // Do it.
  75 window_eval(uneval(function() {
  76     var container = document.getElementById("prodImageCell");
  77     var image_grid = null;
  78     
  79     // Find the image_grid, removing size restrictions
  80     var node = container;
  81     while ((node = node.parentNode)) {
  82         [node.width, node.height] = ['', ''];
  83         [node.style.width, node.style.height] = ['auto', 'auto'];
  84         if (node.className == 'productImageGrid') {
  85             image_grid = node;
  86             break;
  87         }
  88     }
  89     
  90     if (image_grid) { 
  91         image_grid.style.width = '350px'; // Temporary, until images load.
  92         image_grid.style.minWidth = '350px';
  93     }
  94     
  95     var current_width = 0;
  96     var current_height = 0;
  97     var preload_count = 0; // When reaches zero, all images have been preloaded.
  98     if (!registeredImages) return;
  99     for (id in registeredImages) {
 100         preload_count++;
 101         (function(){
 102             var debug_id = id;
 103             var img = registeredImages[id];
 104             // Remove the width and height attributes from the images so that they assume their
 105             // true dimensions upon load.
 106             img.html = src_to_large(img.html).replace(/width="\d*?"/, '').replace(/height="\d*?"/, '');
 107             img.image = src_to_large(img.image);
 108             var preload = img.preload = new Image;
 109             preload.addEventListener('load', function(evt) {
 110                 if (ali_DEBUG) console.log("Image " + debug_id + " preloaded.");
 111                 preload_count--;
 112                 if (!preload_count) allPreloaded = 1;
 113                 function check_sizes() {
 114                     if (ali_DEBUG) console.log("Checking sizes for " + debug_id);
 115                     if (current_width < preload.width) {
 116                         container.width = current_width = preload.width;
 117                         if (image_grid) {
 118                             image_grid.style.width = current_width + 'px';
 119                         }
 120                         if (ali_DEBUG) console.log("  Width: " + current_width);
 121                     }
 122                     if (current_height < preload.height) {
 123                         container.height = current_height = preload.height;
 124                         if (ali_DEBUG) console.log("  Height: " + current_height);
 125                     }
 126                 }
 127                 check_sizes();
 128                 window.setTimeout(check_sizes, 100);
 129             }, false);
 130             preload.src = img.image; // Load it.
 131         })();
 132     }
 133     if (ali_DEBUG) console.log("Done traversing registeredImages.  Container: %o", container);
 134     
 135     //if (image_grid) {
 136         //image_grid.style.cssFloat = 'left';
 137     //}
 138 }) + "();");