RequestHook

Copy Request Information For fetch and XMLHttpRequest

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         RequestHook
// @namespace    https://greasyfork.org/zh-CN/scripts/414756
// @version      0.3
// @description  Copy Request Information For fetch and XMLHttpRequest
// @author       AutumnSun
// @match        *://*.youtube.com/*
// @grant        GM_setClipboard
// @grant        GM_cookie
// @grant        unsafeWindow
// ==/UserScript==

(function() {
    'use strict';
    // Config Here
    function urlChecker(url){
        return /(heartbeat|send_message)$/.test(url.pathname);
    }
    function handleResponse(method, url, data, args){
        if(urlChecker(url)){
            console.log("Current Status:", data.playabilityStatus.status);
            return;
        }
    }

    function copyCookie(extra={}){
        GM_cookie('list', { url: location.href }, (cookies) => {
            const text = JSON.stringify({extra: extra, cookies: cookies});
            console.log(text);
            GM_setClipboard(text);
        });
    }

    const originRequest = unsafeWindow.fetch;
    unsafeWindow.fetch = (...args) => {
        const url = new URL(args[0].url, location.href);
        if(urlChecker(url)){
           let req = args[0].clone();
           req.json().then((data)=>{
                console.log('Request Captured:', req.method, req.url);
                let headers = {};
                for(let pair of req.headers){
                    headers[pair[0]] = pair[1];
                }
                console.log('Headers', headers);
                console.log('Body');
                console.log(data);
                copyCookie({
                    method: req.method,
                    url: req.url,
                    headers: headers,
                    data: data,
                })
            });
        }
        let resp = originRequest(...args);
        return resp;
    }
    console.log("fetch Injected");
    (function() {
        var origOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function() {
            let args = arguments;
            const method = arguments[0];
            const url = new URL(arguments[1], location.href);
            if(urlChecker(url)){
                console.log('request started!', arguments);
                this.addEventListener('load', function() {
                    console.log('request completed!', this.readyState); //will always be 4 (ajax is completed successfully)
                    if(this.responseType === 'text' || this.responseType === ''){
                        let data = JSON.parse(this.responseText);
                        handleResponse(method, url, data, args);
                    }
                });
            }
            origOpen.apply(this, arguments);
        };
        console.log("XMLHttpRequest Injected");
    })();
})();