Tampermonkey 自定义脚本

78 阅读1分钟

好的,下面是一个用 Tampermonkey 自定义脚本的示例,用于拦截所有 POST 请求并在请求中添加 {"name":"test"} 作为参数。

Tampermonkey 脚本示例:

// ==UserScript==
// @name         Add custom parameter to POST requests
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add a custom parameter to all POST requests
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Hook into the fetch API to intercept POST requests
    const originalFetch = window.fetch;

    window.fetch = async function(url, options) {
        if (options && options.method === 'POST' && options.body) {
            // Handle different types of request bodies
            let body = options.body;

            // Check if the body is FormData (no need to modify it)
            if (body instanceof FormData) {
                console.log('FormData body detected, skipping modification.');
            } else {
                // Handle JSON body
                if (options.headers['Content-Type'] && options.headers['Content-Type'].includes('application/json')) {
                    try {
                        body = JSON.parse(body);  // Try to parse JSON body
                        body.name = 'test';  // Add the custom parameter
                        options.body = JSON.stringify(body);  // Reassign the modified body
                    } catch (e) {
                        console.error('Failed to parse JSON body', e);
                    }
                }
            }
        }

        // Call the original fetch function with the modified options
        return originalFetch(url, options);
    };

    // Hook into XMLHttpRequest to intercept POST requests made by older code
    const originalXhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        if (method === 'POST') {
            const originalSend = this.send;
            this.send = function(body) {
                if (body) {
                    // Handle JSON body for XMLHttpRequest
                    if (body.startsWith('{') && body.endsWith('}')) {
                        try {
                            const jsonBody = JSON.parse(body);
                            jsonBody.name = 'test';  // Add the custom parameter
                            body = JSON.stringify(jsonBody);
                        } catch (e) {
                            console.error('Failed to parse JSON body in XMLHttpRequest', e);
                        }
                    }
                }
                originalSend.call(this, body);
            };
        }
        originalXhrOpen.call(this, method, url, async, user, password);
    };
})();

解释:****

fetch:拦截了现代浏览器中使用的 fetch 请求。如果请求是 POST 且包含有效的请求体(通常是 JSON),它会解析请求体并添加 name: "test" 参数。

XMLHttpRequest:这个部分用于拦截老式的 XMLHttpRequest 请求。如果是 POST 请求,它会解析请求体(假设是 JSON 格式)并添加同样的参数。

使用方法:****

  1. 安装 Tampermonkey 插件。

  2. 点击 Tampermonkey 图标,选择 “Create a new script”。

  3. 删除默认模板,并将上面的脚本粘贴到编辑器中。

  4. 保存并启用脚本。

这个脚本会在所有的 POST 请求中添加 name: "test" 参数。你可以根据需要调整脚本,添加更多的逻辑或者修改其他类型的请求。