From 22e0a0cbe45a6993a10cc9497f2c81d0781d9810 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 10 Apr 2023 17:10:20 +0800 Subject: [PATCH] Add some new requirements to selecting scrollable elements 1. The element must have some height to its bounding rect, this can prevent strange things like meta tags from being used as the anchor. 2. The element must be a

tag: the intent is to take the user to a readable position, so this should fit better and eliminates using things like outer container elements that could span the entire document. --- packages/web/lib/anchorElements.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/web/lib/anchorElements.ts b/packages/web/lib/anchorElements.ts index faba6f92c..e028e8689 100644 --- a/packages/web/lib/anchorElements.ts +++ b/packages/web/lib/anchorElements.ts @@ -11,12 +11,16 @@ export const getTopOmnivoreAnchorElement = ( ): string | undefined => { let topVisibleRect: Element | undefined = undefined const anchors = Array.from( - document.querySelectorAll(`[data-omnivore-anchor-idx]`) + document.querySelectorAll(`p[data-omnivore-anchor-idx]`) ).reverse() for (const anchor of anchors) { const rect = anchor.getBoundingClientRect() - if (rect.top >= 0 && rect.bottom <= articleContentElement.clientHeight) { + if ( + rect.height > 0 && + rect.top >= 50 && + rect.bottom <= articleContentElement.clientHeight + ) { if ( topVisibleRect && topVisibleRect.getBoundingClientRect().top < rect.top