Greasy Fork

Greasy Fork is available in English.

EME Logger

Inject EME interface and log its function calls.

当前为 2018-11-09 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         EME Logger
// @namespace    http://greasyfork.org/
// @version      0.3
// @description  Inject EME interface and log its function calls.
// @author       cramer
// @match        *://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(async () => {
    const indent = (s,n=4) => s.split('\n').map(l=>Array(n).fill(' ').join('')+l).join('\n');

    const b64 = {
        decode: s => Uint8Array.from(atob(s), c => c.charCodeAt(0)),
        encode: b => btoa(String.fromCharCode(...new Uint8Array(b)))
    };

    const fnproxy = (object, func) => new Proxy(object, { apply: func });

    const proxy = (object, key, func) => Object.defineProperty(object, key, {
        value: fnproxy(object[key], func)
    });

    proxy(Navigator.prototype, 'requestMediaKeySystemAccess', async (_target, _this, _args) => {
        const [keySystem, supportedConfigurations] = _args;
        console.log(
            `[EME] Navigator::requestMediaKeySystemAccess\n` +
            `    Key System: ${keySystem}\n` +
            `    Supported Configurations:\n` +
            indent(JSON.stringify(supportedConfigurations, null, '    '))
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeySystemAccess.prototype, 'createMediaKeys', async (_target, _this, _args) => {
        console.log(
            `[EME] MediaKeySystemAccess::createMediaKeys\n` +
            `    Key System: ${_this.keySystem}\n` +
            `    Configurations:\n` +
            indent(JSON.stringify(_this.getConfiguration(), null, '    '))
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeys.prototype, 'setServerCertificate', async (_target, _this, _args) => {
        const [serverCertificate] = _args;
        console.log(
            `[EME] MediaKeys::setServerCertificate\n` +
            `    Server Certificate: ${b64.encode(serverCertificate)}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeys.prototype, 'createSession', (_target, _this, _args) => {
        const [sessionType] = _args;
        console.log(
            `[EME] MediaKeys::createSession\n` +
            `    Session Type: ${sessionType || 'temporary (default)'}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(EventTarget.prototype, 'addEventListener', async (_target, _this, _args) => {
        const [type, listener] = _args;
        if (_this instanceof MediaKeySession) {
            switch(type) {
                case 'keystatuseschange': {
                    _args[1] = fnproxy(listener, (_target, _this, _args) => {
                        const [event] = _args;
                        const keySession = event.target;
                        const {sessionId} = keySession;
                        console.log(
                            `[EME] MediaKeySession::keystatuseschange\n` +
                            `    Session ID: ${sessionId || '(not available)'}\n` +
                            Array.from(keySession.keyStatuses).map(([keyId, status]) =>
                                `    [${status.toUpperCase()}] ${b64.encode(keyId)}`
                            ).join('\n') +
                            '\n    Callback:', _target
                        );
                        console.trace();
                        return _target.apply(_this, _args);;
                    });
                    break;
                }
                case 'message': {
                    _args[1] = fnproxy(listener, (_target, _this, _args) => {
                        const [event] = _args;
                        const keySession = event.target;
                        const {sessionId} = keySession;
                        const {message} = event;
                        console.log(
                            `[EME] MediaKeySession::message\n` +
                            `    Session ID: ${sessionId || '(not available)'}\n` +
                            `    Message: ${b64.encode(message)}` +
                            '\n    Callback:', _target
                        );
                        console.trace();
                        return _target.apply(_this, _args);;
                    });
                    break;
                }
            }
        }
        return _target.apply(_this, _args);
    });

    proxy(MediaKeySession.prototype, 'generateRequest', async (_target, _this, _args) => {
        const [initDataType, initData] = _args;
        console.log(
            `[EME] MediaKeySession::generateRequest\n` +
            `    Session ID: ${_this.sessionId || '(not available)'}\n` +
            `    Init Data Type: ${initDataType}\n` +
            `    Init Data: ${b64.encode(initData)}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeySession.prototype, 'load', async (_target, _this, _args) => {
        const [sessionId] = _args;
        console.log(
            `[EME] MediaKeySession::load\n` +
            `    Session ID: ${sessionId || '(not available)'}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeySession.prototype, 'update', async (_target, _this, _args) => {
        const [response] = _args;
        console.log(
            `[EME] MediaKeySession::update\n` +
            `    Session ID: ${_this.sessionId || '(not available)'}\n` +
            `    Response: ${b64.encode(response)}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeySession.prototype, 'close', async (_target, _this, _args) => {
        console.log(
            `[EME] MediaKeySession::close\n` +
            `    Session ID: ${_this.sessionId || '(not available)'}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });

    proxy(MediaKeySession.prototype, 'remove', async (_target, _this, _args) => {
        console.log(
            `[EME] MediaKeySession::remove\n` +
            `    Session ID: ${_this.sessionId || '(not available)'}`
        );
        console.trace();
        return _target.apply(_this, _args);
    });
})();