Giter Site home page Giter Site logo

tampermonkeyscript's People

Contributors

genweiwu avatar

Watchers

 avatar  avatar  avatar

tampermonkeyscript's Issues

note

拦截ajax请求

https://blog.dqv5.com/2021/04/04/tampermonkey-request-intercept/

// ==UserScript==
// @name         百度搜索测试
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  代码功能:百度搜索123,返回456的搜索结果
// @author       You
// @match        https://www.baidu.com/*
// @grant        none
// ==/UserScript==

(function () {
    console.log('加载脚本');

    /**
     * 拦截并修改ajax请求
     */
    window.beforeXMLHttpRequestOpen = function (xhr, options) {
        console.log('before open', xhr);
        // 修改url
        options.url = options.url.replace('wd=123', 'wd=456');
        // 修改method
        // options.method = 'POST';
    };
    /**
     * 拦截并修改ajax请求
     */
    window.beforeXMLHttpRequestSend = function (xhr, body) {
        console.log('before send', xhr);
        // 修改请求头
        xhr.setRequestHeader('key1', 'value1');
    };

    /**
     * 重写open方法
     * https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/open
     */
    XMLHttpRequest.prototype.myOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
        // 用对象便于修改参数
        var options = {
            method: method,
            url: url,
            async: async,
            user: user,
            password: password
        };
        if ('function' === typeof window.beforeXMLHttpRequestOpen) {
            window.beforeXMLHttpRequestOpen(this, options);
        }
        this.myOpen(options.method, options.url, options.async);
    };

    /**
     * 重写send方法
     * https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/send
     */
    XMLHttpRequest.prototype.mySend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function (body) {
        if ('function' === typeof window.beforeXMLHttpRequestSend) {
            window.beforeXMLHttpRequestSend(this, body);
        }
        this.mySend(body);
    };

})();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.