Toogle beta version

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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.");
}