Greasy Fork

Greasy Fork is available in English.

墨水屏Rlog按time重排

Modify specific fetch responses on a given page.

当前为 2024-10-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         墨水屏Rlog按time重排
// @namespace    aizigao
// @version      1.0
// @run_at      document-start
// @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);
    };
  }
})();