Something went wrong. Try again.
A iOS Webkit browser meant to replicate Arc Search's design. (use at your own risk; vibe-coded)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177#!/usr/bin/env python3"""Convert Adblock Plus style filter lists into WebKit content-blocker JSON.
Usage: convert_filters.py <list.txt> <out.json> [--cosmetic-only | --network-only]
Only the subset of syntax that maps cleanly onto WKContentRuleList is kept;anything exotic (regex filters, $csp, $redirect, extended CSS, ...) is dropped."""import jsonimport reimport sys
MAX_RULES = 140_000TYPE_MAP = { "script": "script", "image": "image", "stylesheet": "style-sheet", "xmlhttprequest": "raw", "font": "font", "media": "media", "subdocument": "document", "document": "document", "popup": "popup", "ping": "ping", "other": "raw", "websocket": "websocket",}IGNORED_OPTS = {"important", "match-case", "all", "first-party", "1p"}SAFE = re.compile(r"^[A-Za-z0-9_\-./:?#&=%\[\]\\^$*+()~@!,;|]*$")
def to_regex(pat: str): """ABP network pattern -> WebKit-compatible regex (no alternation).""" anchor_domain = pat.startswith("||") anchor_start = (not anchor_domain) and pat.startswith("|") if anchor_domain: pat = pat[2:] elif anchor_start: pat = pat[1:] anchor_end = pat.endswith("|") if anchor_end: pat = pat[:-1] out = [] for ch in pat: if ch == "*": out.append(".*") elif ch == "^": out.append("[/:?#&=]") elif ch in ".+?()[]{}\\$|": out.append("\\" + ch) else: out.append(ch) body = "".join(out) if anchor_domain: rx = r"^[a-z]+://([^/]+\.)?" + body elif anchor_start: rx = "^" + body else: rx = ".*" + body if anchor_end: rx += "$" return rx
def parse_domains(spec: str): inc, exc = [], [] for d in spec.split("|"): if not d or "*" in d or "/" in d: continue neg = d.startswith("~") host = d.lstrip("~").lower() try: host = host.encode("idna").decode("ascii") # WebKit wants lower-case punycode except UnicodeError: continue if not re.fullmatch(r"[a-z0-9._-]+", host): continue (exc if neg else inc).append("*" + host) return inc, exc
def network_rule(line: str): exception = line.startswith("@@") if exception: line = line[2:] pat, _, opts = line.partition("$") if "$" in line and not line.startswith("/") else (line, "", "") if pat.startswith("/") and pat.endswith("/") and len(pat) > 2: return None # regex filter if not pat or pat in ("*", "|", "||"): return None trigger = {} types, third, inc, exc = [], None, [], [] for o in filter(None, opts.split(",")): neg = o.startswith("~") name = o.lstrip("~") if name == "third-party" or name == "3p": third = "first-party" if neg else "third-party" elif name.startswith("domain="): inc, exc = parse_domains(name[7:]) elif name in TYPE_MAP and not neg: types.append(TYPE_MAP[name]) elif name in IGNORED_OPTS: continue else: return None rx = to_regex(pat) if not SAFE.match(rx) or len(rx) > 400: return None trigger["url-filter"] = rx if types: trigger["resource-type"] = sorted(set(types)) if third: trigger["load-type"] = [third] if inc: trigger["if-domain"] = inc elif exc: trigger["unless-domain"] = exc action = {"type": "ignore-previous-rules" if exception else "block"} return {"trigger": trigger, "action": action}
BAD_COSMETIC = (":has-text", ":-abp", ":xpath", ":matches", ":upward", ":remove", ":style", "+js(", ":not(:", ":contains", ":nth-ancestor", ":min-text")
def cosmetic(line: str): domains, sep, selector = line.partition("##") if not sep or selector.startswith(("+js", "^")): return None if any(b in selector for b in BAD_COSMETIC) or not SAFE.match(selector.replace(" ", "")) and False: return None if "\\" in selector or "'" in selector and "[" not in selector: return None if domains: inc, exc = parse_domains(domains.replace(",", "|")) if exc and not inc: return None return {"domains": inc, "selector": selector} return {"domains": [], "selector": selector}
def main(): src, dst = sys.argv[1], sys.argv[2] mode = sys.argv[3] if len(sys.argv) > 3 else "" blocks, exceptions, generic, scoped = [], [], [], [] for raw in open(src, encoding="utf-8", errors="ignore"): line = raw.strip() if not line or line[0] in "![": continue if "#@#" in line or "#?#" in line or "#$#" in line or "#%#" in line: continue if "##" in line: if mode == "--network-only": continue c = cosmetic(line) if c: (scoped if c["domains"] else generic).append(c) continue if mode == "--cosmetic-only": continue r = network_rule(line) if r: (exceptions if r["action"]["type"] != "block" else blocks).append(r)
rules = blocks[:MAX_RULES - 2000] + exceptions[:1500] # Generic cosmetic selectors are batched; scoped ones grouped by domain set. for i in range(0, len(generic), 40): sel = ",".join(c["selector"] for c in generic[i:i + 40]) rules.append({"trigger": {"url-filter": ".*"}, "action": {"type": "css-display-none", "selector": sel}}) groups = {} for c in scoped: groups.setdefault(tuple(sorted(c["domains"])), []).append(c["selector"]) for doms, sels in groups.items(): for i in range(0, len(sels), 40): rules.append({"trigger": {"url-filter": ".*", "if-domain": list(doms)}, "action": {"type": "css-display-none", "selector": ",".join(sels[i:i + 40])}}) rules = rules[:MAX_RULES] json.dump(rules, open(dst, "w"), separators=(",", ":")) print(f"{src}: {len(blocks)} block, {len(exceptions)} exceptions, " f"{len(generic)} generic + {len(scoped)} scoped cosmetic -> {len(rules)} rules")
if __name__ == "__main__": main()