Twitter old icon

Customize Twitter with a monkey theme

// ==UserScript==
// @name         Twitter old icon
// @namespace    http://tampermonkey.net/
// @version      2
// @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;');
    // 設定網頁圖標
    document.querySelector("link[rel~='icon']").href = "https://pbs.twimg.com/media/GGmfzX_bUAAUUFw?format=png";

    function waitForElement(selector) {
        return new Promise(resolve => {
            if (document.querySelector(selector)) {
                return resolve(document.querySelector(selector));
            }

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

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

    // 設定 loading 頁面圖示
    waitForElement('[aria-label="Loading…"]').then((container) => {
        // 竄改猴腳本
        container.innerHTML = "載入中...";
    });

    // 設定 Web 應用程式圖示
    waitForElement('[aria-label="X"]').then((elm) => {
        const container = elm.children[0];
        container.innerHTML = "";

        const monkeyIcon = document.createElement("img");
        monkeyIcon.src = "https://pbs.twimg.com/media/GGmfzX_bUAAUUFw?format=png";
        monkeyIcon.width = 42;
        monkeyIcon.height = 42;
        container.appendChild(monkeyIcon);
    });

    // 更改網站標題
    const titleObserver = new MutationObserver((mutationList) => {
        for (const mutation of mutationList) {
            if (mutation.addedNodes.forEach(node => {
                const title = document.querySelector('title');
                if (node.textContent.endsWith('X')) title.innerText = title.innerText.slice(0, -1) + 'Twitter';
            }));
        }
    });

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

})();