Advertisement
Guest User

/bag/ filter v1

a guest
Mar 11th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Filter v1 (Improved)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.4.3
  5. // @description Partially filters images based on file type on 4chan VG threads.
  6. // @author Anonymous
  7. // @match https://boards.4chan.org/vg*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. // Validate thread subject: Only proceed if the thread subject contains keywords like "bag/", "blue archive" (case-insensitive).
  15. const subjectElem = document.querySelector('.postInfo.desktop .subject');
  16. const subjectText = subjectElem?.textContent?.trim() ?? '';
  17. // Improved regex: checks for "bag/" or "blue archive" (case-insensitive)
  18. if (!/(bag\/|blue archive)/i.test(subjectText)) {
  19. return;
  20. }
  21.  
  22. // Inject CSS to control the filtered images and hide deleted posts.
  23. const style = document.createElement('style');
  24. style.textContent = `
  25. .fileThumb img.filtered-image {
  26. width: 40px !important;
  27. height: 40px !important;
  28. object-fit: cover;
  29. object-position: center top;
  30. transition: all 0.2s ease;
  31. }
  32. .fileThumb:hover img.filtered-image {
  33. width: auto !important;
  34. height: auto !important;
  35. object-fit: initial;
  36. z-index: 100;
  37. }
  38. .deleted-post {
  39. display: none;
  40. }
  41. `;
  42. document.head.appendChild(style);
  43.  
  44. /**
  45. * Apply partial filtering to image thumbnails.
  46. * For each post, if its file name ends with .png or .jpg, mark the thumbnail with a "filtered-image" class.
  47. * Otherwise, if the file is of type gif, mp4, or webm, skip it.
  48. */
  49. function applyPartialImageFiltering() {
  50. const posts = document.querySelectorAll('.postContainer');
  51. posts.forEach((post) => {
  52. const fileText = post.querySelector('.fileText-original, .fileText');
  53. if (!fileText) return;
  54.  
  55. const link = fileText.querySelector('a');
  56. if (!link) return;
  57.  
  58. const filename = link.textContent.trim();
  59. const isPngOrJpg = /\.(png|jpg)$/i.test(filename);
  60.  
  61. // If not PNG or JPG, skip if file type is gif, mp4, or webm.
  62. if (!isPngOrJpg) {
  63. const excludedTypes = /\.(gif|mp4|webm)$/i;
  64. if (excludedTypes.test(filename)) return;
  65. }
  66.  
  67. const img = post.querySelector('.fileThumb img');
  68. if (img && !img.classList.contains('filtered-image')) {
  69. img.classList.add('filtered-image');
  70. // Cache original dimensions (if not already stored)
  71. if (!img.dataset.originalWidth) {
  72. img.dataset.originalWidth = img.style.width || img.getAttribute('width') || '';
  73. img.dataset.originalHeight = img.style.height || img.getAttribute('height') || '';
  74. }
  75. img.dataset.filtered = 'true';
  76. }
  77. });
  78. }
  79.  
  80. /**
  81. * Patch compatibility with external extensions.
  82. * For example, if the ImageExpansion extension is present, override its expand method
  83. * to temporarily remove the filtered-image class so that the full image is shown.
  84. */
  85. function patchExtensionCompatibility() {
  86. setTimeout(() => {
  87. if (window.ImageExpansion) {
  88. const originalExpand = window.ImageExpansion.expand;
  89. window.ImageExpansion.expand = function (e) {
  90. const targetImg = e.target.nodeName === 'IMG'
  91. ? e.target
  92. : e.target.querySelector('img');
  93. if (targetImg && targetImg.dataset.filtered === 'true') {
  94. targetImg.classList.remove('filtered-image');
  95. }
  96. return originalExpand.apply(this, arguments);
  97. };
  98. }
  99. }, 1000);
  100. }
  101.  
  102. // Set up a MutationObserver to run the filtering function whenever new posts are added.
  103. const observer = new MutationObserver(() => {
  104. applyPartialImageFiltering();
  105. });
  106. observer.observe(document.body, { childList: true, subtree: true });
  107.  
  108. // Initial run
  109. applyPartialImageFiltering();
  110. patchExtensionCompatibility();
  111. })();
Tags: 4chan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement