-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathupdate_manifest_hosts.py
executable file
·50 lines (42 loc) · 1.65 KB
/
update_manifest_hosts.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
48
49
#!/usr/bin/env python
# Please don't judge me for this hack... I know it's horrible.
#
# Extracts hosts used in RULES and injects them into the permissions block in
# the manifest. Can use it to help update the manifest when adding new rules.
import os, re, collections
DIR = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'src')
# Grab rules from javascript code
with open(os.path.join(DIR, 'redirectify.js'), 'r') as fid:
all_code = fid.read()
rules_txt = re.search(r'RULES = \[(.*?)\];\n', all_code, re.DOTALL).groups()[0]
# Find match patterns needed by rules (fragile, not parsing rigorously)
hosts = []
for rule_txt in rules_txt.strip().strip(']').split('],'):
match = rule_txt.split(',')[0].strip().strip('[')
hosts.append(' '*4 + match + ",\n")
# origin of request needs host permission in Chrome (could omit for Firefox)
final_part = rule_txt.split('[')[-1].strip()
if final_part.endswith(']'):
for host in final_part.strip(']').split(','):
hosts.append(' '*4 + '"*://*' + host.strip()[1:-1] + '/*",\n')
# deduplicate:
hosts = list(collections.OrderedDict.fromkeys(hosts))
# Get other bits of existing manifest
bits1 = []
bits2 = []
with open(os.path.join(DIR, 'manifest.json'), 'r') as fid:
for line in fid:
bits1.append(line)
if '"permissions":' in line:
break
for line in fid:
if '"webRequest",' in line:
bits2.append(line)
break
for line in fid:
bits2.append(line)
# Spit out new manifest
with open(os.path.join(DIR, 'manifest.json'), 'w') as fid:
fid.write(''.join(bits1))
fid.write(''.join(hosts))
fid.write(''.join(bits2))