Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. // ==UserScript==
  2. // @name SEImageExpander
  3. // @namespace http://vulpin.com/
  4. // @description Let people click on images to view the full size
  5. // @include http*://*.stackexchange.com/*
  6. // @include http*://*.superuser.com/*
  7. // @include http*://superuser.com/*
  8. // @include http*://*.serverfault.com/*
  9. // @include http*://serverfault.com/*
  10. // @include http*://*.stackoverflow.com/*
  11. // @include http*://stackoverflow.com/*
  12. // @version 1.0.1
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. var Program = {
  18. main: function() {
  19. window.removeEventListener('load', mainEventListener, false);
  20.  
  21. var observer = new MutationObserver(function(mutations, observer) {
  22. // y'know what? MutationObserver sucks.
  23. // it doesn't give every modified node, just the top of a tree of modified nodes
  24. mutations.forEach(function(mutation) {
  25. Array.prototype.forEach.call(mutation.addedNodes, Program.processImgDescendants);
  26. });
  27. });
  28.  
  29. observer.observe(document, {
  30. subtree: true,
  31. childList: true
  32. });
  33.  
  34. Program.processImgDescendants(document);
  35. },
  36.  
  37. processImgDescendants: function(node) {
  38. var imgNodes = node.querySelectorAll('.post-text img');
  39.  
  40. Array.prototype.forEach.call(imgNodes, function(imgNode) {
  41. Program.processNode(imgNode);
  42. });
  43. },
  44.  
  45. processNode: function(node) {
  46. if (node.tagName.toUpperCase() !== 'IMG') {
  47. return;
  48. }
  49.  
  50. // walk up the tree until we reach the main post div, we don't care what's beyond that
  51. var parent = node;
  52. while (!parent.classList.contains('post-text')) {
  53. // if this image is already within a link, don't touch it
  54. if (parent.tagName.toUpperCase() === 'A') {
  55. return;
  56. }
  57.  
  58. parent = parent.parentNode;
  59.  
  60. // have we made it all the way to the top? we're not in a post!
  61. if (parent === null) {
  62. return;
  63. }
  64. }
  65.  
  66. // ok, now we know the node is an unlinked image within a post
  67. var link = document.createElement('a');
  68. link.href = node.src;
  69. link.target = '_blank';
  70. node.parentNode.replaceChild(link, node);
  71. link.appendChild(node);
  72. }
  73. };
  74.  
  75. var mainEventListener = Program.main.bind(Program);
  76.  
  77. window.addEventListener('load', mainEventListener, false);
  78. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement