-
Notifications
You must be signed in to change notification settings - Fork 3
/
content_script_embed.py
47 lines (34 loc) · 993 Bytes
/
content_script_embed.py
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
#! /usr/bin/env python3
def to_js_string(src):
src = src.replace(b'\\', b'\\\\')
src = src.replace(b'`', b'\\`')
src = src.replace(b'$', b'\\$')
src = b'`' + src + b'`'
return src
def from_script(src):
src = to_js_string(src)
dest = [b'''\
(() => {
if (!(document instanceof HTMLDocument)) return;
console.log('[canvas-rr] Injecting rr-record.js inline...');
// -
const blob = new self.Blob([escaped_file()], // Move this big string to the end.
{ type: 'text/javascript' });
const url = self.URL.createObjectURL(blob);
let script = document.createElement('script');
script.src = url;
script.async = false;
document.documentElement.append(script);
// -
function escaped_file() {
return ''', src, b''';
}
})();
''']
return b''.join(dest)
if __name__ == '__main__':
import sys
src = sys.stdin.buffer.read()
dest = from_script(src)
sys.stdout.buffer.write(dest)
exit(0)