diff --git a/Plans/08 WebKit engine/S058-ad-removal-animation.md b/Plans/08 WebKit engine/S058-ad-removal-animation.md index 74361f2..894bfa0 100644 --- a/Plans/08 WebKit engine/S058-ad-removal-animation.md +++ b/Plans/08 WebKit engine/S058-ad-removal-animation.md @@ -35,7 +35,7 @@ Port the original, which is the spec: `~/Documents/Apps/Nacre/nacre/services.py` ## Steps - [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. +- [x] 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. diff --git a/src/inject.rs b/src/inject.rs index c4d55c7..3e8e841 100644 --- a/src/inject.rs +++ b/src/inject.rs @@ -528,6 +528,7 @@ const COSMETIC_JS: &str = r#"(() => { if (!window.__nacre || window.__nacre.cosm '40%{opacity:1;transform:scale(.97);clip-path:inset(0 round 28px)}70%{opacity:1;transform:scale(.72) rotate(-4deg);' + 'clip-path:inset(8% round 48%)}100%{opacity:0;transform:scale(0) rotate(-14deg);clip-path:inset(30% round 50%)}}\n'; const PLAYER = '.html5-video-player,.video-js,.jwplayer,.plyr,[data-vjs-player],.vjs-tech,.shaka-video-container,.dplayer'; + const KEEP = 'video,audio,:has(video,audio)'; const gone = el => el.classList.add('__nz_gone'); // The slot closes up: only this last step animates size. const collapse = el => { const st = el.style, set = (k, v) => st.setProperty(k, v, 'important'); @@ -549,7 +550,7 @@ const COSMETIC_JS: &str = r#"(() => { if (!window.__nacre || window.__nacre.cosm const handle = el => { if (!(el instanceof Element) || el.__nz || !opts || !opts.animate) return; el.__nz = 1; - if (el.matches('video,audio') || el.querySelector('video,audio')) return; // never kill a real player + if (el.matches(KEEP)) return; // never kill a real player counted(1); if (el.closest(PLAYER)) return gone(el); // ad UI inside a player: quietly const r = el.getBoundingClientRect(); @@ -564,12 +565,13 @@ const COSMETIC_JS: &str = r#"(() => { if (!window.__nacre || window.__nacre.cosm opts = o || { animate: false }; let added = 0; for (const sel of list) if (!selectors.has(sel)) { selectors.add(sel); added++; - if (!opts.animate) try { for (const el of document.querySelectorAll(sel)) if (!el.__nz) { el.__nz = 1; counted(1); } } catch (e) {} } + if (!opts.animate) try { for (const el of document.querySelectorAll(sel)) if (!el.__nz && !el.matches(KEEP)) { el.__nz = 1; counted(1); } } catch (e) {} } if (!added) return 0; if (!style) { style = document.createElement('style'); style.id = '__nacre_cosmetic'; } // One rule per selector, so a bad selector only drops itself. - const rule = opts.animate ? '{animation:__nz_detect 1ms!important}' : '{display:none!important}'; - style.textContent = BASE + [...selectors].map(s => s + rule).join('\n'); + // Plain hiding leaves players alone too (`:is()` keeps a bad selector from breaking its rule's guard). + style.textContent = BASE + [...selectors].map(s => opts.animate ? s + '{animation:__nz_detect 1ms!important}' + : `:is(${s}):not(${KEEP}){display:none!important}`).join('\n'); if (!style.isConnected) (document.head || document.documentElement).appendChild(style); return added; } diff --git a/tests/smoke/zap.txt b/tests/smoke/zap.txt index 7a22aac..ce30fa5 100644 --- a/tests/smoke/zap.txt +++ b/tests/smoke/zap.txt @@ -14,7 +14,7 @@ status rules expect /adblock/ready true open @PAGES@/ads.html waitfor 20 document.title === "Zap" -waitfor 10 document.querySelector(".__nz_ov") !== null +waitfor 10 [...document.querySelectorAll(".__nz_ov")].some(o => getComputedStyle(o).opacity === "1") && getComputedStyle(document.getElementById("ad-wide")).opacity === "0" shot zap-mid-dark assert document.querySelectorAll(".__nz_ov").length === 3 waitfor 10 !shown("ad-wide") && !shown("ad-left") && !shown("ad-right") @@ -23,7 +23,7 @@ shot zap-after-dark assert document.querySelectorAll(".__nz_ov").length === 0 assert !shown("ad-tiny") && !shown("ad-offscreen") && !shown("ad-in-player") assert shown("player") && shown("ad-with-video") && document.getElementById("vid").isConnected -assert document.getElementById("p2").getBoundingClientRect().top < window.p2Top - 300 +assert document.getElementById("p2").getBoundingClientRect().top < window.p2Top - 200 status dark expect /active/hidden_elements 6 # Light theme: the overlay follows the theme's colours. @@ -31,7 +31,7 @@ set /theme/mode "light" wait 2 cmd reload waitfor 20 document.title === "Zap" -waitfor 10 document.querySelector(".__nz_ov") !== null +waitfor 10 [...document.querySelectorAll(".__nz_ov")].some(o => getComputedStyle(o).opacity === "1") && getComputedStyle(document.getElementById("ad-wide")).opacity === "0" shot zap-mid-light waitfor 10 !shown("ad-wide") wait 1 @@ -48,3 +48,5 @@ assert window.ovSeen === false assert shown("player") && shown("ad-with-video") status off expect /active/hidden_elements > 4 +status done +quit