Greasy Fork

Greasy Fork is available in English.

Twitter to Mastodon Redirector

Redirects to Mastodon if the Twitter user has a Mastodon account associated with them.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Twitter to Mastodon Redirector
// @namespace    http://your-domain-here/
// @version      1
// @description  Redirects to Mastodon if the Twitter user has a Mastodon account associated with them.
// @match        *://twitter.com/*
// @grant        GM_xmlhttpRequest
// @license MIT
// ==/UserScript==
const HOME_SERVER = 'mastodon.social';

(function() {
    'use strict';

    // Get the Twitter username from the URL
    function getUsername() {
        const match = window.location.pathname.match(/^\/([a-zA-Z0-9_]+)/);
        if (match) {
            return match[1];
        }
        return null;
    }

function checkForMastodon(username) {
    const commonWords = ['home', 'explore', 'notification', 'i', 'message', 'settings','search'];
    if (commonWords.includes(username.toLowerCase())) {
        removeMastodonButton();
        return;
    }
    const apiURL = `https://t2m.ooyeep.uk/api?username=${username}`;
    GM_xmlhttpRequest({
        method: 'GET',
        url: apiURL,
        onload: function(response) {
            const data = JSON.parse(response.responseText);
            if (data.status === 'success' && data.mastodon_domain && data.mastodon_username) {
                createMastodonButton(data.mastodon_domain, data.mastodon_username);
            } else {
                removeMastodonButton();
            }
        }
    });
}

    // Create the Mastodon redirect button component
    function createMastodonButton(domain, username) {
        let button = document.getElementById('mastodon-button');
        if (!button) {
            button = document.createElement('div');
            button.id = 'mastodon-button';
            button.style = 'position: fixed; top: 0; left: 50%; transform: translateX(-50%); z-index: 9999;';
            document.body.appendChild(button);
        }
        button.innerHTML = `
            <a href="https://${HOME_SERVER}/@${username}@${domain}" target="_blank">
                <span class="text" style="color: white; background-color: hotPink; padding: 12px 24px; border-radius: 25px;">Follow on Mastodon</span>
            </a>
        `;
    }

    // Remove the Mastodon redirect button component
    function removeMastodonButton() {
        const button = document.getElementById('mastodon-button');
        if (button) {
            button.remove();
        }
    }

    // Check for Mastodon account periodically
    let username = getUsername();
    setInterval(function() {
        const newUsername = getUsername();
        if (newUsername && newUsername !== username) {
            username = newUsername;
            checkForMastodon(username);
        }
    }, 1000);
})();