您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Greasy Fork is available in English.
Modify specific fetch responses on a given page.
当前为
// ==UserScript== // @name 墨水屏Rlog按time重排 // @namespace http://tampermonkey.net/ // @version 1.0 // @description Modify specific fetch responses on a given page. // @author You // @match https://log-search-docker.zuoyebang.cc/explore // @grant none // @license MIT // ==/UserScript== (function () { 'use strict'; const originalFetch = window.fetch; const log = (...args) => console.log('😜', ...args); function get(obj, path, defaultValue = undefined) { const keys = Array.isArray(path) ? path : path.split('.'); let result = obj; for (const key of keys) { result = result ? result[key] : undefined; if (result === undefined) { return defaultValue; } } return result; } function set(obj, path, value) { const keys = Array.isArray(path) ? path : path.split('.'); let current = obj; keys.forEach((key, index) => { if (index === keys.length - 1) { current[key] = value; } else { current[key] = current[key] || {}; current = current[key]; } }); return obj; } const TARGET_APP_AUTH = ['zphybrid-log-debug', 'zphybrid-log'] const authKey = (() => { const queryString = window.location.search; const urlParams = new URLSearchParams(queryString); // 获取特定参数 const left = urlParams.get('left'); // 'John' log(left) if (!left) { return '' } const qsObjRList = JSON.parse(left) // log(qsObjR) const target = qsObjRList.find(i => i.queryText) if (!target) { return '' } const m = target.queryText.match(/app="([^"]+?)"/) return m ? m[1] : '' })(); log('authKey', authKey) if (TARGET_APP_AUTH.includes(authKey)) { window.fetch = async function (resource, init) { // Check if the request URL matches the target API endpoint if (typeof resource === 'string' && resource.includes('/loki/api/v1/query_range')) { log('Intercepted fetch request:', resource); // Call the original fetch const response = await originalFetch(resource, init); // Clone the response so we can modify it const clonedResponse = response.clone(); const data = await clonedResponse.json(); // Modify the response data log('Original response data:', data); const originList = get(data, ['data', 'result', 0, 'values'], []); const rstList = originList .map(([_oldTs, objStr]) => { const obj = JSON.parse(objStr); const time = obj.time; const content = JSON.parse(obj.content); delete obj.content; return [ String(time * 1e6), JSON.stringify({ ...content, ...obj, }), ]; }) .sort((a, b) => a[0] > b[0]); // Example modification: add a custom field to the response data.customField = 'Modified content'; if (originList.length) { set(data, ['data', 'result', 0, 'values'], rstList); } // Create a new response with the modified data const modifiedResponse = new Response(JSON.stringify(data), { status: response.status, statusText: response.statusText, headers: response.headers, }); log('Modified response data:', data); return modifiedResponse; } // If not our target URL, just proceed with the normal fetch return originalFetch(resource, init); }; } })();