GitHub Images Accept-Language Modifier

修改发送给 GitHub 图片服务器的 Accept-Language 请求头,避免中文用户遇到 429 错误

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GitHub Images Accept-Language Modifier
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  修改发送给 GitHub 图片服务器的 Accept-Language 请求头,避免中文用户遇到 429 错误
// @author       Codming
// @match        *://*.github.com/*
// @grant        GM_xmlhttpRequest
// @run-at       document-start
// @connect      camo.githubusercontent.com
// @connect      avatars.githubusercontent.com
// ==/UserScript==

(function() {
    'use strict';
    
    // 拦截 XMLHttpRequest
    const originalXhrOpen = XMLHttpRequest.prototype.open;
    const originalXhrSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
    
    XMLHttpRequest.prototype.open = function(method, url) {
        this._url = url;
        return originalXhrOpen.apply(this, arguments);
    };
    
    XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
        // 检查 URL 是否匹配目标域名
        if (this._url && 
            (this._url.includes('camo.githubusercontent.com') || 
             this._url.includes('avatars.githubusercontent.com'))) {
            
            // 如果是 Accept-Language 头,修改值
            if (header.toLowerCase() === 'accept-language') {
                value = value.replace('zh-CN', 'en-US').replace('zh;', 'en;');
                console.log('Modified Accept-Language for:', this._url, 'New value:', value);
            }
        }
        
        return originalXhrSetRequestHeader.call(this, header, value);
    };
    
    // 拦截 fetch 请求
    const originalFetch = window.fetch;
    window.fetch = function(resource, init) {
        const url = resource instanceof Request ? resource.url : resource;
        
        if (url && 
            (url.includes('camo.githubusercontent.com') || 
             url.includes('avatars.githubusercontent.com'))) {
            
            init = init || {};
            init.headers = init.headers || {};
            
            // 创建修改后的 headers
            const newHeaders = new Headers(init.headers);
            
            if (newHeaders.has('accept-language')) {
                const langValue = newHeaders.get('accept-language');
                const newLangValue = langValue.replace('zh-CN', 'en-US').replace('zh;', 'en;');
                newHeaders.set('accept-language', newLangValue);
                console.log('Modified fetch Accept-Language for:', url, 'New value:', newLangValue);
            }
            
            // 如果 resource 是 Request 对象
            if (resource instanceof Request) {
                const newRequest = new Request(resource, {
                    headers: newHeaders
                });
                return originalFetch.call(this, newRequest, init);
            } else {
                // 如果是 URL 字符串
                init.headers = newHeaders;
                return originalFetch.call(this, resource, init);
            }
        }
        
        return originalFetch.apply(this, arguments);
    };
})();