YouTube Keyword Blocker (Whole‑Word Mode)

Hide YouTube homepage videos by blacklisted words in title or channel name

目前為 2025-05-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Keyword Blocker (Whole‑Word Mode)
// @description  Hide YouTube homepage videos by blacklisted words in title or channel name
// @match        https://www.youtube.com/*
// @run-at       document-end
// @version 0.0.1.20250522140812
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function () {
  'use strict';

  // skip “results” and “@channel” pages
  if (location.pathname.startsWith('/results') || location.pathname.startsWith('/@')) return;

  // 1) List words
  const terms = [
 
  ];

  // 2) Convert to RegExp objects with \b boundaries, case‑insensitive
  const blacklist = terms.map(term => {
    const esc = term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    return new RegExp(`\\b${esc}\\b`, 'i');
  });

  const ITEM_SELECTOR = 'ytd-rich-item-renderer'; /*, ytd-rich-grid-media, ytd-video-renderer*/
  const TITLE_SELECTOR = '#video-title';
  const CHANNEL_SELECTOR = 'ytd-channel-name a';

  function filterItem(item) {

    // skip “results” and “@channel” pages on dynamic loads
    if (location.pathname.startsWith('/results') ||
      location.pathname.startsWith('/@')) return;

    // skip if we've already seen this node
    if (item.dataset.keywordBlocked) return;
    item.dataset.keywordBlocked = '1';

    const linkEl = item.querySelector('a#video-title-link');
    const title = linkEl
      ? linkEl.getAttribute('aria-label').trim()
      : '‹no title›';
    console.log('› filterItem saw:', item.tagName, title);

    const channelEl = item.querySelector(CHANNEL_SELECTOR);
    const channel = channelEl ? channelEl.textContent.trim() : '';

    for (const re of blacklist) {
      if (re.test(title) || re.test(channel)) {
        console.log(
          `Blocking video:\n` +
          `  title:   "${title}"\n` +
          `  channel: "${channel}"\n` +
          `  matched: ${re}`
        );
        item.style.display = 'none';
        return;
      }
    }
  }

  function scanPage() {
    // skip “results” and “@channel” pages on every scan
    if (location.pathname.startsWith('/results') ||
      location.pathname.startsWith('/@')) return;
    document.querySelectorAll(ITEM_SELECTOR).forEach(filterItem);
  }

  new MutationObserver(mutations => {

    for (const { addedNodes } of mutations) {
      for (const node of addedNodes) {
        if (!(node instanceof HTMLElement)) continue;
        if (node.matches(ITEM_SELECTOR)) filterItem(node);
        else node.querySelectorAll && node.querySelectorAll(ITEM_SELECTOR).forEach(filterItem);
      }
    }
  }).observe(document.body, { childList: true, subtree: true });

  scanPage();

  window.addEventListener('yt-navigate-finish', scanPage);
})();