Prefer AMP CDN versions of pages (less ads, better adblock)

The AMP versions usually have less ads and are often better to use. You can use "back" to get the regular version - this script will not activate on "back" navigation.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Prefer AMP CDN versions of pages (less ads, better adblock)
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  The AMP versions usually have less ads and are often better to use. You can use "back" to get the regular version - this script will not activate on "back" navigation.
// @author       Ray Castro
// @match        http://*/*
// @match        https://*/*
// @exclude      https://rdrr.io/*
// @exclude      https://*.reddit.com/*
// @grant        none
// @run-at       document-body
// ==/UserScript==

(function() {
    'use strict';
    // Don't use this if the user used the "back" button, maybe it did not work.
    if (window.performance && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) return;
    // Avoid cycles:
    if (!window.location || window.location.hostname.indexOf("ampproject.org") >= 0) return;
    if (document.referrer && document.referrer.indexOf("amproject.org") >= 0) return;
    // Find an AMP URL:
    var burl;
    var links = document.getElementsByTagName('link');
	for (var i=0; i<links.length; i++) {
        var link = links[i];
        if (link.getAttribute("rel") == "amphtml") {
            burl = link.getAttribute("href");
            break;
        }
    }
    if (!burl) {
        var root = document.documentElement;
        if (root.hasAttribute("amp") ||  root.hasAttribute("\u26A1")) burl = window.location;
    }
    if (!burl) return;
    if (burl.startsWith("https://")) {
        window.location = "https://cdn.ampproject.org/c/s/" + burl.substring(8);
    } else if (burl.startsWith("http://")) {
        window.location = "https://cdn.ampproject.org/c/" + burl.substring(7);
    } else console.log("Neither http nor https?", burl);
})();