# threads一鍵封鎖和過濾關鍵字 ###### tags: `tampermonkey`, `javascript`, `chrome` ![image](https://hackmd.io/_uploads/SkmlVAHBR.png) ![image](https://hackmd.io/_uploads/BJbX-9ZHA.png) ``` javascript // ==UserScript== // @name threads一鍵封鎖和過濾關鍵字 // @version 1.20240604 // @description threads一鍵封鎖和過濾關鍵字 // @author icps // @match https://www.threads.net/* // @grant none // ==/UserScript== (function() { 'use strict'; // 過濾關鍵字 const keywords = [ { type: "劇透", words: ["咒術迴戰", "咒術", "五條", "五條悟", "gojo", "kaisen", "ryomen"] }, ]; // 建立按鈕 const button1 = createButton('封鎖'); const button2 = createButton('首頁'); const button3 = createButton('返回'); function createButton(text) { const button = document.createElement('button'); button.innerText = text; button.style.textAlign = 'center'; button.style.position = 'fixed'; button.style.transform = 'translateX(-50%)'; button.style.padding = '10px 20px'; button.style.backgroundColor = 'black'; button.style.color = 'white'; button.style.border = '2px solid white'; button.style.borderRadius = '10px'; button.style.cursor = 'pointer'; button.style.fontSize = '16px'; button.style.zIndex = '1000'; document.body.appendChild(button); return button; } // 計算按鈕位置 function buttonPosition() { let browserWidth = window.innerWidth; let bottom = window.innerHeight * 0.45; // 按鈕高度 button1.style.bottom = `${bottom-0*70}px`; button2.style.bottom = `${bottom-1*70}px`; button3.style.bottom = `${bottom-2*70}px`; let left = browserWidth * 0.8; if (browserWidth < 750) {; left = browserWidth - 60; } else if (browserWidth < 1200) {; left = browserWidth * 0.9; } // 按鈕x位置 button1.style.left = `${left}px`; button2.style.left = `${left}px`; button3.style.left = `${left}px`; // console.log(`視窗:${browserWidth}, ${divWidth}, ${left}`) } // 點選返回的箭頭 function clickBack() {; let element4 = document.querySelector("div.xc26acl.x9f619.x40hh3e.xrvj5dj.xfzwrhw.xcm5wjh.xl56j7k.x1hc1fzr.x1swvt13.x1pi30zi.x10l6tqk.x1od1hhl.x13vifvy.x1ja2u2z.xng8ra.x1ggkfyp > div.x1nhvcw1.xurb0ha.x6s0dn4.x78zum5 > div > div > div"); element4.click(); } // 搜尋 function search() { document.querySelectorAll(`div.x1a6qonq.x6ikm8r.x10wlt62.xj0a0fe.x126k92a.x6prxxf:not([keyword_checked])`).forEach(element => {; let elementText = element.innerText.toLowerCase(); keywords.forEach(keywordGroup => { keywordGroup.words.forEach(keyword => { keyword = keyword.toLowerCase().trim(); if (elementText.includes(keyword)) { // console.log(element); // 移除底下span var spans = element.querySelectorAll('span'); spans.forEach(function(span) {; span.remove(); }); // 增加隱藏字樣 var span = document.createElement('span'); span.className = "x1lliihq x1plvlek xryxfnj x1n2onr6 x193iq5w xeuugli x1fj9vlw x13faqbe x1vvkbs x1s928wv xhkezso x1gmr53x x1cpjm7i x1fgarty x1943h6x x1i0vuye xjohtrz xo1l8bm xp07o12 x1yc453h xat24cr xdj266r"; span.setAttribute('dir', 'auto'); span.style.lineHeight = 'var(--base-line-clamp-line-height)'; span.style.setProperty('--base-line-clamp-line-height', 'calc(1.4 * 1em)'); span.textContent = `****** 劇透已隱藏 ${keyword} ******`; element.appendChild(span); return; }; }); }); element.setAttribute('keyword_checked', ""); }); }; // 暫停 async function sleep(duration) { return new Promise((resolve) => setTimeout(resolve, duration)); } // 監聽按鈕button1點擊 button1.addEventListener('click', async function() { await sleep(500); let status = document.querySelector("div.xb57i2i.x1q594ok.x5lxg6s.x1ja2u2z.x1pq812k.x1rohswg.xfk6m8.x1yqm8si.xjx87ck.x1l7klhg.xs83m0k.x2lwn1j.xx8ngbg.xwo3gff.x1oyok0e.x1odjw0f.x1n2onr6.xq1qtft.xz401s1.x195bbgf.x3ir7cq.x1l19134.x1m0igow.x1ejq31n.xu3j5b3.x1q0q8m5.x26u7qi.x13lgxp2.x168nmei.xt8cgyo.xj515ic.x1co6499.x2j4hbs.x1ma7e2m.x9f619.x78zum5.xdt5ytf.x1iyjqo2.x6ikm8r.xy5w88m.xh8yej3.xbwb3hm.x1x3vcui.x19xvnzb.x1y4ox9t.xev1tu8.xpr2fh2.xgzc8be.x1iorvi4 > div.x78zum5.xdt5ytf.x1iyjqo2.x1n2onr6 > div.x7bdzy2.x1ktqkkm.x3i0t3l.x109q661.x1amjocr.x78zum5.xz9dl7a.xsag5q8 > div > div").innerText; // 若尚未封鎖 if (status != "解除封鎖") { // 點三個點 let element1 = document.querySelector("div.xb57i2i.x1q594ok.x5lxg6s.x1ja2u2z.x1pq812k.x1rohswg.xfk6m8.x1yqm8si.xjx87ck.x1l7klhg.xs83m0k.x2lwn1j.xx8ngbg.xwo3gff.x1oyok0e.x1odjw0f.x1n2onr6.xq1qtft.xz401s1.x195bbgf.x3ir7cq.x1l19134.x1m0igow.x1ejq31n.xu3j5b3.x1q0q8m5.x26u7qi.x13lgxp2.x168nmei.xt8cgyo.xj515ic.x1co6499.x2j4hbs.x1ma7e2m.x9f619.x78zum5.xdt5ytf.x1iyjqo2.x6ikm8r.xy5w88m.xh8yej3.xbwb3hm.x1x3vcui.x19xvnzb.x1y4ox9t.xev1tu8.xpr2fh2.xgzc8be.x1iorvi4 > div.x78zum5.xdt5ytf.x1iyjqo2.x1n2onr6 > div.x7bdzy2.x1ktqkkm.x3i0t3l.x109q661.x1a8lsjc.xyamay9 > div.x6s0dn4.x78zum5.x14vqqas.xu0aao5 > div.x6s0dn4.x78zum5.x1c4vz4f.xykv574.x1i64zmx > div.x78zum5.x13a6bvl.xh8yej3 > div"); element1.click(); // console.log('點三個點'); await sleep(1000); // 封鎖 let element2 = document.querySelector("div.x1uvtmcs.x4k7w5x.x1h91t0o.x1beo9mf.xaigb6o.x12ejxvf.x3igimt.xarpa2k.xedcshv.x1lytzrv.x1t2pt76.x7ja8zs.x1n2onr6.x1qrby5j.x1jfb8zj > div > div > div > div > div:nth-child(3) > div > div:nth-child(1) > div.x6s0dn4.x78zum5.x1szn6h9.x1gu4hsp.x1n2onr6.x1qrby5j.x1vjfegm > span"); element2.click(); // console.log('封鎖'); await sleep(500); // 確定 let element3 = document.querySelector("div.x1i10hfl.xjbqb8w.xjqpnuy.xa49m3k.xqeqjp1.x2hbi6w.x13fuv20.xu3j5b3.x1q0q8m5.x26u7qi.x972fbf.xcfux6l.x1qhh985.xm0m39n.x9f619.x1ypdohk.x2lah0s.xe8uvvx.xdj266r.x11i5rnm.xat24cr.x1mh8g0r.x2lwn1j.xeuugli.xexx8yu.x4uap5.x18d9i69.xkhd6sd.x1n2onr6.x16tdsg8.x1hl2dhg.xggy1nq.x1ja2u2z.x1t137rt.x1o1ewxj.x3x9cwd.x13rtm0m.x1q0g3np.x87ps6o.x1lku1pv.x1a2a7pz.x6s0dn4.x78zum5.x1iyjqo2.x1dnml7z.xl56j7k.x18xmwgd.x12v9rci"); element3.click(); // console.log('確定'); await sleep(500); } // 返回 <- clickBack() // console.log('返回'); }); // 監聽按鈕button2點擊 button2.addEventListener('click', async function() { // 首頁 let element = document.querySelector("div.x78zum5.xdt5ytf.x1iyjqo2.xl56j7k.x1r0jzty > div:nth-child(1) > a > div.xefz13k.x1lq5wgf.xgqcy7u.x30kzoy.x9jhf4c.x5yr21d.xg01cxk.x10l6tqk.x13vifvy.x1gqj70i.x12w9bfk.x1g2r6go.x6o7n8i.x9lcvmn.xh8yej3.x1ja2u2z"); element.click(); // console.log('首頁'); }); // 監聽按鈕button3點擊 button3.addEventListener('click', async function() { // 返回 <- clickBack() // console.log('返回'); }); buttonPosition(); window.addEventListener('load', function() { // 每隔幾毫秒 setInterval(search, 300); setInterval(buttonPosition, 300); console.log("threads過濾已經開始運作"); }); // 按 ` 也可以封鎖 document.addEventListener('keydown', function(event) { if (event.key === '`') { button1.click(); } }); })(); ```