Advertisement
Guest User

Untitled

a guest
Apr 20th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Reveal Spoiler Images (Dynamic)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Replaces spoiler images with real thumbnails even on auto-updating sites
  6. // @match *://8chan.se/*
  7. // @match *://8chan.moe/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11.  
  12.  
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. function replaceSpoilerImages(root = document) {
  18. const links = root.querySelectorAll('a.imgLink');
  19.  
  20. links.forEach(link => {
  21. const img = link.querySelector('img');
  22. if (!img) return;
  23.  
  24. // Already processed
  25. if (img.dataset.spoilerHandled === 'true') return;
  26.  
  27. if (img.src.includes('/spoiler.png')) {
  28. const href = link.getAttribute('href');
  29. const filename = href.split('/').pop();
  30. const thumbSrc = `/.media/${filename}`;
  31. img.src = thumbSrc;
  32. img.dataset.spoilerHandled = 'true'; // Prevent reprocessing
  33. }
  34. });
  35. }
  36.  
  37. function expand(mouseEvent, link, mime) {
  38.  
  39. if (mouseEvent.which === 2 || mouseEvent.ctrlKey) {
  40. return true;
  41. }
  42.  
  43. var thumb = link.getElementsByTagName('img')[0];
  44.  
  45. if (thumb.style.display === 'none') {
  46. link.parentNode.parentNode.classList.remove('expandedCell');
  47. link.getElementsByClassName('imgExpanded')[0].style.display = 'none';
  48. thumb.style.display = '';
  49.  
  50. if (thumb.getBoundingClientRect().top < 0) {
  51. thumbs.scrollToPost(thumb);
  52. }
  53.  
  54. return false;
  55. }
  56. link.parentNode.parentNode.classList.add('expandedCell');
  57.  
  58. var expanded = link.getElementsByClassName('imgExpanded')[0];
  59.  
  60. if (expanded) {
  61. thumb.style.display = 'none';
  62. expanded.style.display = '';
  63. thumbs.scrollToPost(link);
  64. } else {
  65. var expandedSrc = link.href;
  66.  
  67. expanded = document.createElement('img');
  68. expanded.setAttribute('src', expandedSrc);
  69. expanded.className = 'imgExpanded';
  70. expanded.style.width = link.dataset.filewidth + "px";
  71.  
  72. thumb.style.display = 'none';
  73. link.appendChild(expanded);
  74. var maxwidth = Math.min(link.parentNode.getBoundingClientRect(), maxwidth);
  75. expanded.style.width = maxwidth.width + "px";
  76. var rect = expanded.getBoundingClientRect();
  77. expanded.style.height = ((link.dataset.fileheight / link.dataset.filewidth) * maxwidth) + "px";
  78. }
  79.  
  80. //remove image on expand
  81. if (thumbs.hoveringImage !== undefined) {
  82. thumbs.hoveringImage.src = "";
  83. thumbs.hoveringImage.remove();
  84. }
  85.  
  86. return false;
  87.  
  88. };
  89.  
  90. // thumbs.expandImg = expand;
  91. // Run on initial load
  92. replaceSpoilerImages();
  93.  
  94. // Set up MutationObserver to watch for new nodes
  95. const observer = new MutationObserver(mutations => {
  96. for (const mutation of mutations) {
  97. mutation.addedNodes.forEach(node => {
  98. if (node.nodeType === 1) { // ELEMENT_NODE
  99. // If the node itself might be a spoiler, or contains some
  100. replaceSpoilerImages(node);
  101. }
  102. });
  103. }
  104. });
  105.  
  106. observer.observe(document.body, {
  107. childList: true,
  108. subtree: true
  109. });
  110.  
  111. window.addEventListener('load', function() {
  112. thumbs.expandImage = expand;
  113. }, false);
  114. })();
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement