Print requests with content-type 'application/vnd.apple.mpegurl'

Print content-type 'application/vnd.apple.mpegurl'

当前为 2023-04-13 提交的版本,查看 最新版本

// ==UserScript==
// @name         Print requests with content-type 'application/vnd.apple.mpegurl'
// @namespace    http://your-domain-here/
// @version      6
// @description  Print content-type 'application/vnd.apple.mpegurl'
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function logXHRRequest(method, url) {
        console.log('XHR request:', method, url);
    }

    function logXHRResponse(response) {
        const contentType = response.headers['content-type'];
        if (contentType === 'application/vnd.apple.mpegurl') {
            console.log('XHR response:', response.data);
        }
    }

    function addInterceptorToIframe(iframe) {
        const iframeWindow = iframe.contentWindow;
        const iframeDocument = iframe.contentDocument;

        // Attach the interceptor to the iframe's XMLHttpRequest object
        iframeWindow.XMLHttpRequest.prototype._send = iframeWindow.XMLHttpRequest.prototype.send;
        iframeWindow.XMLHttpRequest.prototype.send = function(data) {
            logXHRRequest(this.method, this.url);

            const _this = this;
            const xhrOverride = {
                set send(data) {
                    _this._send.call(_this, data);
                },
                set onreadystatechange(fn) {
                    if (fn) {
                        _this.addEventListener("readystatechange", function() {
                            if (_this.readyState === 4) {
                                logXHRResponse(_this);
                            }
                            fn.apply(null, arguments);
                        }, false);
                    }
                }
            };
            for (const key in this) {
                if (this.hasOwnProperty(key)) {
                    xhrOverride[key] = this[key];
                }
            }
            this.xhrOverride = xhrOverride;

            this._send.call(this, data);
        };

        // Attach the interceptor to fetch requests made in the iframe
        const originalFetch = iframeWindow.fetch;
        iframeWindow.fetch = function(url, options) {
            logXHRRequest('GET', url);

            return originalFetch.call(this, url, options).then(function(response) {
                if (response.headers.get('content-type') === 'application/vnd.apple.mpegurl') {
                    response.clone().text().then(function(text) {
                        console.log('XHR response:', text);
                    });
                }
                return response;
            });
        };

        // Attach the interceptor to the iframe's XMLHttpRequest object in the iframe's document
        const originalXHR = iframeDocument.defaultView.XMLHttpRequest;
        iframeDocument.defaultView.XMLHttpRequest = function() {
            const xhr = new originalXHR();
            xhr.addEventListener('load', function() {
                logXHRResponse(xhr);
            });
            return xhr;
        };
    }

    // Get all iframes in the document and attach the interceptor to them
    const iframes = document.getElementsByTagName('iframe');
    for (let i = 0; i < iframes.length; i++) {
        addInterceptorToIframe(iframes[i]);
    }
})();
长期地址
遇到问题?请前往 GitHub 提 Issues。