Twitter old icon

Customize Twitter with a monkey theme

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Twitter old icon
// @namespace    http://tampermonkey.net/
// @version      4.1
// @description  Customize Twitter with a monkey theme
// @author       cygaar
// @match        https://twitter.com/*
// @match        https://x.com/*
// @icon         https://pbs.twimg.com/media/GGmfzX_bUAAUUFw?format=png
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    console.log('%cTwitter Old Icon:注入中', 'background: yellow; color: red;');
    const monkeyIconUrl = "https://pbs.twimg.com/media/GGmfzX_bUAAUUFw?format=png";

    // 設定網頁圖標
    function setFavicon() {
        const favicon = document.querySelector("link[rel~='icon']");
        if (!favicon) {
            const link = document.createElement("link");
            link.rel = "icon";
            link.href = monkeyIconUrl;
            document.head.appendChild(link);
        } else if (favicon.href !== monkeyIconUrl) {
            favicon.href = monkeyIconUrl;
        }
    }

    // 設定 Web 應用程式圖示
    function setWebAppIcon() {
        const appIcon = document.querySelector('[aria-label="X"]');
        if (appIcon) {
            const container = appIcon.children[0];
            if (container) {
                container.innerHTML = ""; // 清空內容
                const monkeyIcon = document.createElement("img");
                monkeyIcon.src = monkeyIconUrl;
                monkeyIcon.width = 42;
                monkeyIcon.height = 42;
                container.appendChild(monkeyIcon);
            }
        }
    }

    // 等待特定元素出現
    function waitForElement(selector, callback) {
        const element = document.querySelector(selector);
        if (element) {
            callback(element);
            return;
        }

        const observer = new MutationObserver(() => {
            const element = document.querySelector(selector);
            if (element) {
                callback(element);
                observer.disconnect();
            }
        });

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

    // 初始化
    setFavicon();
    setWebAppIcon();

    // 每隔五秒檢查一次 Web 應用程式圖示
    setInterval(setWebAppIcon, 5000);

    // 監控頁面加載過程並更新 favicon
    waitForElement("head", setFavicon);
    waitForElement('[aria-label="X"]', setWebAppIcon);

    // 更改網站標題
    const titleObserver = new MutationObserver(() => {
        const title = document.querySelector('title');
        if (title && title.innerText.endsWith('X')) {
            title.innerText = title.innerText.slice(0, -1) + 'Twitter';
        }
    });

    waitForElement('title', (title) => titleObserver.observe(title, { childList: true }));
})();