From c4253d5711f3268bdc30bf14f6b711375b0cb96a Mon Sep 17 00:00:00 2001 From: ashfame Date: Thu, 19 Dec 2024 16:06:54 +0400 Subject: [PATCH 1/2] define getElementXPath function to return full xpath --- src/extension/content.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/extension/content.ts b/src/extension/content.ts index 18be4647..5db907d9 100644 --- a/src/extension/content.ts +++ b/src/extension/content.ts @@ -138,3 +138,36 @@ function removeStyle() { } highlightedElement.style.outline = ''; } + +function getElementXPath( element: Element ): string { + if ( element === document.body ) { + return '/html/body'; + } + + let xpath = ''; + let currentElement: Element | null = element; + + while ( currentElement !== null ) { + let sibling = currentElement.previousSibling; + let count = 1; + + while ( sibling !== null ) { + if ( + sibling.nodeType === Node.ELEMENT_NODE && + sibling.nodeName === currentElement.nodeName + ) { + count++; + } + sibling = sibling.previousSibling; + } + + const tagName = currentElement.nodeName.toLowerCase(); + const index = count > 1 ? `[${ count }]` : ''; + + xpath = `/${ tagName }${ index }${ xpath }`; + + currentElement = currentElement.parentElement; + } + + return xpath; +} From c1267e75821e2f340e901c64f5357494314556a5 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Date: Thu, 19 Dec 2024 22:24:05 +0400 Subject: [PATCH 2/2] suppress unused function notice by linter Co-authored-by: Paulo Pinto --- src/extension/content.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/extension/content.ts b/src/extension/content.ts index 5db907d9..0c1f5c0b 100644 --- a/src/extension/content.ts +++ b/src/extension/content.ts @@ -139,6 +139,7 @@ function removeStyle() { highlightedElement.style.outline = ''; } +// eslint-disable-next-line @typescript-eslint/no-unused-vars function getElementXPath( element: Element ): string { if ( element === document.body ) { return '/html/body';