使用 Tampermonkey 在页面加载完5秒后监听特定页面元素的点击事件并修改 API 返回的数据
示例
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 2024-07-04
// @description try to take over the world!
// @author You
// @match https://a.x.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=zhonganonline.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
console.log('Tampermonkey 脚本已启动');
// Your code here...
// 拦截 XMLHttpRequest
(function(open) {
console.log('XMLHttpRequest 0');
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
//console.log('XMLHttpRequest 1');
this.addEventListener("readystatechange", function() {
//console.log('XMLHttpRequest 2');
//console.log('XMLHttpRequest 3',this.readyState,'---',this.status);
if (this.readyState === 4 && this.status === 200) {
// 仅拦截特定的 API 请求,例如 https://api-endpoint.com/data
console.log('XMLHttpRequest 4---------------------------',url);
if (url.includes('/ide/api/lineage/taskLineage')) {
console.log('原始响应数据:', this.responseText);
// 解析返回的 JSON 数据
let response = JSON.parse(this.responseText);
//let data = response;
// 修改数据
//response.success = 'modifiedValue1';
//var result = replaceStringInObject(data, 'pub_cdm_oneid_v2_', '');
console.log('response------:', response.data[0].nodes[1].msg.docName);
// 手动替换 JSON 数据中的特定字段值
response.data[0].nodes[0].msg.docName = response.data[0].nodes[0].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[1].msg.docName = response.data[0].nodes[1].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[2].msg.docName = response.data[0].nodes[2].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[3].msg.docName = response.data[0].nodes[3].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[4].msg.docName = response.data[0].nodes[4].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[5].msg.docName = response.data[0].nodes[5].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[6].msg.docName = response.data[0].nodes[6].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[7].msg.docName = response.data[0].nodes[7].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[8].msg.docName = response.data[0].nodes[8].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[9].msg.docName = response.data[0].nodes[9].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[10].msg.docName = response.data[0].nodes[10].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
response.data[0].nodes[11].msg.docName = response.data[0].nodes[11].msg.docName.replace('pub_edw_', '').replace('pub_adm_', '').replace('oneid_v2_', '').replace('oneid_', '').replace('pub_cdm_', '').replace('adm_', '');
// 创建新的响应文本
Object.defineProperty(this, 'responseText', { value: JSON.stringify(response) });
//Object.defineProperty(this, 'responseText', { value: JSON.stringify(result) });
console.log('修改后的响应数据:', this.responseText);
}
}
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
// 监听特定页面元素的点击事件
function addEventListeners() {
// 假设按钮的 ID 是 'targetButton'
var button = document.querySelector("#task-table > div > div > div > div > div.ant-table-fixed-right > div.ant-table-body-outer > div > table > tbody > tr > td > div > a:nth-child(6)");
if (button) {
button.addEventListener('click', function() {
console.log('按钮被点击,准备修改 API 返回的数据');
// 这里可以添加代码来进一步修改 API 返回的数据,例如重新发送请求等
});
} else {
console.error('找不到指定的按钮');
}
}
// 确保页面加载完毕后添加事件监听器
window.addEventListener('load', function() {
setTimeout(function() {
addEventListeners();
}, 5000); // 5000 毫秒 = 5 秒
});
})();