Reddit Enhancement Suite Image Auto Expand

Automatically expands images within a thread when used with the Reddit Enhancement Suite script.

  1. // ==UserScript==
  2. // @name Reddit Enhancement Suite Image Auto Expand
  3. // @namespace RESAutoExpand
  4. // @description Automatically expands images within a thread when used with the Reddit Enhancement Suite script.
  5. // @include http://reddit.com/r/*
  6. // @include http://*.reddit.com/r/*
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
  8. // @grant GM_addStyle
  9. // @version 1.0
  10. // ==/UserScript==
  11. /*- The @grant directive is needed to work around a design change
  12. introduced in GM 1.0. It restores the sandbox.
  13. */
  14.  
  15. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  16. that detects and handles AJAXed content.
  17.  
  18. Usage example:
  19.  
  20. waitForKeyElements (
  21. "div.comments"
  22. , commentCallbackFunction
  23. );
  24.  
  25. //--- Page-specific function to do what we want when the node is found.
  26. function commentCallbackFunction (jNode) {
  27. jNode.text ("This comment changed by waitForKeyElements().");
  28. }
  29.  
  30. IMPORTANT: This function requires your script to have loaded jQuery.
  31. */
  32. function waitForKeyElements (
  33. selectorTxt, /* Required: The jQuery selector string that
  34. specifies the desired element(s).
  35. */
  36. actionFunction, /* Required: The code to run when elements are
  37. found. It is passed a jNode to the matched
  38. element.
  39. */
  40. bWaitOnce, /* Optional: If false, will continue to scan for
  41. new elements even after the first match is
  42. found.
  43. */
  44. iframeSelector /* Optional: If set, identifies the iframe to
  45. search.
  46. */
  47. ) {
  48. var targetNodes, btargetsFound;
  49.  
  50. if (typeof iframeSelector == "undefined")
  51. targetNodes = $(selectorTxt);
  52. else
  53. targetNodes = $(iframeSelector).contents ()
  54. .find (selectorTxt);
  55.  
  56. if (targetNodes && targetNodes.length > 0) {
  57. btargetsFound = true;
  58. /*--- Found target node(s). Go through each and act if they
  59. are new.
  60. */
  61. targetNodes.each ( function () {
  62. var jThis = $(this);
  63. var alreadyFound = jThis.data ('alreadyFound') || false;
  64.  
  65. if (!alreadyFound) {
  66. //--- Call the payload function.
  67. var cancelFound = actionFunction (jThis);
  68. if (cancelFound)
  69. btargetsFound = false;
  70. else
  71. jThis.data ('alreadyFound', true);
  72. }
  73. } );
  74. }
  75. else {
  76. btargetsFound = false;
  77. }
  78.  
  79. //--- Get the timer-control variable for this selector.
  80. var controlObj = waitForKeyElements.controlObj || {};
  81. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  82. var timeControl = controlObj [controlKey];
  83.  
  84. //--- Now set or clear the timer as appropriate.
  85. if (btargetsFound && bWaitOnce && timeControl) {
  86. //--- The only condition where we need to clear the timer.
  87. clearInterval (timeControl);
  88. delete controlObj [controlKey]
  89. }
  90. else {
  91. //--- Set a timer, if needed.
  92. if ( ! timeControl) {
  93. timeControl = setInterval ( function () {
  94. waitForKeyElements ( selectorTxt,
  95. actionFunction,
  96. bWaitOnce,
  97. iframeSelector
  98. );
  99. },
  100. 300
  101. );
  102. controlObj [controlKey] = timeControl;
  103. }
  104. }
  105. waitForKeyElements.controlObj = controlObj;
  106. }
  107.  
  108.  
  109. waitForKeyElements (".toggleImage.expando-button.collapsed.collapsedExpando.image.commentImg", clickSaveButton);
  110.  
  111. function clickSaveButton (jNode) {
  112. triggerMouseEvent (jNode[0], "click");
  113. }
  114.  
  115. function triggerMouseEvent (node, eventType) {
  116. var clickEvent = document.createEvent('MouseEvents');
  117. clickEvent.initEvent (eventType, true, true);
  118. node.dispatchEvent (clickEvent);
  119. }
  120.  
  121.  
  122.