Display Original Image

This script will replace thumbnail with full size image if available.

  1. // ==UserScript==
  2. // @name Display Original Image
  3. // @version 0.1.2
  4. // @namespace eight04.blogspot.com
  5. // @description This script will replace thumbnail with full size image if available.
  6. // @include http*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. "use strict";
  11.  
  12. function observeDocument(callback) {
  13.  
  14. callback(document.body);
  15.  
  16. new MutationObserver(function(mutations){
  17. var i;
  18. for (i = 0; i < mutations.length; i++) {
  19. if (!mutations[i].addedNodes.length) {
  20. continue;
  21. }
  22. callback(mutations[i].target);
  23. }
  24. }).observe(document.body, {
  25. childList: true,
  26. subtree: true
  27. });
  28. }
  29.  
  30. function displayOriginalImage(node) {
  31. var imgs = node.querySelectorAll("a[href]>img[src]");
  32. var i;
  33.  
  34. for (i = 0; i < imgs.length; i++) {
  35. replace(imgs[i], imgs[i].parentNode);
  36. }
  37. }
  38.  
  39. function handleError() {
  40. this.src = this.oldSrc;
  41. this.removeEventListener(handleError);
  42. this.classList.add("display-original-image-failed");
  43. }
  44.  
  45. function replace(img, anchor) {
  46. if (anchor.classList.contains("display-original-image")) {
  47. return;
  48. }
  49. if (!/\.(jpg|png|gif)($|\?)/i.test(anchor.href)) {
  50. return;
  51. }
  52. img.addEventListener("error", handleError);
  53. img.oldSrc = img.src;
  54. img.src = anchor.href;
  55. anchor.classList.add("display-original-image");
  56. }
  57.  
  58. observeDocument(displayOriginalImage);