小隐的博客

人生在世,笑饮一生
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
manifest.json
{
  "manifest_version": 3,
  "name": "抖音关键词屏蔽",
  "version": "1.0",
  "description": "屏蔽抖音视频标题中包含'xxxxxx'关键词的视频",
  "permissions": ["activeTab"],
  "content_scripts": [
    {
      "matches": ["*://*.douyin.com/*"],
      "js": ["content.js"],
      "run_at": "document_end"
    }
  ],
  "action": {
    "default_popup": "popup.html"
  }
}

 content.js

// ======== 用户配置 ========
const blockWords = ['惊悚', 'steam游戏', '画离弦', '猫meme', 'roblox'];
const jumpURL = 'about:blank';
// ==========================

const pattern = new RegExp(
  blockWords.map(w => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'),
  'i'
);

/* 1. 取“正在播放”的卡片节点 */
function getActiveCard() {
  // 正在播放的 <video> 必定带 autoplay 属性
  const activeVideo = document.querySelector('video[autoplay]');
  if (!activeVideo) return null;

  // 向上找最近的卡片容器
  return activeVideo.closest('[data-e2e="feed-item"], .dySwiperSlide');
}

/* 2. 在卡片内拿标题 + account,合并文本 */
function getCurrentText() {
  const card = getActiveCard();
  if (!card) return '';

  const desc = card.querySelector('div.title[data-e2e="video-desc"]');
  const acc  = card.querySelector('div.account');
  const t1 = desc ? desc.textContent.trim() : '';
  const t2 = acc  ? acc.textContent.trim()   : '';

  //console.log('[DBG] 当前卡片标题:', t1, '| account:', t2);
  return `${t1} ${t2}`.trim();
}

/* 3. 检查 + 跳转 */
function check() {
  const txt = getCurrentText();
  if (txt && pattern.test(txt)) {
    //console.log('[Block] 命中关键词,立即跳转 → ', txt);
    location.replace(jumpURL);
  }
}

/* 4. 监听:DOM 变化 / 路由变化 */
const observer = new MutationObserver(check);
observer.observe(document.body, {
  childList: true,
  subtree: true,
  attributes: true,
  attributeFilter: ['autoplay']   // <video autoplay> 出现就触发
});

/* 5. 路由变化也补一次 */
let lastHref = location.href;
setInterval(() => {
  if (location.href !== lastHref) {
    lastHref = location.href;
    setTimeout(check, 2000); // 等新卡片渲染完
  }
}, 2000);

/* 6. 首次运行 */
check();

popup.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"><!-- 关键:指定 UTF-8 -->
  <style>
    body { width: 200px; padding: 12px; font: 14px/1.4 sans-serif; }
    .ok { color: #4CAF50; font-weight: bold; }
  </style>
</head>
<body>
  <h3>抖音关键词屏蔽</h3>
  <p>状态: <span class="ok">运行中</span></p>
  <p>屏蔽关键词: xxx </p>
</body>
</html>

 

QQ交流