Greasy Fork

suizhikuo-edit-xhr-header

js修改xhr请求的header

当前为 2024-04-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         suizhikuo-edit-xhr-header
// @namespace    http://tampermonkey.net/
// @version      2024-04-09
// @description  js修改xhr请求的header
// @author       随智阔
// @match	 *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @require      https://code.jquery.com/jquery-3.7.1.js
// @license MIT
// ==/UserScript==

(function () {
    'use strict';
    debugger;
    console.log("篡改猴(Tampermonkey)脚本--->运行");
    window.alert("篡改猴(Tampermonkey)脚本--->运行");

    // 原生js修改xhr请求header
    var originalOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
        this._headers = {};
        this.open = originalOpen.bind(this, method, url, async, user, password);

        var setRequestHeader = this.setRequestHeader;
        this.setRequestHeader = function (header, value) {
            this._headers[header.toLowerCase()] = value;
            return setRequestHeader.apply(this, arguments);
        };

        var send = this.send;
        this.send = function (body) {
            // 在这里添加你想要的header
            this.setRequestHeader("accept-language", "en-US");
            return send.apply(this, arguments);
        };

        this.open(method, url, async, user, password);
    };
})();