Last active
May 14, 2024 14:59
-
-
Save weijarz/8677154a77aedb09738bd3621759c373 to your computer and use it in GitHub Desktop.
Twitter mute all ad accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Twitter: Mute all ads | |
// @namespace Violentmonkey Scripts | |
// @match https://twitter.com/* | |
// @grant none | |
// @version 1.0 | |
// @author @weijarz | |
// @description 7/5/2023, 9:31:41 PM | |
// ==/UserScript== | |
function muteAdUser(el) { | |
const article = el.closest('article'); | |
if (!article) return; | |
const btnArr = article.querySelectorAll('div[aria-haspopup="menu"][role="button"][data-testid="caret"]'); | |
if (btnArr.length !== 1) return; | |
if (article.style.background === 'red') return 'continue' | |
article.style.background = 'red'; | |
setTimeout(() => { | |
btnArr[0].click(); | |
setTimeout(() => { | |
const menuArr = document.querySelectorAll('div[data-testid="Dropdown"]'); | |
if (menuArr.length !== 1) return | |
for (const mi of menuArr[0].querySelectorAll('div[role="menuitem"]')) { | |
if (/^(Unfollow|取消关注)\s@/u.test(mi.innerText)) { | |
break; | |
} | |
if (/^(Mute|隐藏)\s@/u.test(mi.innerText)) { | |
console.log('Mute', mi.innerText); | |
mi.click(); | |
setTimeout(closeMuteAdDialog, 200); | |
setTimeout(closeMuteAdDialog, 500); | |
setTimeout(closeMuteAdDialog, 1000); | |
break; | |
} | |
} | |
}, 250); | |
}, 10); | |
} | |
function closeMuteAdDialog() { | |
const btn = document.querySelectorAll('div[data-testid="sheetDialog"] a+div[role="button"]') | |
if (btn.length !== 1) return | |
btn[0].click() | |
} | |
function muteAllAds() { | |
// 用户主页 mute 后广告推不会主动消失,会导致处理异常,所以路过 | |
if (!/^\/home/.test(location.pathname) && /^\/\w+$/.test(location.pathname)) return | |
const spans = document.querySelectorAll('div>span:first-child:last-child') | |
for (const span of spans) { | |
if (span.childElementCount === 0 && span.firstChild?.nodeType === 3 && /^(Ad|推荐)$/.test(span.firstChild.nodeValue)) { | |
if (muteAdUser(span) !== 'continue') { | |
break; // 因为涉及弹出菜单处理,无法批处理全部 | |
} | |
} | |
} | |
muting = false | |
} | |
document.addEventListener('keyup', ev => { | |
if (['INPUT', 'SELECT', 'TEXTAREA'].includes(ev.target.tagName)) return | |
if (ev.key === 'PageDown' || ev.key === ' ') { | |
lastScrollY = window.scrollY | |
setTimeout(muteAllAds, 500); | |
} | |
}); | |
let lastScrollY = 0 | |
setInterval(() => { | |
if (Math.abs(window.scrollY - lastScrollY) > 200) { | |
lastScrollY = window.scrollY; | |
muteAllAds(); | |
} | |
}, 1500); |
czwen
commented
Mar 22, 2024
如果能把这个dialog 自动关闭就完美了
@czwen 已经加上自动关闭功能。
@czwen 已经加上自动关闭功能。
👍
广告主是屏蔽不完的,且屏蔽帐号的体验不好,会引起页面闪烁和跳动。
推荐用另一个脚本: https://gist.github.com/weijarz/bd7ad3d8f5c8369c13efda568d19d224
不屏蔽帐号,只是隐藏广告。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment