Skip to content

Instantly share code, notes, and snippets.

@daoleno
Last active June 28, 2023 09:58
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 daoleno/602adb48dfb830239b8a529340a6977a to your computer and use it in GitHub Desktop.
Save daoleno/602adb48dfb830239b8a529340a6977a to your computer and use it in GitHub Desktop.
通过关键字模糊推文

Twitter Blur Keywords

通过关键字模糊推文

效果:https://twitter.com/dao_leno/status/1673992286009720832?s=20

Arc

复制这段 JavaScript 代码到 Arc 浏览器 Boost -> Code -> JS 代码框

const keywords = ["keyword1", "keyword2", "keyword3"];
const observer = new MutationObserver(handleMutation);
let hoverTimeout;
const hoverDuration = 3000; // Customize the hover duration (in milliseconds)
const blurValue = "5px"; // Customize the blur value

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

function handleMutation(mutationsList) {
  for (const mutation of mutationsList) {
    if (mutation.type === "childList") {
      mutation.addedNodes.forEach(addedNode => {
        if (addedNode instanceof HTMLElement) {
          const tweetTextElements = addedNode.querySelectorAll(
            '[data-testid="tweet"]'
          );
          Array.from(tweetTextElements).forEach(element => {
            keywords.forEach(keyword => {
              if (element.textContent.includes(keyword)) {
                element.style.filter = `blur(${blurValue})`;
                element.addEventListener("mouseenter", handleMouseEnter);
                element.addEventListener("mouseleave", handleMouseLeave);
              }
            });
          });
        }
      });
    }
  }
}

function handleMouseEnter() {
  const element = this;
  hoverTimeout = setTimeout(() => {
    element.style.filter = "none";
  }, hoverDuration);
}

function handleMouseLeave() {
  clearTimeout(hoverTimeout);
  const element = this;
  element.style.filter = `blur(${blurValue})`;
}

Chrome & Firefox

安装油猴插件

Chrome Firefox

创建新脚本,复制粘贴以下内容并保存

// ==UserScript==
// @name         Twitter Blur Keywords
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Blur tweets containing specified keywords on Twitter
// @author       daoleno
// @match        https://twitter.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const keywords = ["keyword1", "keyword2", "keyword3"];
    const blurValue = "5px"; // Customize the blur value

    const tweetContainerSelector = '[data-testid="tweet"]';

    function handleMutation(mutation) {
        const addedNodes = mutation.addedNodes;
        for (const addedNode of addedNodes) {
            if (addedNode instanceof HTMLElement) {
                const tweetTextElements = addedNode.querySelectorAll(tweetContainerSelector);
                Array.from(tweetTextElements).forEach(element => {
                    keywords.forEach(keyword => {
                        if (element.textContent.includes(keyword)) {
                            element.style.filter = `blur(${blurValue})`;
                        }
                    });
                });
            }
        }
    }

    const observer = new MutationObserver(mutationsList => {
        for (const mutation of mutationsList) {
            if (mutation.type === "childList") {
                handleMutation(mutation);
            }
        }
    });

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

配置

const keywords = ["keyword1", "keyword2", "keyword3"]; // Customize keywords
const hoverDuration = 3000; // Customize the hover duration (in milliseconds)
const blurValue = "5px"; // Customize the blur value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment