Advertisement
Guest User

Untitled

a guest
Feb 27th, 2025
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. // ==UserScript==
  2. // @name /bag/ filter
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6
  5. // @description Hides non whitelisted images. Images are whitelisted after 15 minutes of the post not being deleted.
  6. // @author Anonymous
  7. // @match https://boards.4chan.org/vg*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. // Check for a thread.
  15. if (!/^bag\/|\/bag\/|Blue Archive|BIue Archive/.test(document?.querySelector('.postInfo.desktop .subject')?.textContent?.trim() ?? '')) return;
  16.  
  17. const ADD_TO_WHITELIST_TIMEOUT_MS = 15 * 60 * 1000;
  18. const MD5_WHITELIST_KEY = 'bagFilter';
  19. const IS_DISABLED_KEY = 'bagFilterDisabled';
  20. const FILTERED_CSS_CLASS = 'bag-filter-filtered';
  21. const FILTER_STYLES = `
  22. .fileThumb img:not(.full-image).${FILTERED_CSS_CLASS} {
  23. width: 50px !important;
  24. height: 50px !important;
  25. opacity: 0;
  26. }`;
  27.  
  28. const styleSheet = document.createElement('style');
  29. document.head.appendChild(styleSheet);
  30.  
  31. const opLinks = document.querySelector('.opContainer .fileText');
  32. const toggleLink = document.createElement('a');
  33. toggleLink.href = '#';
  34. opLinks.appendChild(document.createTextNode(' '));
  35. opLinks.appendChild(toggleLink);
  36. toggleLink.onclick = () => {
  37. window.localStorage.setItem(IS_DISABLED_KEY, String(!isDisabled()));
  38. updateToggle();
  39. }
  40.  
  41. function isDisabled() {
  42. return window.localStorage.getItem(IS_DISABLED_KEY) === 'true';
  43. }
  44.  
  45. function updateToggle() {
  46. if (isDisabled()) {
  47. toggleLink.textContent = 'enable bag filter';
  48. styleSheet.textContent = '';
  49. } else {
  50. toggleLink.textContent = 'disable bag filter';
  51. styleSheet.textContent = FILTER_STYLES;
  52. }
  53. }
  54.  
  55. const md5Whitelist = new Set(JSON.parse(window.localStorage.getItem(MD5_WHITELIST_KEY) || '[]'));
  56. function update() {
  57. if (isDisabled()) {
  58. return;
  59. }
  60. const addToWhitelistPostTime = Date.now() - ADD_TO_WHITELIST_TIMEOUT_MS;
  61. const posts = document.querySelectorAll('.replyContainer:has(.fileThumb)');
  62. let md5WhitelistChanged = false;
  63. for (const post of posts) {
  64. const postTime = new Number(post.querySelector('.dateTime').dataset.utc) * 1000;
  65. const thumbnail = post.querySelector('img:not(.full-image');
  66. const md5 = thumbnail.dataset.md5;
  67. const otherFiltersMatch = imageSizeMatch(post);
  68. if (post.hasAttribute('hidden')) {
  69. if (md5Whitelist.has(md5)) {
  70. md5Whitelist.delete(md5);
  71. md5WhitelistChanged = true;
  72. }
  73. } else if (postTime < addToWhitelistPostTime && !post.classList.contains('deleted-post')) {
  74. if (!md5Whitelist.has(md5) && otherFiltersMatch) {
  75. md5Whitelist.add(md5);
  76. md5WhitelistChanged = true;
  77. }
  78. }
  79. if (md5Whitelist.has(md5) || !otherFiltersMatch) {
  80. thumbnail.classList.remove(FILTERED_CSS_CLASS);
  81. } else {
  82. thumbnail.classList.add(FILTERED_CSS_CLASS);
  83. }
  84. }
  85. if (md5WhitelistChanged) {
  86. window.localStorage.setItem(MD5_WHITELIST_KEY, JSON.stringify([...md5Whitelist]));
  87. }
  88. }
  89.  
  90. function imageSizeMatch(post) {
  91. const fileText = post.querySelector('.fileText-original, .fileText')?.textContent;
  92. if (fileText) {
  93. const matchInKB = fileText.match(/\((\d+(?:\.\d+)?)\s*KB/i);
  94. if (matchInKB) {
  95. const sizeInKB = parseFloat(matchInKB[1]);
  96. return sizeInKB >= 50;
  97. }
  98. const matchInMB = fileText.match(/\((\d+(?:\.\d+)?)\s*MB/i);
  99. if (matchInMB) {
  100. const sizeInMB = parseFloat(matchInMB[1]);
  101. return sizeInMB >= 1;
  102. }
  103. }
  104. return false;
  105. }
  106.  
  107. // Observe for dynamic content loading (e.g., infinite scrolling)
  108. const observer = new MutationObserver(() => {
  109. update();
  110. });
  111.  
  112. observer.observe(document.body, {childList: true, subtree: true});
  113.  
  114. // Initial run
  115. updateToggle();
  116. update();
  117. })();
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement