Toogle beta version

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

let btn = document.createElement("BUTTON");
let div = document.querySelector(".metanav-container");
div.appendChild(btn);

btn.innerHTML = "beta";
if(isBeta()){
    btn.innerHTML = "prod";
}

btn.onclick = () => {
    const aktuelleURL = window.location.href;
    const betaMuster = /(\bhttps?:\/\/)(www\.)?/; // Suchmuster für http(s):// und optional www.
    const betaVorhanden = aktuelleURL.includes("beta.");
    let neueURL;

    if (isBeta()) {
        btn.innerHTML = "beta";
        neueURL = aktuelleURL.replace("beta.", ""); // switch to prod
    } else {
        btn.innerHTML = "prod";
        neueURL = aktuelleURL.replace(betaMuster, "$1beta."); // switch to beta
    }

    window.location.href = neueURL;
};

function isBeta() {
    const aktuelleURL = window.location.href;
    return aktuelleURL.includes("beta.");
}