From 05c0d6579610887ee2ec3e36e62bee5f8f5f1fba Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi-Ugbe Date: Wed, 18 Dec 2024 21:53:52 -0500 Subject: [PATCH] fix(export-element): better matching for element node selection --- scripts/export-element/index.user.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/scripts/export-element/index.user.ts b/scripts/export-element/index.user.ts index ceebbbd3..28bfb7df 100644 --- a/scripts/export-element/index.user.ts +++ b/scripts/export-element/index.user.ts @@ -28,9 +28,14 @@ return null; } - /** replace all continous newlines and whitespace in text with a single space */ + /** Replace all continous newlines and whitespace in text with a single space */ function sanitizeText(text: string) { - return text.replace(/\s+/g, " ").trim(); + return text?.replace(/\s+/g, " ").trim() ?? ""; + } + + /** Checks that text contains all words */ + function containsAll(text: string, words: string[]) { + return words.every((word) => text.includes(word)); } // Add context menu to user script @@ -44,10 +49,11 @@ )?.value?.createRange?.()?.text ?? ""; const matchText = sanitizeText(selectedText); + const matchWords = matchText.split(" "); params.target = findLastNodeWithPredicate(event.target as Node, (node) => { const nodeText = sanitizeText((node as HTMLElement).innerText); - return nodeText.includes(matchText); + return containsAll(nodeText, matchWords); }); });