-
Notifications
You must be signed in to change notification settings - Fork 68
/
inject.js
50 lines (43 loc) · 1.8 KB
/
inject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Refactored conversion functions
const hexStrToU8 = hexString => Uint8Array.from(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
const u8ToHexStr = bytes => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
const b64ToHexStr = b64 => [...atob(b64)].map(c=> c.charCodeAt(0).toString(16).padStart(2,0)).join``
// initData to PSSH
function getPssh(buffer) {
const bytes = hexStrToU8(u8ToHexStr(new Uint8Array(buffer)).match(/000000..70737368.*/)[0]);
return window.btoa(String.fromCharCode(...bytes));
}
// Get Clearkey keys
function getClearkey(response) {
let obj=JSON.parse((new TextDecoder("utf-8")).decode(response))
obj = obj["keys"].map(o => [o["kid"], o["k"]]);
obj = obj.map(o => o.map(a => a.replace(/-/g, '+').replace(/_/g, '/')+"=="))
return obj.map(o => `${b64ToHexStr(o[0])}:${b64ToHexStr(o[1])}`).join("\n")
}
// Widevine PSSH extraction from init
const originalGenerateRequest = MediaKeySession.prototype.generateRequest;
MediaKeySession.prototype.generateRequest = function(initDataType, initData) {
const result = originalGenerateRequest.call(this, initDataType, initData);
//Get PSSH and pass into content.js
try {
console.log("[PSSH] " + getPssh(initData))
document.dispatchEvent(new CustomEvent('pssh', {
detail: getPssh(initData)
}));
} finally {
return result;
}
};
//Clearkey Support
const originalUpdate = MediaKeySession.prototype.update;
MediaKeySession.prototype.update = function(response) {
const result = originalUpdate.call(this, response);
try {
console.log("[CLEARKEY] " + getClearkey(response));
document.dispatchEvent(new CustomEvent('clearkey', {
detail: getClearkey(response)
}));
} finally {
return result;
}
};