Load GIFs before playing

Hides a GIF until it's fully loaded

  1. // ==UserScript==
  2. // @name Load GIFs before playing
  3. // @namespace https://greasyfork.org/en/users/321-joesimmons
  4. // @description Hides a GIF until it's fully loaded
  5. // @include http://*
  6. // @include https://*
  7. // @exclude http://*.gif
  8. // @exclude https://*.gif
  9. // @exclude http://*.gif?*
  10. // @exclude https://*.gif?*
  11. // @copyright JoeSimmons
  12. // @author JoeSimmons
  13. // @version 1.0.0
  14. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  15. // @grant GM_addStyle
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. var gifs = document.querySelectorAll('img[src$=".gif"]'), gif, i;
  22.  
  23. function unhide(elem) {
  24. elem.target.style.visibility = 'visible';
  25. }
  26.  
  27. for (i = 0; i < gifs.length; i += 1) {
  28. gif = gifs[i];
  29.  
  30. // skip ones that are hidden already
  31. if (gif.style.visiblity === 'hidden' || gif.style.display === 'none') {
  32. continue;
  33. }
  34.  
  35. // temporarily hide the GIF
  36. gif.style.visibility = 'hidden';
  37.  
  38. // set it to un-hide when it's fully loaded
  39. gif.addEventListener('load', unhide, false);
  40. }
  41. }());