Baidu Netdisk UA Switcher

Force User-Agent to pan.baidu.com for specific domains

当前为 2025-03-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         Baidu Netdisk UA Switcher
// @namespace    https://greasyfork.org
// @version      1.1
// @description  Force User-Agent to pan.baidu.com for specific domains
// @author       Your Name
// @match        *://resource.qblb.net/*
// @match        *://pan.baidu.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 修正后的User-Agent
    const baiduUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 pan.baidu.com";

    // 检测浏览器兼容性
    if (typeof navigator.userAgent !== 'undefined') {
        try {
            // 尝试覆盖userAgent(部分浏览器支持)
            Object.defineProperty(navigator, 'userAgent', {
                value: baiduUA,
                writable: true
            });
            console.log(`[BaiduUA] User-Agent switched to: ${baiduUA}`);
        } catch (error) {
            console.error(`[BaiduUA] Failed to modify userAgent: ${error.message}`);
        }
    }

    // 补充其他关键请求头(可选)
    const headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
        'Referer': 'https://pan.baidu.com/'
    };

    // 覆盖XMLHttpRequest的open方法(需grant权限)
    const originalOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        this.setRequestHeader('User-Agent', baiduUA);
        for (const [key, value] of Object.entries(headers)) {
            this.setRequestHeader(key, value);
        }
        originalOpen.call(this, method, url, async, user, password);
    };

    // 覆盖fetch请求(需grant权限)
    window.fetch = async (...args) => {
        const [url, options] = args;
        const newOptions = {
            ...options,
            headers: {
                ...(options?.headers || {}),
                'User-Agent': baiduUA,
                ...headers
            }
        };
        return window.fetch(url, newOptions);
    };
})();