debug: Add comprehensive logging to JavaScript scroll tracking
- Added console.log statements in JavaScript for scroll events - Added Swift print statements in message handler - Added logging in BookmarkDetailLegacyView onScroll callback - Logs cover: JS initialization, scroll events, message passing, Swift handling This will help diagnose why scroll events aren't being captured.
This commit is contained in:
parent
32dbab400e
commit
0a53705df1
@ -57,6 +57,7 @@ struct BookmarkDetailLegacyView: View {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onScroll: { scrollPercent in
|
onScroll: { scrollPercent in
|
||||||
|
print("🔥 BookmarkDetailLegacyView onScroll callback: \(String(format: "%.3f", scrollPercent)) (\(String(format: "%.1f", scrollPercent * 100))%)")
|
||||||
readingProgress = scrollPercent
|
readingProgress = scrollPercent
|
||||||
viewModel.debouncedUpdateReadProgress(id: bookmarkId, progress: scrollPercent, anchor: nil)
|
viewModel.debouncedUpdateReadProgress(id: bookmarkId, progress: scrollPercent, anchor: nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -264,17 +264,28 @@ struct WebView: UIViewRepresentable {
|
|||||||
document.querySelectorAll('img').forEach(img => {
|
document.querySelectorAll('img').forEach(img => {
|
||||||
img.addEventListener('load', debouncedHeightUpdate);
|
img.addEventListener('load', debouncedHeightUpdate);
|
||||||
});
|
});
|
||||||
|
console.log('🟢 WebView JavaScript loaded');
|
||||||
|
console.log('🔵 Document scroll enabled:', document.body.style.overflow);
|
||||||
|
console.log('🔵 Window innerHeight:', window.innerHeight);
|
||||||
|
console.log('🔵 Document scrollHeight:', document.documentElement.scrollHeight);
|
||||||
|
|
||||||
let lastSent = { value: 0 };
|
let lastSent = { value: 0 };
|
||||||
window.addEventListener('scroll', function() {
|
window.addEventListener('scroll', function() {
|
||||||
|
console.log('📜 Scroll event fired!');
|
||||||
isScrolling = true;
|
isScrolling = true;
|
||||||
|
|
||||||
let scrollTop = window.scrollY || document.documentElement.scrollTop;
|
let scrollTop = window.scrollY || document.documentElement.scrollTop;
|
||||||
let docHeight = document.documentElement.scrollHeight - window.innerHeight;
|
let docHeight = document.documentElement.scrollHeight - window.innerHeight;
|
||||||
let scrollPercent = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0;
|
let scrollPercent = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0;
|
||||||
|
|
||||||
|
console.log('📊 scrollTop:', scrollTop, 'docHeight:', docHeight, 'scrollPercent:', scrollPercent.toFixed(2) + '%');
|
||||||
|
|
||||||
if (Math.abs(scrollPercent - lastSent.value) >= 3) {
|
if (Math.abs(scrollPercent - lastSent.value) >= 3) {
|
||||||
|
console.log('✅ Sending scroll progress:', (scrollPercent / 100).toFixed(3));
|
||||||
window.webkit.messageHandlers.scrollProgress.postMessage(scrollPercent / 100);
|
window.webkit.messageHandlers.scrollProgress.postMessage(scrollPercent / 100);
|
||||||
lastSent.value = scrollPercent;
|
lastSent.value = scrollPercent;
|
||||||
|
} else {
|
||||||
|
console.log('⏸️ Skipping (change < 3%): ', Math.abs(scrollPercent - lastSent.value).toFixed(2) + '%');
|
||||||
}
|
}
|
||||||
|
|
||||||
clearTimeout(scrollTimeout);
|
clearTimeout(scrollTimeout);
|
||||||
@ -282,7 +293,9 @@ struct WebView: UIViewRepresentable {
|
|||||||
isScrolling = false;
|
isScrolling = false;
|
||||||
debouncedHeightUpdate();
|
debouncedHeightUpdate();
|
||||||
}, 200);
|
}, 200);
|
||||||
});
|
}, { passive: true });
|
||||||
|
|
||||||
|
console.log('🎯 Scroll listener attached');
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -361,12 +374,16 @@ class WebViewCoordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
||||||
|
print("🔔 Swift received message: \(message.name)")
|
||||||
|
|
||||||
if message.name == "heightUpdate", let height = message.body as? CGFloat {
|
if message.name == "heightUpdate", let height = message.body as? CGFloat {
|
||||||
|
print("📏 Height update: \(height)px")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.handleHeightUpdate(height: height)
|
self.handleHeightUpdate(height: height)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if message.name == "scrollProgress", let progress = message.body as? Double {
|
if message.name == "scrollProgress", let progress = message.body as? Double {
|
||||||
|
print("📊 Swift received scroll progress: \(String(format: "%.3f", progress)) (\(String(format: "%.1f", progress * 100))%)")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.handleScrollProgress(progress: progress)
|
self.handleScrollProgress(progress: progress)
|
||||||
}
|
}
|
||||||
@ -387,25 +404,28 @@ class WebViewCoordinator: NSObject, WKNavigationDelegate, WKScriptMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func handleScrollProgress(progress: Double) {
|
private func handleScrollProgress(progress: Double) {
|
||||||
|
print("🎯 handleScrollProgress called with: \(String(format: "%.3f", progress))")
|
||||||
|
|
||||||
let now = Date()
|
let now = Date()
|
||||||
let timeDelta = now.timeIntervalSince(lastScrollTime)
|
let timeDelta = now.timeIntervalSince(lastScrollTime)
|
||||||
|
|
||||||
// Calculate scroll velocity to detect fast scrolling
|
// Calculate scroll velocity to detect fast scrolling
|
||||||
if timeDelta > 0 {
|
if timeDelta > 0 {
|
||||||
scrollVelocity = abs(progress) / timeDelta
|
scrollVelocity = abs(progress) / timeDelta
|
||||||
}
|
}
|
||||||
|
|
||||||
lastScrollTime = now
|
lastScrollTime = now
|
||||||
isScrolling = true
|
isScrolling = true
|
||||||
|
|
||||||
// Longer delay for scroll end detection, especially during fast scrolling
|
// Longer delay for scroll end detection, especially during fast scrolling
|
||||||
let scrollEndDelay: TimeInterval = scrollVelocity > 2.0 ? 0.8 : 0.5
|
let scrollEndDelay: TimeInterval = scrollVelocity > 2.0 ? 0.8 : 0.5
|
||||||
|
|
||||||
scrollEndTimer?.invalidate()
|
scrollEndTimer?.invalidate()
|
||||||
scrollEndTimer = Timer.scheduledTimer(withTimeInterval: scrollEndDelay, repeats: false) { [weak self] _ in
|
scrollEndTimer = Timer.scheduledTimer(withTimeInterval: scrollEndDelay, repeats: false) { [weak self] _ in
|
||||||
self?.handleScrollEnd()
|
self?.handleScrollEnd()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("🚀 Calling onScroll callback with progress: \(String(format: "%.3f", progress))")
|
||||||
onScroll?(progress)
|
onScroll?(progress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user