-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
umatrix2ublock.py
executable file
·81 lines (61 loc) · 1.99 KB
/
umatrix2ublock.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import sys
# Input files
UMATRIX = sys.argv[1] # "my-umatrix-rules.txt"
UBLOCK_OLD = sys.argv[2] # "my-ublock-dynamic-rules_2020-02-26_14.07.25.txt"
# Output file
UBLOCK_NEW = "my-ublock-dynamic-rules.txt"
with open(UMATRIX) as fp:
rules = fp.readlines()
# Dictionaries which translate the rule
# NOTE: Only translated when destination is "*"
translate_request = {
"*": "{request}",
"image": "{request}",
"script": "{prefix}-{request}",
"frame": "{prefix}-{request}",
"xhr": "inline-script",
}
translate_action = {"allow": "noop", "inherit": "noop", "block": "block"}
# Requests which cannot be translated
skip_requests = ("cookie", "css", "plugin", "media", "other")
skip_rules = (
"* * * block",
"* * image allow",
"* 1st-party * allow",
)
ub_rules = []
for rule in rules:
rule = rule.rstrip("\n")
if rule in skip_rules:
print("Skipping rule:".rjust(15), rule)
continue
words = rule.split()
if len(words) == 4:
# Is a site specific rule
src, dest, request, action = words
if request in skip_requests:
print("Skipping req:".rjust(15), rule)
continue
if dest == "*":
# First party or third party
prefix = "1p" if src == dest else "3p"
ub_request = translate_request[request].format(
prefix=prefix, request=request
)
else:
ub_request = "*"
ub_action = translate_action[action]
ub_rule = f"{src} {dest} {ub_request} {ub_action}"
print("Adding:".rjust(15), ub_rule)
ub_rules.append(ub_rule + "\n")
else:
# Is a global rule
continue
with open(UBLOCK_OLD) as old:
# ub_rules_old = {rule + "\n" for rule in old.readlines()}
ub_rules_old = set(old.readlines())
# Make a union of old and new uBlock rules
ub_rules_merged = sorted(ub_rules_old.union(ub_rules))
with open(UBLOCK_NEW, "w") as fp:
fp.writelines(ub_rules_merged)