From 25f7fd2b420a0b4d0aa606c2e9ddeb1e05221b6f Mon Sep 17 00:00:00 2001 From: astar76 Date: Sat, 14 Nov 2026 07:31:21 -0600 Subject: [PATCH] S058: WIP zap port, test page and smoke Co-Authored-By: Claude Opus 5.5 --- .../S058-ad-removal-animation.md | 8 +- src/actions.rs | 4 +- src/app.rs | 20 +++- src/inject.rs | 92 ++++++++++++++----- src/theme.rs | 33 ++++++- tests/smoke/pages/ads.html | 35 +++++++ tests/smoke/zap.txt | 50 ++++++++++ 7 files changed, 210 insertions(+), 32 deletions(-) create mode 100644 tests/smoke/pages/ads.html create mode 100644 tests/smoke/zap.txt diff --git a/Plans/08 WebKit engine/S058-ad-removal-animation.md b/Plans/08 WebKit engine/S058-ad-removal-animation.md index 91bbad4..74361f2 100644 --- a/Plans/08 WebKit engine/S058-ad-removal-animation.md +++ b/Plans/08 WebKit engine/S058-ad-removal-animation.md @@ -33,8 +33,8 @@ Port the original, which is the spec: `~/Documents/Apps/Nacre/nacre/services.py` (grep `__ANIMATE__`), the S049 PROGRESS entry (cosmetic-only engine on WebKit). ## Steps -- [ ] Port `ZAP_JS` detection + overlay + collapse into the cosmetic script (keep the class/id → selectors round trip), options (animate, ms, bg, badge, off) from the browser per page; badge SVG built in Rust from theme colours. -- [ ] Safety rules (players, sizes, off-screen, max 12, paused sites) and Motion off / setting off → plain hide. +- [x] Port `ZAP_JS` detection + overlay + collapse into the cosmetic script (keep the class/id → selectors round trip), options (animate, ms, bg, badge, off) from the browser per page; badge SVG built in Rust from theme colours. +- [x] Safety rules (players, sizes, off-screen, max 12, paused sites) and Motion off / setting off → plain hide. - [ ] Test page `tests/smoke/pages/ads.html` (a few visible ad boxes matching local cosmetic rules, one inside a fake player, one tiny, one off-screen) + smoke: shots mid-animation (overlay + badge visible) and after (slots collapsed, layout closed), player untouched, Motion off = instant; dark and light. - [ ] Visible run on a real ad-heavy page (e.g. a news site) on DP-1 (D-045): watch it, read the shots; note anything that flickers or jumps. - [ ] PROGRESS entry, INDEX line, tracker, commit `S058: …`, push. @@ -48,3 +48,7 @@ Port the original, which is the spec: `~/Documents/Apps/Nacre/nacre/services.py` ## Handoff (→ PROGRESS.md) What differs from the original, if anything. + +## Resume +Boxes 1–2 done (src/inject.rs `COSMETIC_JS`, `App::zap_options` in src/app.rs, `theme::zap_badge`). Box 3 in +progress: `tests/smoke/pages/ads.html` + `tests/smoke/zap.txt` (`SMOKE_SIZE=1280x900 tools/smoke.sh tests/smoke/zap.txt`). diff --git a/src/actions.rs b/src/actions.rs index 4a232a8..f33bb91 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1707,7 +1707,8 @@ impl App { } let selectors = self.shared.adblock.generic_selectors(&list("classes"), &list("ids"), &exceptions); if !selectors.is_empty() { - self.eval(tab_id, inject::call("cosmetic.hide", &[serde_json::json!(selectors)])); + let options = self.zap_options(w); + self.eval(tab_id, inject::call("cosmetic.hide", &[serde_json::json!(selectors), options])); } }, "cosmetic_removed" => { @@ -2470,7 +2471,6 @@ impl App { || old.web.preload != new.web.preload || old.adblock.enabled != new.adblock.enabled || old.adblock.cosmetic != new.adblock.cosmetic - || old.adblock.animate != new.adblock.animate || old.adblock.youtube != new.adblock.youtube || old.vault.enabled != new.vault.enabled || old.spell.enabled != new.spell.enabled diff --git a/src/app.rs b/src/app.rs index 187f9ef..1cf4dd0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -482,7 +482,6 @@ impl App { preload: s.web.preload, save_data: s.privacy.save_data, cosmetic: s.adblock.enabled && s.adblock.cosmetic, - animate_removal: s.adblock.animate && !crate::perf::still(&s), youtube_ads: s.adblock.enabled && s.adblock.youtube, autofill: s.vault.enabled, spell: s.spell.enabled, @@ -882,13 +881,30 @@ impl App { if selectors.is_empty() { return; } + let Some((w, _)) = self.find_webview(id) else { return }; + let options = self.zap_options(w); if let Some(tab) = self.tab_mut(id) && let Some(web) = &tab.web { - web.webview.evaluate_javascript(inject::call("cosmetic.hide", &[serde_json::json!(selectors)]), |_| {}); + web.webview.evaluate_javascript(inject::call("cosmetic.hide", &[serde_json::json!(selectors), options]), |_| {}); } } + /// How a page removes the ads it hides (`cosmetic.hide`): animated in the window's theme colours unless the + /// setting, Motion or still mode says no. + pub fn zap_options(&self, w: WindowId) -> serde_json::Value { + let s = self.shared.settings.borrow(); + let motion = crate::theme::motion_scale(&s); + let palette = self.windows.get(&w).map(|win| win.ui.palette()).unwrap_or_else(|| crate::ui::WindowUi::default().palette()); + let c = palette.primary_container; + serde_json::json!({ + "animate": s.adblock.animate && motion > 0.0 && !crate::perf::still(&s), + "ms": (900.0 * motion.max(0.3)).round() as u32, + "bg": format!("rgba({},{},{},0.92)", c.r(), c.g(), c.b()), + "badge": crate::theme::zap_badge(palette.primary, palette.on_primary), + }) + } + fn on_favicon(&mut self, id: ViewId) { let Some((w, i)) = self.find_webview(id) else { return }; let win = self.windows.get_mut(&w).expect("found"); diff --git a/src/inject.rs b/src/inject.rs index 6dfea10..c4d55c7 100644 --- a/src/inject.rs +++ b/src/inject.rs @@ -48,7 +48,6 @@ pub struct Options<'a> { pub preload: bool, pub save_data: bool, pub cosmetic: bool, - pub animate_removal: bool, pub youtube_ads: bool, pub autofill: bool, pub spell: bool, @@ -83,7 +82,7 @@ pub fn page_scripts(o: &Options) -> Vec { modules.push(SAVE_DATA_JS.to_owned()); } if o.cosmetic { - modules.push(COSMETIC_JS.replace("__ANIMATE__", if o.animate_removal { "true" } else { "false" })); + modules.push(COSMETIC_JS.to_owned()); } if o.autofill { modules.push(AUTOFILL_JS.to_owned()); @@ -501,10 +500,18 @@ const SAVE_DATA_JS: &str = r#"(() => { try { const c = navigator.connection; if document.addEventListener('DOMContentLoaded', () => { for (const v of document.querySelectorAll('video[autoplay],video[preload]')) { v.preload = 'none'; } }, { once: true }); } catch (e) {} })();"#; -/// Cosmetic filtering: the page's classes and ids go to the browser, which answers with selectors. -/// Removal can animate (squish) for visible elements. +/// Cosmetic filtering: the page's classes and ids go to the browser, which answers with selectors and options +/// (`hide(selectors, {animate, ms, bg, badge})`). +/// +/// Removal is animated like Pie Adblock (a port of the original Nacre's zapper): rules don't hide elements, they +/// give them a 1 ms `__nz_detect` animation, and the page's own `animationstart` hands over each element as it +/// renders (no selector scanning). A cookie-shaped shield badge covers a visible ad, squishes and spins away, the ad +/// fades under it, then its slot collapses. Never touches an element that is or holds `