Skip to content

Commit e522e37

Browse files
MaxGhenisclaude
andcommitted
Add wheel event simulation to help trigger Intersection Observer
Dispatch WheelEvent in addition to scroll event during programmatic scrolling. This simulates natural user scrolling which may help Intersection Observer-based libraries detect visibility changes. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 00dad4e commit e522e37

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

background.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,35 @@ async function injectScrollScript(tabId, duration) {
179179

180180
let intervalId = null;
181181

182+
// Calculate pixels per tick based on duration
183+
const pixelsPerTick = totalHeight / (totalMs / INTERVAL_MS);
184+
let currentScroll = 0;
185+
182186
function tick() {
183-
const elapsed = Date.now() - startTime;
184-
const progress = Math.min(elapsed / totalMs, 1);
185-
const targetY = totalHeight * progress;
187+
currentScroll += pixelsPerTick;
188+
if (currentScroll >= totalHeight) {
189+
currentScroll = totalHeight;
190+
}
191+
192+
// Use wheel event simulation for more natural scroll that triggers IO
193+
const wheelEvent = new WheelEvent('wheel', {
194+
deltaY: pixelsPerTick,
195+
deltaMode: 0, // pixels
196+
bubbles: true,
197+
cancelable: true,
198+
});
186199

187200
if (scrollContainer) {
188-
scrollContainer.scrollTop = targetY;
201+
scrollContainer.scrollTop = currentScroll;
202+
scrollContainer.dispatchEvent(wheelEvent);
189203
scrollContainer.dispatchEvent(new Event('scroll', { bubbles: true }));
190204
} else {
191-
window.scrollTo({ top: targetY, behavior: 'instant' });
205+
window.scrollTo({ top: currentScroll, behavior: 'instant' });
206+
document.documentElement.dispatchEvent(wheelEvent);
192207
window.dispatchEvent(new Event('scroll'));
193208
}
194209

195-
if (progress >= 1) {
210+
if (currentScroll >= totalHeight) {
196211
clearInterval(intervalId);
197212
// Restore original scroll behavior
198213
const override = document.getElementById(overrideId);

0 commit comments

Comments
 (0)