-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadapt-special-case.py
More file actions
158 lines (136 loc) · 6.1 KB
/
Copy pathadapt-special-case.py
File metadata and controls
158 lines (136 loc) · 6.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
"""
Transform ophirofox content_script JS → userscript-compatible JS.
Replaces chrome.storage.sync (unavailable in userscripts) with GM.* equivalents
and configurationsSpecifiques() with getOphirofoxConfig() + property check.
Usage: python3 adapt-special-case.py <site_type> < input.js > output.js
site_type: mediapart | arretsurimages | alternativeseconomiques
"""
import sys, re
SITE = sys.argv[1] if len(sys.argv) > 1 else ""
code = sys.stdin.read()
# ── Helper: extract quoted keys from ["k1", 'k2'] strings ────────────────────
def _extract_keys(s):
return re.findall(r"""['"]([^'"]+)['"]""", s)
# ── 1. configurationsSpecifiques([...]) → getOphirofoxConfig() ──────────────
code = re.sub(
r"const config = await configurationsSpecifiques\(\[.*?\]\);?",
"const config = await getOphirofoxConfig();",
code,
)
# ── 2. if (!config) → property check (site-specific) ────────────────────────
PROPERTY_MAP = {
"mediapart": "AUTH_URL_MEDIAPART",
"arretsurimages": "AUTH_URL_ARRETSURIMAGES",
"alternativeseconomiques": "AUTH_URL_ALTERNATIVESECONOMIQUES",
"pressreader": "AUTH_URL_PRESSREADER",
}
prop = PROPERTY_MAP.get(SITE)
if prop:
code = re.sub(r"if\s*\(!config\)\s*return;", f"if (!config.{prop}) return;", code)
code = re.sub(r"if\s*\(!config\)\s*\{", f"if (!config.{prop}) {{", code)
# ── 3. chrome.storage.sync.remove(["k1","k2"]) → GM.deleteValue(...) ────────
def _replace_remove(m):
indent = m.group(1)
has_await = bool(m.group(2))
keys = _extract_keys(m.group(3))
prefix = "await " if has_await else ""
return "\n".join(f"{indent}{prefix}GM.deleteValue(\"{k}\");" for k in keys)
code = re.sub(
r"(\s*)(await\s+)?chrome\.storage\.sync\.remove\(\s*\[(.*?)\]\s*\);?",
_replace_remove,
code,
flags=re.DOTALL,
)
# ── 4. chrome.storage.sync.set({...}) → GM.setValue(...) ────────────────────
def _replace_set(m):
indent = m.group(1)
has_await = bool(m.group(2))
inner = m.group(3)
pairs = re.findall(r"""['"]([^'"]+)['"]\s*:\s*(.+?)\s*(?:,|$)""", inner, re.DOTALL)
prefix = "await " if has_await else ""
lines = []
for k, v in pairs:
v = v.strip().rstrip(",")
lines.append(f"{indent}{prefix}GM.setValue(\"{k}\", {v});")
return "\n".join(lines)
code = re.sub(
r"(\s*)(await\s+)?chrome\.storage\.sync\.set\(\s*\{(.*?)\}\s*\);?",
_replace_set,
code,
flags=re.DOTALL,
)
# ── 5. chrome.storage.sync.get(...).then(...) → GM.getValue(...) ───────────
# Handles: let articlePath;\n await chrome.storage.sync.get(...).then(...)
def _replace_get_then(m):
let_indent = m.group(1) or "" # indent of optional "let x;" line
let_var = m.group(2) or "" # variable name from "let x;"
await_indent = m.group(3) or "" # indent of "await chrome..." line
keys_str = m.group(4)
body = m.group(5)
indent = let_indent or await_indent
keys = _extract_keys(keys_str)
lines = []
for k in keys:
# Find variable assigned from result.KEY
vm = re.search(r"(\w+)\s*=\s*result\." + re.escape(k), body)
if vm:
varname = vm.group(1)
lines.append(f"{indent}const {varname} = await GM.getValue(\"{k}\", null);")
return "\n".join(lines)
code = re.sub(
r"(?:(\s*)let\s+(\w+);\s*\n)?" # optional preceding let declaration
r"(\s*)await chrome\.storage\.sync\.get\(\s*\[(.*?)\]\s*\)"
r"\.then\(\s*\(result\)\s*=>\s*\{(.*?)\}\s*\);?",
_replace_get_then,
code,
flags=re.DOTALL,
)
# ── 6. const { key: var } = await chrome.storage.sync.get([...]) ────────────
def _replace_get_destructure(m):
indent = m.group(1)
destructure = m.group(2)
keys_str = m.group(3) # captured but unused — destructured names ARE the keys
parts = re.findall(r"(\w+)\s*:\s*(\w+)", destructure)
lines = []
for storage_key, var_name in parts:
lines.append(f"{indent}const {var_name} = await GM.getValue(\"{storage_key}\", null);")
return "\n".join(lines)
code = re.sub(
r"(\s*)const\s*\{(.*?)\}\s*=\s*await chrome\.storage\.sync\.get\(\s*\[(.*?)\]\s*\);?",
_replace_get_destructure,
code,
)
# ── 7. Cleanup ───────────────────────────────────────────────────────────────
code = re.sub(r"await\s+await\s+", "await ", code)
# ── 8. Apple-platform cog wheel (⚙) on special-case links ───────────────────
COG_WRAPPER = """\
function ophirofoxWrapWithCog(element) {
if (isApplePlatform()) {
const wrapper = document.createElement("span");
wrapper.style.cssText = "display: inline-flex; align-items: center; gap: 6px;";
wrapper.appendChild(element);
const cog = document.createElement("button");
cog.textContent = "\\\\u2699";
cog.title = "Parametres Ophirofox";
cog.style.cssText = "background: white; border: 1px solid #ccc; border-radius: 4px; cursor: pointer; font-size: 16px; padding: 2px 6px; line-height: 1; z-index: 20;";
cog.addEventListener("click", async (evt) => {
evt.preventDefault();
evt.stopPropagation();
const result = await showSettingsPanel();
universityName = result.universityName;
settingsOpenLinksNewTab = result.openLinksNewTab;
settingsAutoOpenLink = result.autoOpenLink;
await getSettings();
});
wrapper.appendChild(cog);
return wrapper;
}
return element;
}
"""
# Inject the wrapper before the onLoad function
code = re.sub(r"(\s*)(async function onLoad)", r"\1" + COG_WRAPPER + r"\n\1\2", code)
# Wrap return value of createLink functions with the cog
code = re.sub(r"(\s+return) (a|div);", r"\1 ophirofoxWrapWithCog(\2);", code)
sys.stdout.write(code)