Nitterify

Turns all twitter.com links into nitter.net links

当前为 2022-12-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         Nitterify
// @namespace    *
// @version      0.1
// @description  Turns all twitter.com links into nitter.net links
// @author       DarkWiiPlayer
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=nitter.net
// @grant        none
// @license      Unlicense
// ==/UserScript==

(function() {
    'use strict';

    /* Inject ✨magic✨ to get location change events */
    const old = { pushState: history.pushState, replaceState: history.replaceState }
    history.pushState = function(...params) {
        old.pushState.call(history, ...params)
        window.dispatchEvent(new CustomEvent("locationchange"))
    }
    history.replaceState = function(...params) {
        old.replaceState.call(history, ...params)
        window.dispatchEvent(new CustomEvent("locationchange"))
    }
    window.addEventListener("popstate", event => {window.dispatchEvent(new CustomEvent("locationchange"))})

    const stylesheet = `
        div {
            position: fixed;
            color: white;
            left: 0;
            bottom: 0; /* this is me */
            padding: 1em;
            transition: opacity .3s;
        }
        .hidden {
            opacity: 0;
        }
        a {
            color: inherit;
            text-decoration: none;
        }
    `

    const nitterify = link => {
        const url = new URL(link)
        if (url.host == "twitter.com") {
            url.host = "nitter.net"
        }
        return url
    }

    const nitterifyTree = subtree => {
        subtree.querySelectorAll(`a[href]`).forEach(link => {
            link.href = nitterify(link.href)
        })
    }

    const observer = new MutationObserver(list => {
        list.forEach(mutation => {
            console.log(mutation)
            mutation.addedNodes.forEach(nitterifyTree)
        })
    })

    const blacklist = new Set([
        "home"
    ])

    const url = new URL(location)
    if (url.host == "twitter.com") {
        const nitterButton = document.createElement("nitter-button")
        const shadow = nitterButton.attachShadow({mode: "open"})
        url.host = "nitter.net"
        shadow.innerHTML = `<style>${stylesheet}</style><div><a href="${url}">View on <b>Nitter</b></a></div>`
        console.log(nitterButton)
        document.body.append(nitterButton)
        const update = () => {
            shadow.querySelector("a").href = nitterify(location)
            const div = shadow.querySelector("div")
            const page = new URL(location).pathname.match(/[^/]+/)[0]
            if (blacklist.has(page)) {
                div.classList.add("hidden")
            } else {
                div.classList.remove("hidden")
            }
        }
        window.addEventListener('locationchange', update)
        update()
    } else {
        observer.observe(document, {childList: true, subtree: true})
        nitterifyTree(document)
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址