EME Logger

Inject EME interface and log its function calls.

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

  1. // ==UserScript==
  2. // @name EME Logger
  3. // @namespace http://greasyfork.org/
  4. // @version 0.2
  5. // @description Inject EME interface and log its function calls.
  6. // @author cramer
  7. // @match *://*/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (async () => {
  13. const indent = (s,n=4) => s.split('\n').map(l=>Array(n).fill(' ').join('')+l).join('\n');
  14.  
  15. const b64 = {
  16. decode: s => Uint8Array.from(atob(s), c => c.charCodeAt(0)),
  17. encode: b => btoa(String.fromCharCode(...new Uint8Array(b)))
  18. };
  19.  
  20. const fnproxy = (object, func) => new Proxy(object, { apply: func });
  21.  
  22. const proxy = (object, key, func) => Object.defineProperty(object, key, {
  23. value: fnproxy(object[key], func)
  24. });
  25.  
  26. proxy(Navigator.prototype, 'requestMediaKeySystemAccess', async (_target, _this, _args) => {
  27. const [keySystem, supportedConfigurations] = _args;
  28. console.log(
  29. `[EME] Navigator::requestMediaKeySystemAccess\n` +
  30. ` Key System: ${keySystem}\n` +
  31. ` Supported Configurations:\n` +
  32. indent(JSON.stringify(supportedConfigurations, null, ' '))
  33. );
  34. console.trace();
  35. return _target.apply(_this, _args);
  36. });
  37.  
  38. proxy(MediaKeySystemAccess.prototype, 'createMediaKeys', async (_target, _this, _args) => {
  39. console.log(
  40. `[EME] MediaKeySystemAccess::createMediaKeys\n` +
  41. ` Key System: ${_this.keySystem}\n` +
  42. ` Configurations:\n` +
  43. indent(JSON.stringify(_this.getConfiguration(), null, ' '))
  44. );
  45. console.trace();
  46. return _target.apply(_this, _args);
  47. });
  48.  
  49. proxy(MediaKeys.prototype, 'setServerCertificate', async (_target, _this, _args) => {
  50. const [serverCertificate] = _args;
  51. console.log(
  52. `[EME] MediaKeys::setServerCertificate\n` +
  53. ` Server Certificate: ${b64.encode(serverCertificate)}`
  54. );
  55. console.trace();
  56. return _target.apply(_this, _args);
  57. });
  58.  
  59. proxy(MediaKeys.prototype, 'createSession', (_target, _this, _args) => {
  60. const [sessionType] = _args;
  61. console.log(
  62. `[EME] MediaKeys::createSession\n` +
  63. ` Session Type: ${sessionType || 'temporary (default)'}`
  64. );
  65. console.trace();
  66. return _target.apply(_this, _args);
  67. });
  68.  
  69. proxy(EventTarget.prototype, 'addEventListener', async (_target, _this, _args) => {
  70. const [type, listener] = _args;
  71. if (_this instanceof MediaKeySession) {
  72. switch(type) {
  73. case 'keystatuseschange': {
  74. _args[1] = fnproxy(listener, (_target, _this, _args) => {
  75. const [event] = _args;
  76. const keySession = event.target;
  77. const {sessionId} = keySession;
  78. console.log(
  79. `[EME] MediaKeySession::keystatuseschange\n` +
  80. ` Session ID: ${sessionId || '(not available)'}\n` +
  81. Array.from(keySession.keyStatuses).map(([keyId, status]) =>
  82. ` [${status.toUpperCase()}] ${b64.encode(keyId)}`
  83. ).join('\n')
  84. );
  85. console.trace();
  86. return _target.apply(_this, _args);;
  87. });
  88. break;
  89. }
  90. case 'message': {
  91. _args[1] = fnproxy(listener, (_target, _this, _args) => {
  92. const [event] = _args;
  93. const keySession = event.target;
  94. const {sessionId} = keySession;
  95. const {message} = event;
  96. console.log(
  97. `[EME] MediaKeySession::message\n` +
  98. ` Session ID: ${sessionId || '(not available)'}\n` +
  99. ` Message: ${b64.encode(message)}`
  100. );
  101. console.trace();
  102. return _target.apply(_this, _args);;
  103. });
  104. break;
  105. }
  106. }
  107. }
  108. return _target.apply(_this, _args);
  109. });
  110.  
  111. proxy(MediaKeySession.prototype, 'generateRequest', async (_target, _this, _args) => {
  112. const [initDataType, initData] = _args;
  113. console.log(
  114. `[EME] MediaKeySession::generateRequest\n` +
  115. ` Session ID: ${_this.sessionId || '(not available)'}\n` +
  116. ` Init Data Type: ${initDataType}\n` +
  117. ` Init Data: ${b64.encode(initData)}`
  118. );
  119. console.trace();
  120. return _target.apply(_this, _args);
  121. });
  122.  
  123. proxy(MediaKeySession.prototype, 'load', async (_target, _this, _args) => {
  124. const [sessionId] = _args;
  125. console.log(
  126. `[EME] MediaKeySession::load\n` +
  127. ` Session ID: ${sessionId || '(not available)'}`
  128. );
  129. console.trace();
  130. return _target.apply(_this, _args);
  131. });
  132.  
  133. proxy(MediaKeySession.prototype, 'update', async (_target, _this, _args) => {
  134. const [response] = _args;
  135. console.log(
  136. `[EME] MediaKeySession::update\n` +
  137. ` Session ID: ${_this.sessionId || '(not available)'}\n` +
  138. ` Response: ${b64.encode(response)}`
  139. );
  140. console.trace();
  141. return _target.apply(_this, _args);
  142. });
  143.  
  144. proxy(MediaKeySession.prototype, 'close', async (_target, _this, _args) => {
  145. console.log(
  146. `[EME] MediaKeySession::close\n` +
  147. ` Session ID: ${_this.sessionId || '(not available)'}`
  148. );
  149. console.trace();
  150. return _target.apply(_this, _args);
  151. });
  152.  
  153. proxy(MediaKeySession.prototype, 'remove', async (_target, _this, _args) => {
  154. console.log(
  155. `[EME] MediaKeySession::remove\n` +
  156. ` Session ID: ${_this.sessionId || '(not available)'}`
  157. );
  158. console.trace();
  159. return _target.apply(_this, _args);
  160. });
  161. })();