Modify RES Image Title

Sets the title attribute on the RES Image in a Reddit thread to match the document title. This is done so extensions like Pinterest can capture the image and use the document's title as the description.

  1. // ==UserScript==
  2. // @name Modify RES Image Title
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.0
  5. // @description Sets the title attribute on the RES Image in a Reddit thread to match the document title. This is done so extensions like Pinterest can capture the image and use the document's title as the description.
  6. // @author PoorPolonius
  7. // @include http://www.reddit.com/r/*
  8. // @include https://www.reddit.com/r/*
  9. // @match http://www.reddit.com/r/*
  10. // @match https://www.reddit.com/r/*
  11. // @grant GM_log
  12. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
  13. // @license Copyright 2016 reddit.com/u/PoorPolonius; Released under the MIT license
  14. // ==/UserScript==
  15. /* jshint -W097 */
  16. 'use strict';
  17.  
  18. // Your code here...
  19.  
  20. var COUNTER_LIMIT = 5;
  21.  
  22. var _siteTable = "#siteTable",
  23. _toggleButton = "a[class*='toggleImage']",
  24. _resImage = "img[class*='RESImage']";
  25.  
  26. var _buttonCounter = 0,
  27. _imageCounter = 0;
  28.  
  29. var _handlerAttached = false,
  30. _delayTimer = null;
  31.  
  32. function ClearTimeout() {
  33. if (null !== _delayTimer) {
  34. clearTimeout(_delayTimer);
  35. }
  36. }
  37.  
  38. function ToggleHandler(e) {
  39.  
  40. ClearTimeout();
  41. var resImage = $(_resImage);
  42.  
  43. if (0 < resImage.length) {
  44.  
  45. $(_resImage).attr("title", document.title);
  46. DetachToggleHandler();
  47. }
  48. else if (COUNTER_LIMIT !== _imageCounter) {
  49. _delayTimer = setTimeout(ToggleHandler, 100);
  50. }
  51. else {
  52. DetachToggleHandler();
  53. }
  54. _imageCounter++;
  55. }
  56.  
  57. function AttachToggleHandler() {
  58.  
  59. ClearTimeout();
  60. var toggleButton = $(_toggleButton);
  61. if (!_handlerAttached && 0 < toggleButton.length) {
  62. toggleButton.on("click", ToggleHandler);
  63. _handlerAttached = true;
  64. }
  65. else if (COUNTER_LIMIT !== _buttonCounter) {
  66. _delayTimer = setTimeout(AttachToggleHandler, 100);
  67. }
  68. _buttonCounter++;
  69. }
  70.  
  71. function DetachToggleHandler() {
  72. $(_toggleButton).off("click", ToggleHandler);
  73. }
  74.  
  75. $(document).ready(function () {
  76.  
  77. AttachToggleHandler();
  78. });