Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 2ch: hide posts containing "sosal"/"сосал"
- // @namespace https://2ch.su/
- // @version 1.0
- // @description Hides forum messages that contain the word "sosal" or "сосал" (case-insensitive)
- // @author you
- // @match https://2ch.su/*
- // @match https://2ch.hk/*
- // @match https://2ch.life/*
- // @match https://2ch.pm/*
- // @run-at document-end
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- // ---------- settings ----------
- // Substring match: also catches "СОСАЛ", "сосалом", "сосала", "sosal'".
- // If you want only the exact whole word, use instead:
- // const FILTER = /(^|[^a-zа-яё])(sosal|сосал)(?![a-zа-яё])/i;
- const FILTER = /(sosal|сосал)/i;
- const HIDE_OP_POST = false; // true = also hide the thread's opening post if it matches
- const REMOVE_FROM_DOM = false; // false = display:none (safer for the site's own JS), true = remove element
- // --------------------------------
- const PROCESSED = 'data-sosal-filter-checked';
- let hiddenCount = 0;
- function checkPost(post) {
- if (!post || post.hasAttribute(PROCESSED)) return;
- post.setAttribute(PROCESSED, '1');
- if (!HIDE_OP_POST && post.classList.contains('post_type_oppost')) return;
- const article = post.querySelector('article.post__message');
- if (!article) return;
- const text = article.textContent || '';
- if (FILTER.test(text)) {
- if (REMOVE_FROM_DOM) {
- post.remove();
- } else {
- post.style.display = 'none';
- post.classList.add('sosal-filtered');
- }
- hiddenCount++;
- console.log(`[sosal-filter] hidden post #${post.dataset.num} (total: ${hiddenCount})`);
- }
- }
- function scan(root) {
- if (!root || root.nodeType !== Node.ELEMENT_NODE) return;
- if (root.matches && root.matches('div.post')) checkPost(root);
- root.querySelectorAll('div.post').forEach(checkPost);
- }
- // 1) initial pass over already-rendered posts
- scan(document.body);
- // 2) watch for posts added later (auto-refresh, "Обновить", hover previews)
- const observer = new MutationObserver(mutations => {
- for (const m of mutations) {
- for (const node of m.addedNodes) {
- if (node.nodeType !== Node.ELEMENT_NODE) continue;
- if (node.matches('div.post')) {
- checkPost(node);
- } else if (node.querySelector && node.querySelector('div.post')) {
- scan(node);
- } else {
- // message body may be inserted/replaced separately
- const parentPost = node.closest && node.closest('div.post');
- if (parentPost) {
- parentPost.removeAttribute(PROCESSED);
- checkPost(parentPost);
- }
- }
- }
- }
- });
- observer.observe(document.body, { childList: true, subtree: true });
- })();
Add Comment
Please, Sign In to add comment