Skip to content

Commit

Permalink
fix(export-element): better matching for element node selection
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Dec 19, 2024
1 parent 31c95f1 commit 05c0d65
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions scripts/export-element/index.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
});

Expand Down

0 comments on commit 05c0d65

Please sign in to comment.