Greasy Fork

Greasy Fork is available in English.

小鹅通 通用m3u8获取

获取某鹅通m3u8内容 重新拼装真实ts地址和解密真实密钥 发送给扩展

当前为 2023-08-15 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         小鹅通 通用m3u8获取
// @namespace    https://94cat.com/
// @version      0.3
// @description  获取某鹅通m3u8内容 重新拼装真实ts地址和解密真实密钥 发送给扩展
// @author       mz
// @match        https://*/*
// @match        http://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @run-at       document-start
// @license      GPL v3
// ==/UserScript==
(function () {
    'use strict';

    const _JSONparse = JSON.parse;
    JSON.parse = function () {
        let data = _JSONparse.apply(this, arguments);
        findMedia(data);
        return data;
    }
    JSON.parse.toString = function () {
        return _JSONparse.toString();
    }
    async function findMedia(data, raw = undefined, depth = 0) {
        for (let key in data) {
            if (typeof data[key] == "object") {
                if (depth > 25) { continue; }
                if (!raw) { raw = data; }
                findMedia(data[key], raw, ++depth);
                continue;
            }
            if (typeof data[key] == "string" && key == "video_urls" && data[key].slice(-4) == "__ba") {
                let base64 = data[key].replace("__ba", "");
                base64 = base64.replaceAll("@", "1").replaceAll("#", "2").replaceAll("$", "3").replaceAll("%", "4");
                let json = _JSONparse(atob(base64));
                if (!json) { return }
                for (let obj of json) {
                    fetch(obj.url).then(response => response.text())
                        .then(m3u8 => {
                            const lines = m3u8.split('\n');
                            let keyFlag = false;
                            for (let i = 0; i < lines.length; i++) {
                                if(lines[i] == '#EXT-X-ENDLIST'){ break; }
                                if (!keyFlag && lines[i].includes("#EXT-X-KEY:METHOD=AES-128,URI=")) {
                                    const match = lines[i].match(/URI="([^"]*)"/);
                                    if (match && match[1]) {
                                        keyFlag = true;
                                        if (window.__user_id) {
                                            getKey(match[1] + "&uid=" + window.__user_id, window.__user_id);
                                        } else if (document.cookie) {
                                            for (let cookie of document.cookie.split(';')) {
                                                cookie = cookie.trim();
                                                if (cookie.substring(0, 10) == "userInfo={") {
                                                    cookie = cookie.slice(9);
                                                    cookie = isJSON(cookie);
                                                    cookie && cookie.user_id && getKey(match[1] + "&uid=" + cookie.user_id, cookie.user_id);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    continue;
                                }
                                if (lines[i][0] != "#") {
                                    lines[i] = `${obj.ext.host}/${obj.ext.path}/${lines[i]}&${obj.ext.param}`;
                                }
                            }
                            m3u8 = lines.join('\n');
                            let url = URL.createObjectURL(new Blob([new TextEncoder("utf-8").encode(m3u8)]));
                            window.postMessage({ action: "catCatchAddMedia", url: url, href: location.href, ext: "m3u8" });
                        });
                }
            }
        }
    }
    function uid2byte(uid) {
        const byteArray = new Array;
        for (let i = 0; i < uid.length; i++) {
            let temp = uid.charCodeAt(i);
            if (temp >= 65536 && temp <= 1114111) {
                byteArray.push(temp >> 18 & 7 | 240);
                byteArray.push(temp >> 12 & 63 | 128);
                byteArray.push(temp >> 6 & 63 | 128);
                byteArray.push(63 & temp | 128);
            } else if (temp >= 2048 && temp <= 65535) {
                byteArray.push(temp >> 12 & 15 | 224);
                byteArray.push(temp >> 6 & 63 | 128);
                byteArray.push(63 & temp | 128);
            } else if (temp >= 128 && temp <= 2047) {
                byteArray.push(temp >> 6 & 31 | 192);
                byteArray.push(63 & temp | 128);
            } else {
                byteArray.push(255 & temp);
            }
        }
        return byteArray;
    }
    function getKey(url, userId) {
        fetch(url).then(response => response.arrayBuffer())
            .then(buffer => {
                let newKey = [];
                buffer = new Uint8Array(buffer);
                const uidByte = uid2byte(userId);
                for (let i in buffer) {
                    newKey.push(buffer[i] ^ uidByte[i]);
                }
                console.log(newKey);
                window.postMessage({ action: "catCatchAddKey", key: newKey, href: location.href });
            });
    }

    const _xhrOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function () {
        this.addEventListener("readystatechange", function (event) {
            const isJson = isJSON(this.response);
            isJson && findMedia(isJson);
        });
        _xhrOpen.apply(this, arguments);
    }
    XMLHttpRequest.prototype.open.toString = function () {
        return _xhrOpen.toString();
    }
    function isJSON(str) {
        if (typeof str == "object") {
            return str;
        }
        if (typeof str == "string") {
            try {
                return _JSONparse(str);
            } catch (e) { return false; }
        }
        return false;
    }
})();