Skip to content

Instantly share code, notes, and snippets.

@picasso250
Last active January 20, 2024 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picasso250/8c9f1d1275a8760c90c3d426ca31ec0f to your computer and use it in GitHub Desktop.
Save picasso250/8c9f1d1275a8760c90c3d426ca31ec0f to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name lowlightRecommended-twitter
// @namespace Violentmonkey Scripts
// @match https://twitter.com/*
// @grant none
// @version 1.0
// @author -
// @description 2024/1/18 14:45:09
// ==/UserScript==
function applyStyles() {
// 获取所有的 span 元素
const allSpans = document.querySelectorAll('span');
// 遍历每个 span 元素
allSpans.forEach(span => {
// 检查 span 的文本内容是否为 "推荐" 或者以 "由 XXX 推广" 结尾
if (
span.textContent.trim() === '推荐' ||
/^由 .+ 推广$/.test(span.textContent.trim())) {
// 获取最近的父级 article 元素
const parentArticle = span.closest('article');
// 检查是否找到了父级 article 元素
if (parentArticle) {
parentArticle.style.opacity = '0.5';
}
}
});
}
// 每隔1秒执行applyStyles函数
setInterval(applyStyles, 1000);
@cweijan
Copy link

cweijan commented Jan 20, 2024

这个方式确实太暴力了, 个人感觉性能不好, 可以参考下面的监听模式

var observer = new MutationObserver(mutationList => {
    for (var mutation of mutationList) {
        for (var node of mutation.addedNodes) {
            if (!node.querySelectorAll) continue;
        }
    }
});

observer.observe(document, {
    childList: true,
    subtree: true
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment