Guest User

Untitled

a guest
Sep 25th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name Stop Gif Autoloading
  3. // @include *
  4. // @grant GM_getResourceURL
  5. // @grant GM_addStyle
  6. // @resource placeholder data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAQAQAAAADRoQmIAAAAO0lEQVR42mNQUlJg+P//A0P9/x8MBR0GDPm3PzC8B+L82z8Y8m8UgPlvgDjv9g8g/sBQ0P0Brl5JSQEAEV0g/OxTjp0AAAAASUVORK5CYII=
  7. // ==/UserScript==
  8.  
  9. "use strict";
  10. const placeholderAttr = "fktjifsj";
  11. GM_addStyle(`
  12.     [${placeholderAttr}] {
  13.         cursor: pointer !important;
  14.         image-rendering: -moz-crisp-edges !important;
  15.         image-rendering: crisp-edges !important;
  16.         image-rendering: -webkit-optimize-contrast !important;
  17.         image-rendering: pixelated !important;
  18.     }
  19. `);
  20.  
  21. new MutationObserver((mutations) => {
  22.     mutations.forEach((mutation) => {
  23.         mutation.addedNodes.forEach((node) => {
  24.             if (node.tagName == "IMG") {
  25.                 stopGif(node);
  26.             } else if (node.firstChild) {
  27.                 for (const img of node.getElementsByTagName("img")) {
  28.                     stopGif(img);
  29.                 }
  30.             }
  31.         });
  32.     });
  33. }).observe(document.body, { childList: true, subtree: true });
  34.  
  35. for (const img of document.images) {
  36.     stopGif(img);
  37. }
  38.  
  39. function stopGif(img) {
  40.     let urlObj;
  41.  
  42.     try {
  43.         urlObj = new URL(img.src);
  44.     } catch {
  45.         return;
  46.     }
  47.  
  48.     if (urlObj.pathname.endsWith(".gif")) {
  49.         const origUrl = img.src;
  50.         img.src = GM_getResourceURL("placeholder");
  51.         img.setAttribute(placeholderAttr, "");
  52.  
  53.         img.addEventListener("click", async () => {
  54.             if (!img.alt) img.alt = "GIF";
  55.             img.src = "";
  56.             img.removeAttribute(placeholderAttr);
  57.             await sleep(0);
  58.             img.src = origUrl;
  59.         }, { once: true });
  60.     }
  61. }
  62.  
  63. function sleep(delay) {
  64.     return new Promise(resolve => setTimeout(resolve, delay));
  65. }
  66.  
Add Comment
Please, Sign In to add comment