deAMP

AMP sucks, thus no AMP thanks.

// ==UserScript==
// @name               deAMP
// @name:zh-TW         deAMP
// @name:zh-CN         deAMP
// @description        AMP sucks, thus no AMP thanks.
// @description:zh-TW  垃圾 AMP,好走不送。
// @description:zh-CN  垃圾 AMP,好走不送。
// @icon               data:image/webp;base64,UklGRhoKAABXRUJQVlA4WAoAAAAQAAAAfwAAfwAAQUxQSC8EAAABoAQAkCFJimrNrG3btm3btm3btm3btm1bY7Qr49AzNZEV9dtHREwA/HXRkqZw3X7z9996F+AUzoC3N/fP71u3UBqLQZjjVZh792uYG7W6w77enVcxvpm9jO13fEfK7zs6ZmJMiV3qQJAHqT3BB8vFUXhKNOC6inKqtwYnYihp188qyit+9EnOjLXzM4Fyixc9rIwoRU8K1OHF0goXsWcEoz5D58fjofA51O+VEgxYmv5GPQe3tejNf5YL9e1e4KevRNsF6l0cSaanxGeRQXE1lX7S3kAeb6fXS+Jrggl8lFofic8in+cS6cG2XTCCR2LLZ5mJrHqnK9I1dvGCaitFskK/kdvvueSKfQ75PR9bqunIsNjeonQsaYqFcISI6rvmJjmsp5BtT19Fii6CL7QXliHZC+R8rQw9VdaemegSfUXWP5vJlCGCt5smsri3kfcJQF5O8PYnGd1x5P2aQpY1hLm3yci6eZlzVaAy70fuF1IlCmDvi5WoDrLvLUm0mj8xjMb/CX+4w0KS8YcB3I1LUjLSAP4kJ2nuNQDMSzICjbAtyWpDmE1yyhAOkjwxhLskXwzhjY0izBC+xKHwGsLvhBTCEIKTUHgMISQpRaghBCeh+GwIvxJQPDWET7EpzhjCCwvFekO4BpTjDGETSVvVCEaQVHAYQRWSHAEGYE9HkuCtAbxMQAJHDeCIjaYXf2KaQpPNzZ5aE2htD9gLiEukzBLcbQDq6k7mPDXI4n9k7kNKMpjC3EYTXYZg1rzFgd62l7UzigRQR2XM2QBkVC4z9iS+FFDJwVdjkDP2GbbO2iSBki6mnLVA2oWCpy2KPBk+svQ7DUjcVOXIXlUmyxrBEP4uKhGkeMURfsoqERT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LPpq+dPo/c73R5jZTDGfL0Qf8MJO5kX69AUPNHE80Wz5VaH5rP59ZOjN2F70LFEdga5IBhaoCdTiqbIBRLWaElKaKvFGUCtVTjW58XneiZxgbKeew0Ce6mXnejJT+w9xCfSn279gdfzN+g2Asiqv8OXL3J4sGQ8u+632zSbCepuVKS4wYTnDgPONmN0vpGogZ6a+TImUDCBH505OS/t8G687fI2+tDs4D2ZcjF/dZtQRMDt+E1Wnt9MzgSVNYAe2w+TZEhGdcWEplsKd57igV6wvdurGLMXei4vEvbqiBAAC9nCjGkrkF7PnRW9DOeu0/OpkIvd/oHAKAEXDe3y7WdOOUXzJpk55gPKEH1mPaMr2Yk0qxD038WAAA=
// @author             Jason Kwok
// @namespace          https://jasonhk.dev/
// @version            2.3.2
// @license            MIT
// @match              *://*/*
// @exclude-match      *://web.archive.org/web/*
// @run-at             document-end
// @grant              GM.getValue
// @grant              GM.setValue
// @grant              GM.deleteValue
// @grant              GM.registerMenuCommand
// @require            https://cdn.jsdelivr.net/npm/uuid-random@1.3.2/uuid-random.min.js
// @supportURL         https://greasyfork.org/scripts/450569/feedback
// ==/UserScript==

let t;
{
    const translations =
    {
        "en": {
            COMMAND: "Reset Session Key",
            MESSAGE: "Session key has been reseted.",
        },
        "zh-TW": {
            COMMAND: "重設工作階段密鑰",
            MESSAGE: "工作階段密鑰已被重設。",
        },
    };

    let locale = "en";
    for (let _locale of navigator.languages.map((language) => new Intl.Locale(language)))
    {
        if (_locale.language === "zh")
        {
            _locale = new Intl.Locale("zh", { region: _locale.maximize().region });
        }
;
        if (_locale.baseName in translations)
        {
            locale = _locale.baseName;
            break;
        }
    }

    t = translations[locale];
}

(async () =>
{
    const SESSION_KEY = await getSessionKey();

    const isAmp = document.documentElement.hasAttribute("⚡") || document.documentElement.hasAttribute("amp");
    const canonical = document.head.querySelector("link[rel=canonical][href]");

    if (isAmp && (canonical !== null))
    {
        const lastVisit = sessionStorage.getItem(SESSION_KEY);

        if (location.href === lastVisit)
        {
            console.debug("[deAMP] Last visited URL is the current URL, abort redirection.");
            sessionStorage.removeItem(SESSION_KEY);
        }
        else if (location.href === canonical.href)
        {
            console.debug("[deAMP] Canonical URL is the current URL, abort redirection.");
            sessionStorage.removeItem(SESSION_KEY);
        }
        else
        {
            console.debug(`[deAMP] Redirecting to canonical URL: ${canonical.href}`);

            sessionStorage.setItem(SESSION_KEY, location.href);
            location.replace(canonical.href);
        }
    }
    else
    {
        console.debug("[deAMP] Not an AMP page.");
        sessionStorage.removeItem(SESSION_KEY);
    }

    async function getSessionKey()
    {
        let key = await GM.getValue("SESSION_KEY");
        if (key) { return key; }

        key = uuid();
        GM.setValue("SESSION_KEY", key);
        return key;
    }
})();

GM.registerMenuCommand?.(t.COMMAND, () =>
{
    setTimeout(async () =>
    {
        await GM.deleteValue("SESSION_KEY");
        alert(t.MESSAGE);
    }, 0);
});