|
1 | 1 | import 'webextension-polyfill'; |
2 | 2 |
|
| 3 | +// Helper to check if an element is visible |
| 4 | +export function isElementVisible(el: Element): boolean { |
| 5 | + if (!(el instanceof HTMLElement)) return false; |
| 6 | + const style = window.getComputedStyle(el); |
| 7 | + return ( |
| 8 | + style.display !== 'none' && |
| 9 | + style.visibility !== 'hidden' && |
| 10 | + style.opacity !== '0' && |
| 11 | + el.offsetWidth > 0 && |
| 12 | + el.offsetHeight > 0 |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +// Helper to check if an element contains only text nodes as children |
| 17 | +export function hasOnlyTextNodeChildren(element: HTMLElement): boolean { |
| 18 | + if (!element.hasChildNodes()) { |
| 19 | + return true; // No children, so effectively only text nodes (or none) |
| 20 | + } |
| 21 | + for (let i = 0; i < element.childNodes.length; i++) { |
| 22 | + const child = element.childNodes[i]; |
| 23 | + if (child.nodeType !== Node.TEXT_NODE) { |
| 24 | + return false; // Found a non-text node child |
| 25 | + } |
| 26 | + } |
| 27 | + return true; // All children are text nodes |
| 28 | +} |
| 29 | + |
3 | 30 | // Function to get a robust XPath for an element |
4 | 31 | export function getElementXPath(element: Element): string { |
5 | 32 | if (element.id !== '') { |
@@ -83,18 +110,146 @@ export function extractFormData() { |
83 | 110 |
|
84 | 111 | // Helper functions for specific actions |
85 | 112 | export function fillTextInput(element: HTMLInputElement | HTMLTextAreaElement, value: string) { |
| 113 | + console.log(`Filling text input with selector: ${getElementXPath(element)} with value: ${value}`); |
86 | 114 | element.value = value; |
87 | 115 | element.dispatchEvent(new Event('input', { bubbles: true })); |
88 | 116 | element.dispatchEvent(new Event('change', { bubbles: true })); |
89 | 117 | } |
90 | 118 |
|
91 | 119 | export function selectDropdownOption(element: HTMLSelectElement, value: string) { |
| 120 | + console.log(`Selecting dropdown option with selector: ${getElementXPath(element)} with value: ${value}`); |
92 | 121 | element.value = value; |
93 | 122 | element.dispatchEvent(new Event('change', { bubbles: true })); |
94 | 123 | } |
95 | 124 |
|
96 | 125 | export function checkRadioOrCheckbox(element: HTMLInputElement, checked: boolean) { |
| 126 | + console.log(`Checking radio/checkbox with selector: ${getElementXPath(element)} with checked: ${checked}`); |
97 | 127 | element.checked = checked; |
98 | 128 | element.dispatchEvent(new Event('click', { bubbles: true })); // Click often triggers more reliably for checkboxes/radios |
99 | 129 | element.dispatchEvent(new Event('change', { bubbles: true })); |
100 | 130 | } |
| 131 | + |
| 132 | +// Function to extract all relevant page content (form elements and surrounding text) |
| 133 | +export function extractPageContext() { |
| 134 | + const pageContextItems: any[] = []; |
| 135 | + let domOrderCounter = 0; |
| 136 | + |
| 137 | + // Whitelist of tags from which to extract general text content |
| 138 | + const TEXT_EXTRACTION_TAGS_WHITELIST = new Set([ |
| 139 | + 'LABEL', 'DIV', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'SPAN', 'LI', 'TD', 'TH', 'A', 'BUTTON' |
| 140 | + ]); |
| 141 | + |
| 142 | + const traverse = (node: Node) => { |
| 143 | + if (!node) return; |
| 144 | + |
| 145 | + // Skip script, style, noscript, and comment nodes entirely |
| 146 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 147 | + const el = node as HTMLElement; |
| 148 | + const tagName = el.tagName.toUpperCase(); |
| 149 | + if (['SCRIPT', 'STYLE', 'NOSCRIPT'].includes(tagName)) { |
| 150 | + return; // Skip this node and its children |
| 151 | + } |
| 152 | + } else if (node.nodeType === Node.COMMENT_NODE) { |
| 153 | + return; // Skip comment nodes |
| 154 | + } |
| 155 | + |
| 156 | + const currentDomOrder = domOrderCounter++; |
| 157 | + |
| 158 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 159 | + const el = node as HTMLElement; |
| 160 | + |
| 161 | + if (isElementVisible(el)) { // Keep visibility check |
| 162 | + // Check if it's a form element |
| 163 | + if (el.matches('input, textarea, select')) { |
| 164 | + const data: any = { |
| 165 | + tagName: el.tagName.toLowerCase(), |
| 166 | + type: (el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement).type || el.tagName.toLowerCase(), |
| 167 | + id: el.id, |
| 168 | + name: (el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement).name, |
| 169 | + placeholder: ('placeholder' in el) ? (el as HTMLInputElement | HTMLTextAreaElement).placeholder : undefined, |
| 170 | + value: (el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement).value, |
| 171 | + selector: getElementXPath(el), |
| 172 | + domOrder: currentDomOrder, |
| 173 | + }; |
| 174 | + |
| 175 | + // Get associated label text |
| 176 | + let labelText = ''; |
| 177 | + const inputEl = el as HTMLInputElement; |
| 178 | + if (inputEl.labels && inputEl.labels.length > 0) { |
| 179 | + const firstLabel = inputEl.labels[0]; |
| 180 | + if (firstLabel) { |
| 181 | + labelText = firstLabel.textContent || ''; |
| 182 | + } |
| 183 | + } else { |
| 184 | + let current = el.previousElementSibling; |
| 185 | + while (current) { |
| 186 | + if (current.tagName.toLowerCase() === 'label') { |
| 187 | + labelText = current.textContent || ''; |
| 188 | + break; |
| 189 | + } |
| 190 | + current = current.previousElementSibling; |
| 191 | + } |
| 192 | + const parent = el.parentElement; |
| 193 | + if (!labelText && parent?.tagName.toLowerCase() === 'label') { |
| 194 | + labelText = parent.textContent || ''; |
| 195 | + } |
| 196 | + } |
| 197 | + data.labelText = labelText.trim(); |
| 198 | + |
| 199 | + // Get aria-label or aria-labelledby |
| 200 | + data.ariaLabel = el.getAttribute('aria-label'); |
| 201 | + data.ariaLabelledBy = el.getAttribute('aria-labelledby'); |
| 202 | + |
| 203 | + if (el.tagName.toLowerCase() === 'select') { |
| 204 | + data.options = Array.from((el as HTMLSelectElement).options).map(opt => ({ |
| 205 | + text: opt.textContent, |
| 206 | + value: opt.value, |
| 207 | + })); |
| 208 | + } else if (el.type === 'radio' || el.type === 'checkbox') { |
| 209 | + data.checked = (el as HTMLInputElement).checked; |
| 210 | + } |
| 211 | + pageContextItems.push({ type: 'formField', domOrder: currentDomOrder, selector: data.selector, formData: data }); |
| 212 | + } else { |
| 213 | + // Extract text content from non-form elements if visible and in whitelist |
| 214 | + const text = el.textContent?.trim(); |
| 215 | + const tagName = el.tagName.toUpperCase(); |
| 216 | + // Heuristic to avoid capturing text already covered by form field labels or redundant text |
| 217 | + const isFormRelated = el.closest('label, input, textarea, select'); |
| 218 | + // Check if tag is in whitelist, text is meaningful, not form-related, and element only contains text nodes |
| 219 | + if (text && text.length > 1 && TEXT_EXTRACTION_TAGS_WHITELIST.has(tagName) && !isFormRelated && hasOnlyTextNodeChildren(el)) { |
| 220 | + pageContextItems.push({ |
| 221 | + type: 'text', |
| 222 | + domOrder: currentDomOrder, |
| 223 | + selector: getElementXPath(el), |
| 224 | + text: text, |
| 225 | + }); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } else if (node.nodeType === Node.TEXT_NODE) { |
| 230 | + const text = node.textContent?.trim(); |
| 231 | + const parentElement = node.parentElement; |
| 232 | + // Ensure parent is visible, its tag is in whitelist, text is meaningful, not form-related, and parent only contains text nodes |
| 233 | + if (text && text.length > 1 && parentElement && isElementVisible(parentElement)) { |
| 234 | + const parentTagName = parentElement.tagName.toUpperCase(); |
| 235 | + if (TEXT_EXTRACTION_TAGS_WHITELIST.has(parentTagName) && |
| 236 | + !parentElement.matches('input, textarea, select, label, option') && // Avoid text within form elements/labels, and option elements |
| 237 | + hasOnlyTextNodeChildren(parentElement)) { // Add this condition |
| 238 | + pageContextItems.push({ |
| 239 | + type: 'text', |
| 240 | + domOrder: currentDomOrder, |
| 241 | + selector: getElementXPath(parentElement), // Use parent's XPath for context |
| 242 | + text: text, |
| 243 | + }); |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + // Recursively traverse children |
| 249 | + node.childNodes.forEach(traverse); |
| 250 | + }; |
| 251 | + |
| 252 | + traverse(document.body); // Start traversal from the body |
| 253 | + |
| 254 | + return pageContextItems; |
| 255 | +} |
0 commit comments