Skip to content

Instantly share code, notes, and snippets.

@i-sync
Created February 4, 2021 16:38
Show Gist options
  • Save i-sync/64f5e59607a796cb2a8334aa8ad7423c to your computer and use it in GitHub Desktop.
Save i-sync/64f5e59607a796cb2a8334aa8ad7423c to your computer and use it in GitHub Desktop.
Dueros Developer Name Updated - Tampermonkey Script
// ==UserScript==
// @name Dueros Developer Name Updated
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Mike
// @run-at document-start
// @match https://dueros.baidu.com/dbp/bot/index
// @require https://unpkg.com/[email protected]/dist/ajaxhook.min.js
// @grant MIT
// ==/UserScript==
(function() {
'use strict';
//Method 1
/*
var _open = XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function (method, URL) {
var _onreadystatechange = this.onreadystatechange,
_this = this;
_this.onreadystatechange = function () {
// catch only completed 'api/search/universal' requests
if (_this.readyState === 4 && _this.status === 200 && ~URL.indexOf('dbp/bot/getversion')) {
try {
//////////////////////////////////////
// THIS IS ACTIONS FOR YOUR REQUEST //
// EXAMPLE: //
//////////////////////////////////////
var data = JSON.parse(_this.responseText); // {"fields": ["a","b"]}
console.log(data);
if (data && data.data && data.data.developer) {
data.data.developer = "MT";
}
// rewrite responseText
Object.defineProperty(_this, 'responseText', {value: JSON.stringify(data)});
/////////////// END //////////////////
} catch (e) {}
//console.log('Caught! :)', method, URL/, _this.responseText/);
}
// call original callback
if (_onreadystatechange) _onreadystatechange.apply(this, arguments);
};
// detect any onreadystatechange changing
Object.defineProperty(this, "onreadystatechange", {
get: function () {
return _onreadystatechange;
},
set: function (value) {
_onreadystatechange = value;
}
});
return _open.apply(_this, arguments);
};*/
//Method 2
ah.proxy({
//请求发起前进入
onRequest: (config, handler) => {
//console.log(config.url);
//if(config.url.match(/\/dbp\/bot\/(getversion|getuserinfo)/)) {
handler.next(config);
//}
},
//请求发生错误时进入,比如超时;注意,不包括http状态码错误,如404仍然会认为请求成功
onError: (err, handler) => {
//console.log(err.type);
handler.next(err);
},
//请求成功后进入
onResponse: (response, handler) => {
if(response.config.url.indexOf('/dbp/bot/getversion') > -1) {
console.log("url:", response.config.url);
var res = JSON.parse(response.response);
console.log(res);
if (res && res.data && res.data.developer) {
res.data.developer = "MT";
}
// rewrite response
response.response = JSON.stringify(res);
}
else if(response.config.url.indexOf('/dbp/bot/getuserinfo') > -1) {
console.log("url:", response.config.url);
var res = JSON.parse(response.response);
console.log(res);
if (res && res.data && res.data.passName) {
res.data.passName = "MT";
}
// rewrite response
response.response = JSON.stringify(res);
}
handler.next(response);
}
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment