Greasy Fork is available in English.

Cloudflare CDN检测脚本

根据网页标头中的Server字段判断是否使用Cloudflare CDN,并在页面右上角显示相应内容,方便用于IP优选站点识别。

// ==UserScript==
// @name         Cloudflare CDN检测脚本
// @version      1.0
// @author       PoppyGuy
// @description  根据网页标头中的Server字段判断是否使用Cloudflare CDN,并在页面右上角显示相应内容,方便用于IP优选站点识别。
// @match        *://*/*
// @grant        GM_setClipboard
// @icon         https://www.cloudflare-cn.com/favicon.ico
// @license MIT
// @namespace https://greasyfork.org/users/1304869
// ==/UserScript==

(function() {
    'use strict';

    let headersCache = {}; // 缓存标头信息

    function checkCDNStatus() {
        if (headersCache[window.location.href]) {
            processServerHeader(headersCache[window.location.href]);
        } else {
            let xhr = new XMLHttpRequest();
            xhr.open('HEAD', window.location.href, true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    headersCache[window.location.href] = xhr.getAllResponseHeaders();
                    processServerHeader(headersCache[window.location.href]);
                }
            };
            xhr.send();
        }
    }

    function processServerHeader(headers) {
        let serverHeader = headers.match(/Server: (.*)/i);
        if (serverHeader && serverHeader[1].toLowerCase().includes('cloudflare')) {
            showCDNStatus(true);
        } else {
            showCDNStatus(false);
        }
    }

    function showCDNStatus(isUsingCDN) {
        removeCDNStatus(); // 清除之前的状态

        let statusDiv = document.createElement('div');
        statusDiv.style.cssText = "position: fixed; top: 10px; right: 10px; padding: 20px; border-radius: 10px; font-weight: bold; z-index: 9999; font-size: 24px; text-align: center; color: #fff;";

        if (isUsingCDN) {
            statusDiv.style.background = 'linear-gradient(45deg, #00FF42, #74F3D4, #045DE9, #A43FC1, #FF0080, #FF2626)';
            statusDiv.textContent = '使用CDN加速';
        } else {
            statusDiv.style.backgroundColor = 'red';
            statusDiv.textContent = '未使用CDN加速';
        }

        statusDiv.addEventListener('click', function() {
            GM_setClipboard(window.location.hostname);
            statusDiv.style.backgroundColor = 'gold';
        });

        document.body.appendChild(statusDiv);
    }

    function removeCDNStatus() {
        let existingStatusDiv = document.querySelector('.cdn-status');
        if (existingStatusDiv) {
            existingStatusDiv.remove();
        }
    }

    checkCDNStatus();
})();