| 432 | |
| 433 | |
| 434 | def rewrite_html(raw_html: str, base_url: str, prefs: dict, embedded: bool = False) -> str: |
| 435 | def repl(match): |
| 436 | prefix, quote, value, suffix = match.groups() |
| 437 | if not value: |
| 438 | return match.group(0) |
| 439 | if value.startswith(("javascript:", "data:", "mailto:", "tel:", "#")): |
| 440 | return match.group(0) |
| 441 | abs_url = urllib.parse.urljoin(base_url, value) |
| 442 | abs_url = strip_tracking(abs_url) |
| 443 | proxied = url_for("browser_open") + "?url=" + urllib.parse.quote(abs_url, safe="") |
| 444 | if embedded: |
| 445 | proxied += "&embed=1" |
| 446 | return f"{prefix}{quote}{proxied}{suffix}" |
| 447 | |
| 448 | out = URL_ATTR_RE.sub(repl, raw_html) |
| 449 | out = FORM_TAG_RE.sub(r'<form\1 enctype="multipart/form-data">', out) |
| 450 | |
| 451 | if prefs.get("adblock"): |
| 452 | out = re.sub(r"(?is)<script[^>]+(?:doubleclick|googlesyndication|googletagmanager|google-analytics|adsystem|taboola|outbrain|criteo)[^>]*>.*?</script>", "", out) |
| 453 | out = re.sub(r"(?is)<iframe[^>]+(?:doubleclick|googlesyndication|googletagmanager|adsystem|taboola|outbrain)[^>]*>.*?</iframe>", "", out) |
| 454 | out = re.sub(r"(?is)<[^>]+(?:id|class)=['\"][^'\"]*(?:ad-|ads-|advert|advertisement|sponsor|sponsored|popup|promoted)[^'\"]*['\"][^>]*>.*?</[^>]+>", "", out) |
| 455 | |
| 456 | if prefs.get("lite"): |
| 457 | out = re.sub(r"(?is)<script[^>]*>.*?</script>", "", out) |
| 458 | out = re.sub(r"(?is)<link[^>]+rel=['\"](?:preload|prefetch|modulepreload)['\"][^>]*>", "", out) |
| 459 | out = re.sub(r"(?is)<video[^>]*>.*?</video>", '<div style="padding:12px;border:1px solid rgba(191,151,255,.34);border-radius:14px;margin:10px 0;">Video hidden in Lite mode.</div>', out) |
| 460 | |
| 461 | if prefs.get("block_images"): |
| 462 | out = re.sub(r"(?is)<img\b[^>]*>", '<div style="display:inline-block;padding:8px 10px;border:1px solid rgba(191,151,255,.34);border-radius:12px;font-size:12px;opacity:.8;">Image blocked</div>', out) |
| 463 | |
| 464 | if embedded: |
| 465 | return out |
| 466 | |
| 467 | mini = f""" |
| 468 | <style> |
| 469 | #freeinternet-mini{{position:sticky;top:0;z-index:2147483000;padding:8px 10px;display:flex;gap:8px;align-items:center;flex-wrap:wrap;border-bottom:1px solid rgba(191,151,255,.24);font:12px system-ui,sans-serif;background:rgba(11,8,17,.92);backdrop-filter:blur(10px);color:#f4f1fb}} |
| 470 | #freeinternet-mini a{{color:inherit;text-decoration:none;padding:7px 10px;border-radius:10px;border:1px solid rgba(191,151,255,.20);background:rgba(255,255,255,.04)}} |
| 471 | #freeinternet-mini .url{{opacity:.72;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:min(48vw,420px)}} |
| 472 | @media (prefers-color-scheme: light){{#freeinternet-mini{{background:rgba(246,242,251,.92);color:#21172f}} #freeinternet-mini a{{background:rgba(255,255,255,.72)}}}} |
| 473 | </style> |
| 474 | <div id="freeinternet-mini"> |
| 475 | <a href="{url_for('browser_home')}">Browser</a> |
| 476 | <a href="{url_for('browser_save_bookmark')}?url={urllib.parse.quote(base_url, safe='')}&title=Saved">Bookmark</a> |
| 477 | <span class="url">{html.escape(base_url)}</span> |
| 478 | </div> |
| 479 | """ |
| 480 | body_idx = out.lower().find("<body") |
| 481 | if body_idx != -1: |
| 482 | close_idx = out.find(">", body_idx) |
| 483 | if close_idx != -1: |
| 484 | return out[:close_idx+1] + mini + out[close_idx+1:] |
| 485 | return mini + out |
| 486 | |
| 487 | def page_title_from_html(raw_html: str, fallback: str) -> str: |
| 488 | m = TITLE_RE.search(raw_html or "") |