Mastodon View in Home Instance

Show a Mastodon-style banner to view profiles on your own instance (avoids broken post links)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Mastodon View in Home Instance
// @version      1.5
// @description  Show a Mastodon-style banner to view profiles on your own instance (avoids broken post links)
// @author       https://mastodon.online/@prvrtl
// @match        https://*/@*
// @grant        none
// @namespace https://greasyfork.org/users/1458606
// ==/UserScript==

(function () {
    const homeInstance = "mastodon.online";
    const currentHost = window.location.hostname;

    if (currentHost === homeInstance) return;

    const path = window.location.pathname;
    const usernameMatch = path.match(/^\/@([^\/]+)/); // only username part
    if (!usernameMatch) return;

    const username = usernameMatch[1];
    const federatedHost = currentHost.replace(/^www\./, "");
    const redirectUrl = `https://${homeInstance}/@${username}@${federatedHost}`;

    // Create styled banner
    const banner = document.createElement("div");
    banner.style.cssText = `
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        background: #282c37;
        color: white;
        padding: 12px 20px;
        font-family: system-ui, BlinkMacSystemFont, sans-serif;
        font-size: 15px;
        z-index: 9999;
        display: flex;
        justify-content: space-between;
        align-items: center;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
        border-bottom: 1px solid #444;
    `;

    const text = document.createElement("div");
    text.innerHTML = `🌍 You're viewing a Mastodon profile on <strong>${currentHost}</strong>.`;

    const button = document.createElement("button");
    button.textContent = `Open in ${homeInstance}`;
    button.style.cssText = `
        background: #6364ff;
        color: white;
        border: none;
        padding: 8px 16px;
        font-weight: 600;
        font-size: 14px;
        border-radius: 6px;
        cursor: pointer;
        transition: background 0.2s ease;
        box-shadow: inset 0 0 0 1px rgba(255,255,255,0.1);
    `;
    button.addEventListener("mouseover", () => {
        button.style.background = "#5253e6";
    });
    button.addEventListener("mouseout", () => {
        button.style.background = "#6364ff";
    });
    button.addEventListener("click", () => {
        window.location.href = redirectUrl;
    });

    banner.appendChild(text);
    banner.appendChild(button);
    document.body.style.paddingTop = "64px";
    document.body.prepend(banner);
})();