From b2180149139fc07c8d2fac6ef4f136cb01443af3 Mon Sep 17 00:00:00 2001 From: Gold Holk Date: Wed, 7 Sep 2022 12:47:31 +0800 Subject: [PATCH] Try to make the highlight's position always correct This will reposition the highlight whenever user focus other match, but the scroll behavior may work bad sometimes. --- src/content/finding.ts | 69 +++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/src/content/finding.ts b/src/content/finding.ts index 29fde6c5..c042d815 100644 --- a/src/content/finding.ts +++ b/src/content/finding.ts @@ -26,7 +26,7 @@ function getFindHost() { class FindHighlight extends HTMLSpanElement { public top = Infinity - constructor(private rects, public range: Range) { + constructor(public range: Range) { super() { // https://bugzilla.mozilla.org/show_bug.cgi?id=1716685 @@ -38,32 +38,45 @@ class FindHighlight extends HTMLSpanElement { this.style.position = "absolute" this.style.top = "0px" this.style.left = "0px" - for (const rect of rects) { - if (rect.top < this.top) { - this.top = rect.top + this.updateRectsPosition() + ;(this as any).unfocus() + } + + updateRectsPosition() { + const rects = this.getClientRects() + this.top = Infinity + const windowTop = window.pageYOffset + const windowLeft = window.pageXOffset + for (let i=0; i