Fetch Logger

Logs fetch requests in a GUI for user viewing

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Fetch Logger
// @namespace    http://tampermonkey.net/
// @version      0.2 
// @description  Logs fetch requests in a GUI for user viewing
// @author       zuxity
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Create a container for logging fetch requests
    const logContainer = document.createElement('div');
    logContainer.style.position = 'fixed';
    logContainer.style.top = '0';
    logContainer.style.right = '0';
    logContainer.style.background = '#fff';
    logContainer.style.border = '1px solid #ccc';
    logContainer.style.padding = '10px';
    logContainer.style.maxWidth = '300px';
    logContainer.style.overflow = 'auto';
    logContainer.style.zIndex = '9999';
    document.body.appendChild(logContainer);

    // Intercept fetch requests
    const originalFetch = window.fetch;
    window.fetch = function(url, options) {
        return originalFetch(url, options)
            .then(response => {
                // Log the fetch request
                logFetch(url, options, response);
                return response;
            })
            .catch(error => {
                // Log errors
                logError(url, options, error);
                throw error;
            });
    };

    // Function to log fetch requests
    function logFetch(url, options, response) {
        const logEntry = document.createElement('div');
        logEntry.textContent = `URL: ${url}, Method: ${options.method}, Status: ${response.status}`;
        logContainer.appendChild(logEntry);
    }

    // Function to log errors
    function logError(url, options, error) {
        const logEntry = document.createElement('div');
        logEntry.textContent = `Error fetching ${url}: ${error}`;
        logContainer.appendChild(logEntry);
    }
})();