Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reveal Spoiler Images (Dynamic)
- // @namespace http://tampermonkey.net/
- // @version 1.1
- // @description Replaces spoiler images with real thumbnails even on auto-updating sites
- // @match *://8chan.se/*
- // @match *://8chan.moe/*
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- function replaceSpoilerImages(root = document) {
- const links = root.querySelectorAll('a.imgLink');
- links.forEach(link => {
- const img = link.querySelector('img');
- if (!img) return;
- // Already processed
- if (img.dataset.spoilerHandled === 'true') return;
- if (img.src.includes('/spoiler.png')) {
- const href = link.getAttribute('href');
- const filename = href.split('/').pop();
- const thumbSrc = `/.media/${filename}`;
- img.src = thumbSrc;
- img.dataset.spoilerHandled = 'true'; // Prevent reprocessing
- }
- });
- }
- function expand(mouseEvent, link, mime) {
- if (mouseEvent.which === 2 || mouseEvent.ctrlKey) {
- return true;
- }
- var thumb = link.getElementsByTagName('img')[0];
- if (thumb.style.display === 'none') {
- link.parentNode.parentNode.classList.remove('expandedCell');
- link.getElementsByClassName('imgExpanded')[0].style.display = 'none';
- thumb.style.display = '';
- if (thumb.getBoundingClientRect().top < 0) {
- thumbs.scrollToPost(thumb);
- }
- return false;
- }
- link.parentNode.parentNode.classList.add('expandedCell');
- var expanded = link.getElementsByClassName('imgExpanded')[0];
- if (expanded) {
- thumb.style.display = 'none';
- expanded.style.display = '';
- thumbs.scrollToPost(link);
- } else {
- var expandedSrc = link.href;
- expanded = document.createElement('img');
- expanded.setAttribute('src', expandedSrc);
- expanded.className = 'imgExpanded';
- expanded.style.width = link.dataset.filewidth + "px";
- thumb.style.display = 'none';
- link.appendChild(expanded);
- var maxwidth = Math.min(link.parentNode.getBoundingClientRect(), maxwidth);
- expanded.style.width = maxwidth.width + "px";
- var rect = expanded.getBoundingClientRect();
- expanded.style.height = ((link.dataset.fileheight / link.dataset.filewidth) * maxwidth) + "px";
- }
- //remove image on expand
- if (thumbs.hoveringImage !== undefined) {
- thumbs.hoveringImage.src = "";
- thumbs.hoveringImage.remove();
- }
- return false;
- };
- // thumbs.expandImg = expand;
- // Run on initial load
- replaceSpoilerImages();
- // Set up MutationObserver to watch for new nodes
- const observer = new MutationObserver(mutations => {
- for (const mutation of mutations) {
- mutation.addedNodes.forEach(node => {
- if (node.nodeType === 1) { // ELEMENT_NODE
- // If the node itself might be a spoiler, or contains some
- replaceSpoilerImages(node);
- }
- });
- }
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true
- });
- window.addEventListener('load', function() {
- thumbs.expandImage = expand;
- }, false);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement