Guest User

filter only images, images without quotes

a guest
Jun 4th, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Toggle Post Filters (Images / No Quote Backlinks)
  3. // @namespace    http://tampermonkey.net/
  4. // @version      2.1
  5. // @description  Toggle to hide posts without images, or posts with images but containing quotelinks in backlinks
  6. // @match        https://boards.4chan.org/*
  7. // @icon         https://www.google.com/s2/favicons?sz=64&domain=4chan.org
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12.     'use strict';
  13.  
  14.     const hiddenClass = 'tm-hidden';
  15.  
  16.     const style = document.createElement('style');
  17.     style.textContent = `
  18.         .tm-hidden {
  19.             display: none !important;
  20.         }
  21.         .tm-toggle-button {
  22.             position: fixed;
  23.             top: 10px;
  24.             right: 10px;
  25.             padding: 8px 12px;
  26.             margin-top: 5px;
  27.             background-color: #007BFF;
  28.             color: #fff;
  29.             border: none;
  30.             border-radius: 4px;
  31.             cursor: pointer;
  32.             z-index: 10000;
  33.             font-size: 14px;
  34.         }
  35.         .tm-toggle-button:hover {
  36.             background-color: #0056b3;
  37.         }
  38.         #toggleQuoteBtn {
  39.             top: 50px;
  40.         }
  41.     `;
  42.     document.head.appendChild(style);
  43.  
  44.     let filterNoImage = false;
  45.     let filterImageWithQuote = false;
  46.  
  47.     function updatePosts() {
  48.         const posts = document.querySelectorAll('.postContainer');
  49.         posts.forEach(post => {
  50.             const hasImage = post.querySelector('img') !== null;
  51.  
  52.             const backlinkContainer = post.querySelector('.postInfo > .backlink');
  53.             const hasBacklinkQuote = backlinkContainer && backlinkContainer.querySelector('a.quotelink') !== null;
  54.  
  55.             let hide = false;
  56.  
  57.             if (filterNoImage && !hasImage) {
  58.                 hide = true;
  59.             }
  60.  
  61.             if (filterImageWithQuote && hasImage && hasBacklinkQuote) {
  62.                 hide = true;
  63.             }
  64.  
  65.             if (hide) {
  66.                 post.classList.add(hiddenClass);
  67.             } else {
  68.                 post.classList.remove(hiddenClass);
  69.             }
  70.         });
  71.     }
  72.  
  73.     function createButton(id, text, topOffset, onClick) {
  74.         const btn = document.createElement('button');
  75.         btn.id = id;
  76.         btn.className = 'tm-toggle-button';
  77.         btn.style.top = `${topOffset}px`;
  78.         btn.textContent = text;
  79.         btn.addEventListener('click', onClick);
  80.         document.body.appendChild(btn);
  81.         return btn;
  82.     }
  83.  
  84.     const btnImage = createButton('toggleImageBtn', 'Filter: Only Images [OFF]', 10, () => {
  85.         filterNoImage = !filterNoImage;
  86.         btnImage.textContent = `Filter: Only Images [${filterNoImage ? 'ON' : 'OFF'}]`;
  87.         updatePosts();
  88.     });
  89.  
  90.     const btnQuote = createButton('toggleQuoteBtn', 'Filter: Images w/ Backlink [OFF]', 50, () => {
  91.         filterImageWithQuote = !filterImageWithQuote;
  92.         btnQuote.textContent = `Filter: Images w/ Backlink [${filterImageWithQuote ? 'ON' : 'OFF'}]`;
  93.         updatePosts();
  94.     });
  95.  
  96.     window.addEventListener('load', updatePosts);
  97.  
  98.     const observer = new MutationObserver(mutations => {
  99.         mutations.forEach(mutation => {
  100.             mutation.addedNodes.forEach(node => {
  101.                 if (node.nodeType === Node.ELEMENT_NODE) {
  102.                     const posts = node.matches('.postContainer') ? [node] : node.querySelectorAll('.postContainer');
  103.                     posts.forEach(post => {
  104.                         const hasImage = post.querySelector('img') !== null;
  105.  
  106.                         const backlinkContainer = post.querySelector('.postInfo > .backlink');
  107.                         const hasBacklinkQuote = backlinkContainer && backlinkContainer.querySelector('a.quotelink') !== null;
  108.  
  109.                         let hide = false;
  110.  
  111.                         if (filterNoImage && !hasImage) hide = true;
  112.                         if (filterImageWithQuote && hasImage && hasBacklinkQuote) hide = true;
  113.  
  114.                         if (hide) post.classList.add(hiddenClass);
  115.                     });
  116.                 }
  117.             });
  118.         });
  119.     });
  120.  
  121.     observer.observe(document.body, { childList: true, subtree: true });
  122. })();
Advertisement
Add Comment
Please, Sign In to add comment