跳到正文

个人记录

没什么用的东西

huyu123约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年7月27日
收录
2026年7月27日
更新
2026年10月11日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1
代码
// ==UserScript==
// @name         Cloudflare 验证加速脚本(Codeforces专用)
// @namespace    https://mrpython.top/
// @version      2.0
// @description  劫持XHR+Fetch,移除验证请求超时限制,解决转圈卡住问题
// @match        https://*.codeforces.com/*
// @match        https://challenges.cloudflare.com/*
// @match        https://*.challenges.cloudflare.com/*
// @match        https://*.atcoder.jp/*
// @run-at       document-start
// @grant        unsafeWindow
// ==/UserScript==

(function() {
    'use strict';

    // ================= 配置区 =================
    // 匹配 Cloudflare 验证接口的特征,无需修改
    const TARGET_URL_FEATURE = '/cdn-cgi/';
    // ==========================================

    const win = unsafeWindow || window;

    // ---------- 1. 劫持 XMLHttpRequest ----------
    const OriginalXHR = win.XMLHttpRequest;
    if (OriginalXHR) {
        const originalOpen = OriginalXHR.prototype.open;
        const originalSend = OriginalXHR.prototype.send;

        OriginalXHR.prototype.open = function(method, url) {
            this._hook_url = url ? String(url) : '';
            return originalOpen.apply(this, arguments);
        };

        OriginalXHR.prototype.send = function(body) {
            const url = this._hook_url;
            if (url && (url.includes(TARGET_URL_FEATURE) || url.includes('challenges.cloudflare.com'))) {
                console.log(`%c[XHR 拦截] 移除超时: ${url}`, 'color: #00ff00; font-weight: bold;');
                // 0 = 关闭超时限制,等价无限等待,兼容性最好
                this.timeout = 0;
            }
            return originalSend.apply(this, arguments);
        };
    }

    // ---------- 2. 劫持 Fetch(核心,新版CF验证主要用Fetch) ----------
    const originalFetch = win.fetch;
    if (originalFetch) {
        win.fetch = function(input, init = {}) {
            let url = '';

            // 兼容字符串 URL 和 Request 对象
            if (typeof input === 'string') {
                url = input;
            } else if (input && input.url) {
                url = input.url;
                // 合并 Request 对象参数,移除自带的超时中断信号
                init = Object.assign({}, input, init);
                delete init.signal;
                input = url;
            }
            url = String(url);

            if (url.includes(TARGET_URL_FEATURE) || url.includes('challenges.cloudflare.com')) {
                console.log(`%c[Fetch 拦截] 移除超时: ${url}`, 'color: #00ccff; font-weight: bold;');
                // 删除超时中断信号,防止前端主动断开请求
                if (init.signal) delete init.signal;
            }

            return originalFetch.call(win, input, init);
        };
    }

    console.log('%c[CF 辅助脚本] 劫持已就绪(XHR+Fetch)', 'color: #0088ff; font-weight: bold;');
})();

不是我写的

如果cf变成手机界面,输入codeforces.com/?mobile=false,然后就正常了

原文