genweiwu / tampermonkeyscript Goto Github PK
View Code? Open in Web Editor NEW油猴脚本
License: MIT License
油猴脚本
License: MIT License
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);
};
})();
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.