# FB過濾關鍵字 ###### tags: `tampermonkey`, `javascript`, `chrome` ``` javascript // ==UserScript== // @name FB過濾關鍵字 // @version 1.20240604 // @description 刪除所有提到的過濾字貼文 // @author icps // @match https://www.facebook.com/* // @grant none // ==/UserScript== (function() { 'use strict'; const keywords = [ { type: "推薦", words: ["為你推薦", "贊助", "· 追蹤\n", "· 加入", " · \n加入", "品牌合作"] }, { type: "劇透", words: ["咒術迴戰", "咒術", "五條", "五條悟", "gojo", "kaisen", "ryomen"] }, ]; function search() { // 貼文 <div class="x1lliihq"> document.querySelectorAll(`div.x1lliihq:not([keyword_checked]):not(div.x1lliihq div.x1lliihq)`).forEach(element => { let shouldHide = false; let elementText = element.innerText.toLowerCase(); keywords.forEach(keywordGroup => { keywordGroup.words.forEach(keyword => { keyword = keyword.toLowerCase().trim() // 是否含有關鍵字 if (elementText.includes(keyword)) { shouldHide = true; // console.log(element); const name = extractText(elementText); console.log(`刪除-${keywordGroup.type}:${keyword} ${name}`); return; }; }); }); // 偵測是否為贊助 element.querySelectorAll('div div span span span span a span span canvas').forEach(child => { if (child.className != null && child.getAttribute('style').includes("vertical-align: middle; display: inline-block;")) { shouldHide = true; // console.log(child); let sponsor = extractText(element.innerText); console.log(`刪除-贊助-${sponsor}`); return; } }); element.setAttribute('keyword_checked', ""); if (shouldHide) { // 隱藏 element.style.display = "none"; // element.innerHTML = ''; // element.remove(element); } }); }; function extractText(text) { text = text.replace(/Facebook/g, "").replace(/在線上\n/g, "") return text.substring(0, 20).split('\n')[0].trim() } window.addEventListener('load', function() { setInterval(search, 300); console.log("FB過濾已經開始運作"); }); })(); ```