-
Notifications
You must be signed in to change notification settings - Fork 6
/
hook-hls.js
73 lines (66 loc) · 1.85 KB
/
hook-hls.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
if (self != top) {
class Hooker {
static _hookCall(cb) {
const call = Function.prototype.call;
Function.prototype.call = function(...args) {
let ret = call.apply(this, args);
try {
if (args && cb(args)) {
Function.prototype.call = call;
cb = () => {};
}
} catch (err) {
console.error(err.stack);
}
return ret;
};
this._hookCall = null;
}
static _isEsModule(obj) {
return obj.__esModule;
}
static _isFuction(arg) {
return 'function' === typeof arg;
}
static _isModuleCall(args) { // module.exports, module, module.exports, require
return args.length === 4 && args[1] && Object.getPrototypeOf(args[1]) === Object.prototype && args[1].hasOwnProperty('exports');
}
static _hookModuleCall(cb, pred) {
const callbacksMap = new Map([[pred, [cb]]]);
this._hookCall((args) => {
if (!this._isModuleCall(args)) return;
const exports = args[1].exports;
for (const [pred, callbacks] of callbacksMap) {
if (!pred.apply(this, [exports])) continue;
callbacks.forEach(cb => cb(exports, args));
callbacksMap.delete(pred);
!callbacksMap.size && (this._hookModuleCall = null);
break;
}
return !callbacksMap.size;
});
this._hookModuleCall = (cb, pred) => {
if (callbacksMap.has(pred)) {
callbacksMap.get(pred).push(cb);
} else {
callbacksMap.set(pred, [cb]);
}
};
}
}
const isHls_lib = (exports) => !exports.default && exports.buildAbsoluteURL
&& exports.buildURLFromParts && exports.parseURL;
Hooker._hookModuleCall((exports, args) => {
const p = exports.buildAbsoluteURL;
exports.buildAbsoluteURL = function() {
top.postMessage({
id: 'metal-h5-get-frame',
url: arguments[1],
vType: 'hls'
}, '*');
chrome.runtime.sendMessage({id:'inject-lib',vType:'hls',url: arguments[1]});
exports.buildAbsoluteURL = p;
return p.apply(exports,arguments)
}
}, isHls_lib);
}