deAMP

垃圾 AMP,好走不送。

目前為 2024-02-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name               deAMP
// @name:zh-TW         deAMP
// @description        AMP sucks, thus no AMP thanks.
// @description:zh-TW  垃圾 AMP,好走不送。
// @author             Jason Kwok
// @namespace          https://jasonhk.dev/
// @version            2.2.1
// @license            MIT
// @match              http*://*/*
// @run-at             document_end
// @grant              GM.getValue
// @grant              GM.setValue
// @require            https://cdn.jsdelivr.net/npm/[email protected]/uuid-random.min.js
// @supportURL         https://greasyfork.org/scripts/450569/feedback
// ==/UserScript==

(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;
    }
})();