Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name ITHardware - ukrywanie wybranych tematów
- // @namespace local.ithardware.hide-topics
- // @version 1.0.0
- // @description Ukrywa wybrane tematy z list działów, najnowszych postów, aktywności i nieprzeczytanych na forum ITHardware / Invision Community.
- // @match https://forum.ithardware.pl/*
- // @run-at document-start
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- /*
- Jak dodać kolejny temat?
- Z URL-a:
- https://forum.ithardware.pl/topic/7993-polityka-w-polsce-i-na-świecie-wątek-ogólny/
- bierzesz ID po /topic/, czyli tutaj: 7993
- Dodajesz np.:
- { id: 12345, title: 'Nazwa tematu opcjonalnie' },
- */
- const HIDDEN_TOPICS = [
- {
- id: 7993,
- title: 'Polityka w Polsce i na Świecie - wątek ogólny',
- },
- ];
- const OPTIONS = {
- debug: false,
- // false = ukrywa temat z list i aktywności, ale jak ręcznie wejdziesz w URL tematu, strona się otworzy
- // true = także blokuje bezpośrednie wejście w ukryty temat
- blockDirectTopicPage: false,
- // gdzie przenieść użytkownika, gdy blockDirectTopicPage = true
- directTopicRedirectUrl: 'https://forum.ithardware.pl/',
- };
- const HIDE_CLASS = 'tm-ith-hidden-topic';
- const STYLE_ID = 'tm-ith-hidden-topic-style';
- const ITEM_SELECTORS = [
- // widok działu / listy tematów
- 'li.ipsDataItem',
- 'div.ipsDataItem',
- 'tr.ipsDataItem',
- // aktywność / discover / unread
- 'li.ipsStreamItem',
- 'div.ipsStreamItem',
- 'article.ipsStreamItem',
- // różne warianty Invision / widgety
- 'li.cForumRow',
- 'li[data-rowid]',
- 'article',
- // fallback dla widgetu "Najnowsze posty"
- 'li',
- // fallback dla tabel
- 'tr',
- ];
- function log(...args) {
- if (OPTIONS.debug) {
- console.log('[ITH hide topics]', ...args);
- }
- }
- function injectStyle() {
- if (document.getElementById(STYLE_ID)) return;
- const style = document.createElement('style');
- style.id = STYLE_ID;
- style.textContent = `
- .${HIDE_CLASS} {
- display: none !important;
- }
- `;
- const target = document.head || document.documentElement;
- target.appendChild(style);
- }
- function safeDecode(value) {
- try {
- return decodeURIComponent(value);
- } catch {
- return value;
- }
- }
- function normalizeText(value) {
- return safeDecode(String(value || ''))
- .replace(/\s+/g, ' ')
- .trim()
- .toLowerCase();
- }
- function escapeRegExp(value) {
- return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
- }
- function hrefMatchesTopicId(href, topicId) {
- const id = escapeRegExp(topicId);
- return (
- new RegExp(`/topic/${id}(?:-|/|\\?|#|$)`, 'i').test(href) ||
- new RegExp(`[?&](?:topic|showtopic)=${id}(?:&|$)`, 'i').test(href)
- );
- }
- function linkMatchesHiddenTopic(anchor) {
- const rawHref = `${anchor.getAttribute('href') || ''} ${anchor.href || ''}`;
- const href = normalizeText(rawHref);
- const text = normalizeText(anchor.textContent);
- return HIDDEN_TOPICS.some(topic => {
- if (topic.id && hrefMatchesTopicId(href, topic.id)) {
- return true;
- }
- if (topic.urlParts) {
- const matchesUrlPart = topic.urlParts.some(part =>
- href.includes(normalizeText(part))
- );
- if (matchesUrlPart) {
- return true;
- }
- }
- if (topic.title) {
- const wantedTitle = normalizeText(topic.title);
- // Celowo sprawdzamy tekst linku, nie cały kontener,
- // żeby nie ukrywać przypadkowych wpisów tylko dlatego,
- // że ktoś wspomniał nazwę tematu w treści posta.
- if (text.includes(wantedTitle)) {
- return true;
- }
- }
- return false;
- });
- }
- function currentUrlMatchesHiddenTopic() {
- const href = normalizeText(window.location.href);
- return HIDDEN_TOPICS.some(topic => {
- if (topic.id && hrefMatchesTopicId(href, topic.id)) {
- return true;
- }
- if (topic.urlParts) {
- return topic.urlParts.some(part => href.includes(normalizeText(part)));
- }
- return false;
- });
- }
- function findHideContainer(anchor) {
- const direct = anchor.closest(ITEM_SELECTORS.join(','));
- if (
- direct &&
- direct !== document.documentElement &&
- direct !== document.body
- ) {
- return direct;
- }
- // Fallback: idziemy kilka poziomów w górę i szukamy sensownego kontenera.
- let el = anchor;
- for (let i = 0; i < 8; i++) {
- el = el.parentElement;
- if (!el || el === document.body || el === document.documentElement) {
- break;
- }
- const className = String(el.className || '');
- if (
- /ipsDataItem|ipsStreamItem|cForum|cTopic|ipsWidget/.test(className)
- ) {
- return el;
- }
- }
- return null;
- }
- function hideAnchorOwner(anchor) {
- const container = findHideContainer(anchor);
- if (!container) {
- log('Nie znaleziono kontenera dla linku:', anchor);
- return;
- }
- container.classList.add(HIDE_CLASS);
- container.setAttribute('data-tm-hidden-topic', '1');
- log('Ukryto element:', container);
- }
- function scan(root = document) {
- if (!root || !root.querySelectorAll) return;
- injectStyle();
- if (OPTIONS.blockDirectTopicPage && currentUrlMatchesHiddenTopic()) {
- window.location.replace(OPTIONS.directTopicRedirectUrl);
- return;
- }
- const anchors = root.querySelectorAll('a[href]');
- for (const anchor of anchors) {
- if (linkMatchesHiddenTopic(anchor)) {
- hideAnchorOwner(anchor);
- }
- }
- }
- let scanQueued = false;
- function queueScan() {
- if (scanQueued) return;
- scanQueued = true;
- window.requestAnimationFrame(() => {
- scanQueued = false;
- scan(document);
- });
- }
- injectStyle();
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', () => scan(document), {
- once: true,
- });
- } else {
- scan(document);
- }
- const observer = new MutationObserver(queueScan);
- function startObserver() {
- const target = document.body || document.documentElement;
- if (!target) {
- window.setTimeout(startObserver, 50);
- return;
- }
- observer.observe(target, {
- childList: true,
- subtree: true,
- });
- scan(document);
- }
- startObserver();
- // Invision czasem doładowuje / podmienia widoki bez pełnego reloadu.
- const originalPushState = history.pushState;
- const originalReplaceState = history.replaceState;
- history.pushState = function (...args) {
- const result = originalPushState.apply(this, args);
- queueScan();
- return result;
- };
- history.replaceState = function (...args) {
- const result = originalReplaceState.apply(this, args);
- queueScan();
- return result;
- };
- window.addEventListener('popstate', queueScan);
- })();
Advertisement
Add Comment
Please, Sign In to add comment