Duanmao HTTPS

使断锚网页版支持 HTTPS

目前為 2021-08-28 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Duanmao HTTPS
// @namespace    CCCC_David
// @version      0.2.1
// @description  使断锚网页版支持 HTTPS
// @author       CCCC_David
// @match        https://duanmao.top/*
// @run-at       document-start
// @grant        none
// @require      https://unpkg.com/ajax-hook/dist/ajaxhook.min.js
// ==/UserScript==

(() => {
    'use strict';

    const fixURL = (url) => url === undefined ? undefined : url.replace(/^https?:\/\/cdn1\.duanmao\.top\//i, 'https://duanmao.top/')
                                                               .replace(/^http:\/\//i, 'https://');
    ah.proxy({
        onRequest: (config, handler) => {
            config.url = fixURL(config.url);
            handler.next(config);
        },
        onError: (err, handler) => {
            handler.next(err);
        },
        onResponse: (response, handler) => {
            handler.next(response);
        },
    });

    const isForumPage = window.location.pathname === '/forum';

    const addMetaTags = () => {
        if (isForumPage) {
            return;
        }
        const metaReferrer = document.createElement('meta');
        metaReferrer.name = 'referrer';
        metaReferrer.content = 'same-origin';
        document.head.appendChild(metaReferrer);
        const metaCSP = document.createElement('meta');
        metaCSP.httpEquiv = 'Content-Security-Policy';
        metaCSP.content = "base-uri 'self'; script-src 'self'; object-src 'none'; upgrade-insecure-requests";
        document.head.appendChild(metaCSP);
    };

    const updateElementURL = (el) => {
        const tagName = el.nodeName.toLowerCase();
        // There are many HTML tag attributes which can include URLs
        // (see https://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value),
        // but here we just deal with the most common two cases:
        // - <a href="..."></a>
        // - <img src="..." />
        if (tagName === 'a') {
            el.href = fixURL(el.href);
        } else if (tagName === 'img') {
            el.src = fixURL(el.src);
        }
    };

    const observer = new MutationObserver((mutationsList) => {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList') {
                for (const el of mutation.addedNodes) {
                    if (el.nodeName.toLowerCase() === 'head') {
                        addMetaTags();
                    }
                    updateElementURL(el);
                }
            }
        }
    });

    observer.observe(document.documentElement, {
        subtree: true,
        childList: true,
    });

    if (document.head) {
        addMetaTags();
    }

    for (const tagName of ['a', 'img']) {
        for (const el of document.getElementsByTagName(tagName)) {
            updateElementURL(el);
        }
    }

    const insertScriptInline = (script) => {
        const el = document.createElement('script');
        el.appendChild(document.createTextNode(script));
        document.body.appendChild(el);
    };

    const insertScriptSrc = (src) => {
        const el = document.createElement('script');
        el.src = src;
        document.body.appendChild(el);
    };

    if (isForumPage) {
        document.documentElement.innerHTML = `
            <!DOCTYPE html>
            <html lang="zh-CN">
                <head>
                    <meta charset="utf-8">
                    <meta http-equiv="X-UA-Compatible" content="IE=edge">
                    <meta name="referrer" content="same-origin">
                    <meta http-equiv="Content-Security-Policy" content="base-uri 'self'; script-src 'self' 'sha256-tjTO3rQxUkgLj//lUhUlsDm7/uSQn+aGhV+Lb2IwohA='; object-src 'none'; upgrade-insecure-requests">
                    <title>断锚</title>
                    <link rel="stylesheet" href="/static/index.d0f128df.css">
                </head>
                <body><noscript><strong>Please enable JavaScript to continue.</strong></noscript>
                    <div id="app"></div>
                </body>
            </html>
        `;
        insertScriptInline(`
            (() => {
                window.coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'));
                const metaViewport = document.createElement('meta');
                metaViewport.name = 'viewport';
                metaViewport.content = 'width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (window.coverSupport ? ', viewport-fit=cover' : '');
                document.head.appendChild(metaViewport);
            })();
        `);
        insertScriptSrc('/static/js/chunk-vendors.6e103c75.js');
        insertScriptSrc('/static/js/index.37f94612.js');
    }

    console.log('Duanmao HTTPS script is loaded');
})();