Toogle beta version

Extension for foodsharing.de - toggles to beta version and back

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        Toogle beta version
// @namespace   Violentmonkey Scripts
// @match       https://*.foodsharing.*/*
// @run-at      document-idle
// @version     1.3
// @author      Martin G. (166111)
// @license     MIT
// @require     https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
// @description Extension for foodsharing.de - toggles to beta version and back
// ==/UserScript==

function isBeta() {
    return window.location.hostname.startsWith("beta.");
}

function initButton() {
    if (document.getElementById("fs-toggle-beta-btn")) return;

    // Try prod container first, then common header targets on beta, or fall back to body
    const targetContainer = 
        document.querySelector(".metanav-container") || 
        document.querySelector("header nav") || 
        document.querySelector("#nav") ||
        document.body;

    if (!targetContainer) return;

    const btn = document.createElement("button");
    btn.id = "fs-toggle-beta-btn";
    btn.innerHTML = isBeta() ? "prod" : "beta";

    // Basic styling so it remains visible if appended to body/header
    btn.style.marginLeft = "8px";
    btn.style.cursor = "pointer";

    btn.onclick = () => {
        const aktuelleURL = window.location.href;
        const betaMuster = /(\bhttps?:\/\/)(www\.)?/;
        let neueURL;

        if (isBeta()) {
            neueURL = aktuelleURL.replace("beta.", "");
        } else {
            neueURL = aktuelleURL.replace(betaMuster, "$1beta.");
        }

        window.location.href = neueURL;
    };

    targetContainer.appendChild(btn);
}

// Run initial attempt
initButton();

// Observe DOM changes in case Vue re-renders the navigation
const observer = new MutationObserver(() => {
    if (!document.getElementById("fs-toggle-beta-btn")) {
        initButton();
    }
});
observer.observe(document.body, { childList: true, subtree: true });