Guest User

Untitled

a guest
Jul 3rd, 2026
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | Software | 0 0
  1. // ==UserScript==
  2. // @name ITHardware - ukrywanie wybranych tematów
  3. // @namespace local.ithardware.hide-topics
  4. // @version 1.0.0
  5. // @description Ukrywa wybrane tematy z list działów, najnowszych postów, aktywności i nieprzeczytanych na forum ITHardware / Invision Community.
  6. // @match https://forum.ithardware.pl/*
  7. // @run-at document-start
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. /*
  15. Jak dodać kolejny temat?
  16.  
  17. Z URL-a:
  18. https://forum.ithardware.pl/topic/7993-polityka-w-polsce-i-na-świecie-wątek-ogólny/
  19.  
  20. bierzesz ID po /topic/, czyli tutaj: 7993
  21.  
  22. Dodajesz np.:
  23. { id: 12345, title: 'Nazwa tematu opcjonalnie' },
  24. */
  25.  
  26. const HIDDEN_TOPICS = [
  27. {
  28. id: 7993,
  29. title: 'Polityka w Polsce i na Świecie - wątek ogólny',
  30. },
  31. ];
  32.  
  33. const OPTIONS = {
  34. debug: false,
  35.  
  36. // false = ukrywa temat z list i aktywności, ale jak ręcznie wejdziesz w URL tematu, strona się otworzy
  37. // true = także blokuje bezpośrednie wejście w ukryty temat
  38. blockDirectTopicPage: false,
  39.  
  40. // gdzie przenieść użytkownika, gdy blockDirectTopicPage = true
  41. directTopicRedirectUrl: 'https://forum.ithardware.pl/',
  42. };
  43.  
  44. const HIDE_CLASS = 'tm-ith-hidden-topic';
  45. const STYLE_ID = 'tm-ith-hidden-topic-style';
  46.  
  47. const ITEM_SELECTORS = [
  48. // widok działu / listy tematów
  49. 'li.ipsDataItem',
  50. 'div.ipsDataItem',
  51. 'tr.ipsDataItem',
  52.  
  53. // aktywność / discover / unread
  54. 'li.ipsStreamItem',
  55. 'div.ipsStreamItem',
  56. 'article.ipsStreamItem',
  57.  
  58. // różne warianty Invision / widgety
  59. 'li.cForumRow',
  60. 'li[data-rowid]',
  61. 'article',
  62.  
  63. // fallback dla widgetu "Najnowsze posty"
  64. 'li',
  65.  
  66. // fallback dla tabel
  67. 'tr',
  68. ];
  69.  
  70. function log(...args) {
  71. if (OPTIONS.debug) {
  72. console.log('[ITH hide topics]', ...args);
  73. }
  74. }
  75.  
  76. function injectStyle() {
  77. if (document.getElementById(STYLE_ID)) return;
  78.  
  79. const style = document.createElement('style');
  80. style.id = STYLE_ID;
  81. style.textContent = `
  82. .${HIDE_CLASS} {
  83. display: none !important;
  84. }
  85. `;
  86.  
  87. const target = document.head || document.documentElement;
  88. target.appendChild(style);
  89. }
  90.  
  91. function safeDecode(value) {
  92. try {
  93. return decodeURIComponent(value);
  94. } catch {
  95. return value;
  96. }
  97. }
  98.  
  99. function normalizeText(value) {
  100. return safeDecode(String(value || ''))
  101. .replace(/\s+/g, ' ')
  102. .trim()
  103. .toLowerCase();
  104. }
  105.  
  106. function escapeRegExp(value) {
  107. return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  108. }
  109.  
  110. function hrefMatchesTopicId(href, topicId) {
  111. const id = escapeRegExp(topicId);
  112.  
  113. return (
  114. new RegExp(`/topic/${id}(?:-|/|\\?|#|$)`, 'i').test(href) ||
  115. new RegExp(`[?&](?:topic|showtopic)=${id}(?:&|$)`, 'i').test(href)
  116. );
  117. }
  118.  
  119. function linkMatchesHiddenTopic(anchor) {
  120. const rawHref = `${anchor.getAttribute('href') || ''} ${anchor.href || ''}`;
  121. const href = normalizeText(rawHref);
  122. const text = normalizeText(anchor.textContent);
  123.  
  124. return HIDDEN_TOPICS.some(topic => {
  125. if (topic.id && hrefMatchesTopicId(href, topic.id)) {
  126. return true;
  127. }
  128.  
  129. if (topic.urlParts) {
  130. const matchesUrlPart = topic.urlParts.some(part =>
  131. href.includes(normalizeText(part))
  132. );
  133.  
  134. if (matchesUrlPart) {
  135. return true;
  136. }
  137. }
  138.  
  139. if (topic.title) {
  140. const wantedTitle = normalizeText(topic.title);
  141.  
  142. // Celowo sprawdzamy tekst linku, nie cały kontener,
  143. // żeby nie ukrywać przypadkowych wpisów tylko dlatego,
  144. // że ktoś wspomniał nazwę tematu w treści posta.
  145. if (text.includes(wantedTitle)) {
  146. return true;
  147. }
  148. }
  149.  
  150. return false;
  151. });
  152. }
  153.  
  154. function currentUrlMatchesHiddenTopic() {
  155. const href = normalizeText(window.location.href);
  156.  
  157. return HIDDEN_TOPICS.some(topic => {
  158. if (topic.id && hrefMatchesTopicId(href, topic.id)) {
  159. return true;
  160. }
  161.  
  162. if (topic.urlParts) {
  163. return topic.urlParts.some(part => href.includes(normalizeText(part)));
  164. }
  165.  
  166. return false;
  167. });
  168. }
  169.  
  170. function findHideContainer(anchor) {
  171. const direct = anchor.closest(ITEM_SELECTORS.join(','));
  172.  
  173. if (
  174. direct &&
  175. direct !== document.documentElement &&
  176. direct !== document.body
  177. ) {
  178. return direct;
  179. }
  180.  
  181. // Fallback: idziemy kilka poziomów w górę i szukamy sensownego kontenera.
  182. let el = anchor;
  183.  
  184. for (let i = 0; i < 8; i++) {
  185. el = el.parentElement;
  186.  
  187. if (!el || el === document.body || el === document.documentElement) {
  188. break;
  189. }
  190.  
  191. const className = String(el.className || '');
  192.  
  193. if (
  194. /ipsDataItem|ipsStreamItem|cForum|cTopic|ipsWidget/.test(className)
  195. ) {
  196. return el;
  197. }
  198. }
  199.  
  200. return null;
  201. }
  202.  
  203. function hideAnchorOwner(anchor) {
  204. const container = findHideContainer(anchor);
  205.  
  206. if (!container) {
  207. log('Nie znaleziono kontenera dla linku:', anchor);
  208. return;
  209. }
  210.  
  211. container.classList.add(HIDE_CLASS);
  212. container.setAttribute('data-tm-hidden-topic', '1');
  213.  
  214. log('Ukryto element:', container);
  215. }
  216.  
  217. function scan(root = document) {
  218. if (!root || !root.querySelectorAll) return;
  219.  
  220. injectStyle();
  221.  
  222. if (OPTIONS.blockDirectTopicPage && currentUrlMatchesHiddenTopic()) {
  223. window.location.replace(OPTIONS.directTopicRedirectUrl);
  224. return;
  225. }
  226.  
  227. const anchors = root.querySelectorAll('a[href]');
  228.  
  229. for (const anchor of anchors) {
  230. if (linkMatchesHiddenTopic(anchor)) {
  231. hideAnchorOwner(anchor);
  232. }
  233. }
  234. }
  235.  
  236. let scanQueued = false;
  237.  
  238. function queueScan() {
  239. if (scanQueued) return;
  240.  
  241. scanQueued = true;
  242.  
  243. window.requestAnimationFrame(() => {
  244. scanQueued = false;
  245. scan(document);
  246. });
  247. }
  248.  
  249. injectStyle();
  250.  
  251. if (document.readyState === 'loading') {
  252. document.addEventListener('DOMContentLoaded', () => scan(document), {
  253. once: true,
  254. });
  255. } else {
  256. scan(document);
  257. }
  258.  
  259. const observer = new MutationObserver(queueScan);
  260.  
  261. function startObserver() {
  262. const target = document.body || document.documentElement;
  263.  
  264. if (!target) {
  265. window.setTimeout(startObserver, 50);
  266. return;
  267. }
  268.  
  269. observer.observe(target, {
  270. childList: true,
  271. subtree: true,
  272. });
  273.  
  274. scan(document);
  275. }
  276.  
  277. startObserver();
  278.  
  279. // Invision czasem doładowuje / podmienia widoki bez pełnego reloadu.
  280. const originalPushState = history.pushState;
  281. const originalReplaceState = history.replaceState;
  282.  
  283. history.pushState = function (...args) {
  284. const result = originalPushState.apply(this, args);
  285. queueScan();
  286. return result;
  287. };
  288.  
  289. history.replaceState = function (...args) {
  290. const result = originalReplaceState.apply(this, args);
  291. queueScan();
  292. return result;
  293. };
  294.  
  295. window.addEventListener('popstate', queueScan);
  296. })();
Advertisement
Add Comment
Please, Sign In to add comment