1010; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1111; ⚠️ to make this work, download pdfJS library from https://mozilla.github.io/pdf.js/
1212; for example:
13- ; https://github.com/mozilla/pdf.js/releases/download/v5.4.530 /pdfjs-5.4.530 -dist.zip
13+ ; https://github.com/mozilla/pdf.js/releases/download/v5.4.624 /pdfjs-5.4.624 -dist.zip
1414; and unzip to: @ScriptDir & "\JS_Lib\pdfjs\"
1515; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1616
@@ -50,10 +50,19 @@ Func _Example()
5050 #EndRegion ; GUI CREATION
5151
5252 ; navigate to the page
53- __SetupStaticPDF($oWeb , @ScriptDir & " \invoice-plugin-sample.pdf" , True , True )
53+ __SetupStaticPDF($oWeb , @ScriptDir & " \invoice-plugin-sample.pdf" , True , False , True )
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 ()
@@ -111,7 +120,7 @@ Func __MyEVENTS_Bridge_OnMessageReceived($sMsg)
111120 EndIf
112121EndFunc ; ==>__MyEVENTS_Bridge_OnMessageReceived
113122
114- Func __SetupStaticPDF(ByRef $oWeb , $s_PDF_Path , $bBlockLinks = False , $bBlockSelection = False )
123+ Func __SetupStaticPDF(ByRef $oWeb , $s_PDF_Path , $bBlockLinks = False , $bBlockSelection = False , $bShowToolbar = False )
115124 ; 🏆 https://mozilla.github.io/pdf.js/
116125
117126 Local $sBlockLinksJS = " "
@@ -168,21 +177,63 @@ Func __SetupStaticPDF(ByRef $oWeb, $s_PDF_Path, $bBlockLinks = False, $bBlockSel
168177 " setTimeout(runExtraction, 500);" & _
169178 " }" & _
170179 " } catch (e) {" & _
171- " window.chrome.webview.postMessage('ERROR| ' + e.message);" & _
180+ " window.chrome.webview.postMessage('JS_ERROR|extractPDFText() SLN= " & @ScriptLineNumber & " ' + e.message);" & _
172181 " }" & _
173182 " };" & _
174183 " runExtraction();" & _
175184 " };" & _
176185 " /* 3. Style Injection */ " & _
177186 " window.addEventListener('DOMContentLoaded', () => {" & _
178187 " const style = document.createElement('style');" & _
179- " style.innerHTML = '#toolbarContainer, #sidebarContainer { display: none !important; } ' + " & _
188+ " style.innerHTML = " & (( $bShowToolbar ) ? ( " " ) : ( " '#toolbarContainer, #sidebarContainer { display: none !important; } ' + " )) & _
180189 " '#viewerContainer { top: 0 !important; bottom: 0 !important; overflow: hidden !important; } ' + " & _
181190 " '" & $sBlockLinksCSS & " ' + " & _
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
@@ -194,6 +245,10 @@ Func __SetupStaticPDF(ByRef $oWeb, $s_PDF_Path, $bBlockLinks = False, $bBlockSel
194245 ConsoleWrite (" - $s_Viewer_URL= " & $s_Viewer_URL & @CRLF )
195246
196247 $oWeb .Navigate($s_Viewer_URL )
248+ ; ~ _NetWebView2_Navigate($oWeb, $s_Viewer_URL, $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, 5000)
249+ ConsoleWrite (" ! we're done with navigation, but check how many more messages there are below. SLN=" & @ScriptLineNumber & @CRLF )
250+
251+ MsgBox ($MB_TOPMOST , " TEST #" & @ScriptLineNumber , ' Wait for all messages to full loading PDF by pdf.js' )
197252
198253 ; $oWeb.IsZoomControlEnabled = False ; <--- It doesn't work in PDF. 👈
199254 $oWeb .DisableBrowserFeatures()
0 commit comments