@@ -54,6 +54,15 @@ Func _Example()
5454
5555 _NetWebView2_ExecuteScript($oWeb , " extractPDFText();" , $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET )
5656
57+ _NetWebView2_ExecuteScript($oWeb , " highlightSpansContainingText('January 31, 2016', 'red', 'yellow');" , $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET )
58+ MsgBox ($MB_TOPMOST , " TEST #" & @ScriptLineNumber , " after" & @CRLF & " highlightSpansContainingText('January 31, 2016', 'red', 'yellow');" )
59+
60+ _NetWebView2_ExecuteScript($oWeb , " highlightSpansContainingText('Total Due', 'red', 'lightblue');" , $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET )
61+ MsgBox ($MB_TOPMOST , " TEST #" & @ScriptLineNumber , " after" & @CRLF & " highlightSpansContainingText('Total Due', 'red', 'lightblue');" )
62+
63+ _NetWebView2_ExecuteScript($oWeb , " removeHighlights('January 31, 2016');" , $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET )
64+ MsgBox ($MB_TOPMOST , " TEST #" & @ScriptLineNumber , " after" & @CRLF & " removeHighlights('January 31, 2016');" )
65+
5766 ; Main Loop
5867 While 1
5968 Switch GUIGetMsg ()
@@ -182,7 +191,49 @@ Func __SetupStaticPDF(ByRef $oWeb, $s_PDF_Path, $bBlockLinks = False, $bBlockSel
182191 " '" & $sSelectionCSS & " ' + " & _
183192 " ' ::-webkit-scrollbar { display: none !important; }';" & _
184193 " document.head.appendChild(style);" & _
185- " });"
194+ " });" & _
195+ " /* 4. HighLight text */ " & _
196+ " function highlightSpansContainingText(searchText, borderColor = 'red', backgroundColor = 'yellow') {" & _
197+ " if (!searchText || typeof searchText !== 'string') return;" & _
198+ " const normalize = str => str.replace(/\s+/g, ' ').trim().toLowerCase();" & _
199+ " const normalizedSearch = normalize(searchText);" & _
200+ " const spans = document.querySelectorAll('span');" & _
201+ " spans.forEach(span => {" & _
202+ " // Reset previous highlights in this SPAN" & _
203+ " if (!span.dataset.originalText) {" & _
204+ " span.dataset.originalText = span.innerHTML; // preserve original content" & _
205+ " } else {" & _
206+ " span.innerHTML = span.dataset.originalText; // restore previous state" & _
207+ " }" & _
208+ " const spanText = span.textContent;" & _
209+ " const spanTextNormalized = normalize(spanText);" & _
210+ " if (spanTextNormalized.includes(normalizedSearch)) {" & _
211+ " const regex = new RegExp(searchText, 'gi');" & _
212+ " span.innerHTML = spanText.replace(regex, match => {" & _
213+ " return `<span data-highlight-by-au3udf='true' style='border:1px solid ${borderColor}; background-color:${backgroundColor}; color:black; padding:1px;'>${match}</span>`;" & _
214+ " });" & _
215+ " }" & _
216+ " });" & _
217+ " };" & _
218+ " function removeHighlights(searchText) {" & _
219+ " if (!searchText || typeof searchText !== 'string') return;" & _
220+ " const normalize = str => str.replace(/\s+/g, ' ').trim().toLowerCase();" & _
221+ " const normalizedSearch = normalize(searchText);" & _
222+ " // Find all highlighted SPANs" & _
223+ " const highlightedSpans = document.querySelectorAll('span[data-highlight-by-au3udf=\'true\']');" & _
224+ " highlightedSpans.forEach(innerSpan => {" & _
225+ " const parentSpan = innerSpan.parentElement;" & _
226+ " if (!parentSpan || !parentSpan.dataset.originalText) return;" & _
227+ " // Check if the highlighted fragment contains searchText" & _
228+ " const spanText = innerSpan.textContent;" & _
229+ " if (normalize(spanText).includes(normalizedSearch)) {" & _
230+ " // Restore parent's original content" & _
231+ " parentSpan.innerHTML = parentSpan.dataset.originalText;" & _
232+ " delete parentSpan.dataset.originalText;" & _
233+ " }" & _
234+ " });" & _
235+ " };" & _
236+ " "
186237
187238 $oWeb .AddInitializationScript($sCleanupJS )
188239
0 commit comments