Skip to content

Commit 5b554c7

Browse files
authored
Merge pull request #85 from mlipok/patch-1
my Beta CleanUp
2 parents 5b22a65 + 9be6e80 commit 5b554c7

7 files changed

Lines changed: 462 additions & 89 deletions

NetWebView2Lib.au3

Lines changed: 371 additions & 71 deletions
Large diffs are not rendered by default.

examples/002-Html_Gui.au3

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ Func _Create_Form(ByRef $oWebV2M, ByRef $oBridge)
5151

5252
$idBlue = GUICtrlCreateLabel("Blue Theme", 10, 10, 100, 30)
5353
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
54+
GUICtrlSetResizing(-1, $GUI_DOCKALL)
5455
GUICtrlSetColor(-1, 0x0078D7)
5556

5657
$idRed = GUICtrlCreateLabel("Red Theme", 120, 10, 100, 30)
5758
GUICtrlSetFont(-1, 12, Default, $GUI_FONTUNDER, "Segoe UI")
59+
GUICtrlSetResizing(-1, $GUI_DOCKALL)
5860
GUICtrlSetColor(-1, 0xFF0000)
5961

6062
; Create WebView2 Manager object and register events
@@ -69,16 +71,16 @@ Func _Create_Form(ByRef $oWebV2M, ByRef $oBridge)
6971
$oWebV2M.IsZoomControlEnabled = False
7072

7173
; Create bridge object and register events
72-
$oBridge = _NetWebView2_GetBridge($oWebV2M, "__MyEVENTS_Bridge_")
74+
$oBridge = _NetWebView2_GetBridge($oWebV2M, "__USER_Events_Bridge_")
7375
#forceref $oBridge
7476

7577
Local $sHTML = "<html><head><meta charset='UTF-8'><style>:" & __FormCSS() & "</style></head><body>" & __FormHTML() & "</body></html>"
76-
$oWebV2M.NavigateToString($sHTML)
78+
_NetWebView2_NavigateToString($oWebV2M, $sHTML, $NETWEBVIEW2_MESSAGE__TITLE_CHANGED, 5000)
7779
GUISetState(@SW_SHOW, $hGUI)
7880
EndFunc ;==>_Create_Form
7981

8082
; Handles data received from the JavaScript 'postMessage'
81-
Func __MyEVENTS_Bridge_OnMessageReceived($oWebV2M, $hGUI, $sMessage) ; fork from __NetWebView2_JSEvents__OnMessageReceived()
83+
Func __USER_Events_Bridge_OnMessageReceived($oWebV2M, $hGUI, $sMessage) ; fork from __NetWebView2_JSEvents__OnMessageReceived()
8284
#forceref $hGUI
8385
__Example_Log(@ScriptLineNumber, "$sMessage=" & $sMessage)
8486

@@ -115,7 +117,7 @@ Func __MyEVENTS_Bridge_OnMessageReceived($oWebV2M, $hGUI, $sMessage) ; fork from
115117
EndIf
116118
EndIf
117119
EndIf
118-
EndFunc ;==>__MyEVENTS_Bridge_OnMessageReceived
120+
EndFunc ;==>__USER_Events_Bridge_OnMessageReceived
119121

120122
; Generates the CSS block with dynamic variables
121123
Func __FormCSS()

examples/004-FileViewerDemo_Loop.au3

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ Func _Example()
3939
; Create the UI
4040
Local $iHeight = 800
4141
Local $hMainGUIWindow = GUICreate("WebView2 .NET Manager - Demo: " & @ScriptName, 1100, $iHeight, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPCHILDREN))
42-
Local $idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, $iHeight - 20, 880, 20)
42+
Local $idLabelStatus = GUICtrlCreateLabel("Status: Initializing Engine...", 10, $iHeight - 20, 1080, 20)
4343
GUICtrlSetFont(-1, 9, 400, 0, "Segoe UI")
4444

4545
; Initialize WebView2 Manager and register events
4646
Local $oWebV2M = _NetWebView2_CreateManager("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0", "", "--mute-audio")
4747
If @error Then Return SetError(@error, @extended, $oWebV2M)
4848

49+
ConsoleWrite("! " & _NetWebView2_GetVersion($oWebV2M) & @CRLF)
50+
4951
;~ ; Initialize JavaScript Bridge
5052
;~ Local $oJSBridge = _NetWebView2_GetBridge($oWebV2M, "_BridgeMyEventsHandler_")
5153
;~ If @error Then Return SetError(@error, @extended, $oWebV2M)
@@ -60,7 +62,7 @@ Func _Example()
6062
;~ MsgBox($MB_TOPMOST, "TEST #" & @ScriptLineNumber, 0)
6163
Local $s_PDF_FileFullPath
6264

63-
Local $s_PDF_Directory = FileSelectFolder('Choose folder with PDF', '')
65+
Local $s_PDF_Directory = FileSelectFolder('Choose folder with PDF', @ScriptDir)
6466

6567
WinSetOnTop($hMainGUIWindow, "", $WINDOWS_ONTOP)
6668

@@ -74,7 +76,7 @@ Func _Example()
7476

7577
$s_PDF_FileFullPath = $a_Files[$IDX_File]
7678
GUICtrlSetData($idLabelStatus, $sProgress & ' - Navigation started: ' & $s_PDF_FileFullPath)
77-
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, '#view=FitH', 1000)
79+
_NetWebView2_NavigateToPDF($oWebV2M, $s_PDF_FileFullPath, '#view=FitH', 1000, True)
7880
GUICtrlSetData($idLabelStatus, $sProgress & ' - Navigation completed: ' & $s_PDF_FileFullPath)
7981
ConsoleWrite("! === @SLN=" & @ScriptLineNumber & ' ' & $s_PDF_FileFullPath & @CRLF)
8082
If $bSleep_UserReaction Then Sleep(2000) ; simulates user reaction on PDF
@@ -95,8 +97,7 @@ Func _Example()
9597
EndFunc ;==>_Example
9698

9799
Func __NetWebView2_freezer($oWebV2M, ByRef $idPic)
98-
#TODO https://github.com/ioa747/NetWebView2Lib/issues/52#issuecomment-3864784975
99-
Local $hWebView2_Window = HWnd("0x" & Hex($oWebV2M.BrowserWindowHandle, 16))
100+
Local $hWebView2_Window = WinGetHandle($oWebV2M.BrowserWindowHandle)
100101
#Region ; if $idPic is given then it means you already have it and want to delete it - unfreeze - show WebView2 content
101102
If $idPic Then
102103
_SendMessage($hWebView2_Window, $WM_SETREDRAW, True, 0) ; Enables
@@ -112,6 +113,7 @@ Func __NetWebView2_freezer($oWebV2M, ByRef $idPic)
112113
#Region ; add PIC to parent window
113114
Local $hMainGUI_Window = _WinAPI_GetWindow($hWebView2_Window, $GW_HWNDPREV)
114115
Local $aPos = WinGetPos($hWebView2_Window)
116+
;~ _ArrayDisplay($aPos, '$aPos ' & @ScriptLineNumber)
115117
Local $hPrev = GUISwitch($hMainGUI_Window)
116118
$idPic = GUICtrlCreatePic('', 0, 0, $aPos[2], $aPos[3])
117119
Local $hPic = GUICtrlGetHandle($idPic)

examples/005-SaveDemo.au3

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ Func _Example()
7373
ShellExecute($s_MHTML_FileFullPath)
7474
#EndRegion ; MHTML
7575

76+
#Region ; PDF
77+
; get Browser content as PDF Base64 encoded binary data
78+
Local $s_PDF_FileFullPath = @ScriptDir & '\5-SaveDemo_result.pdf'
79+
Local $dBinaryDataToWrite = _NetWebView2_PrintToPdfStream($oWebV2M, True)
80+
81+
; finally save PDF to FILE
82+
Local $hFile = FileOpen($s_PDF_FileFullPath, $FO_OVERWRITE + $FO_BINARY)
83+
FileWrite($hFile, $dBinaryDataToWrite)
84+
FileClose($hFile)
85+
86+
; open PDF file in viewer (viewer which is set as default in Windows)
87+
ShellExecute($s_PDF_FileFullPath)
88+
#EndRegion ; PDF
89+
7690
#Region ; GUI Loop
7791
; Main Loop
7892
While 1

examples/010-NavigationBar.au3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Func _Example()
5757

5858

5959
; navigate to the page
60-
_NetWebView2_Navigate($oWebV2M, "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population", $NETWEBVIEW2_MESSAGE__NAVIGATION_COMPLETED)
60+
_NetWebView2_Navigate($oWebV2M, "https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population", $NETWEBVIEW2_MESSAGE__TITLE_CHANGED)
6161
ConsoleWrite("navigate to the page" & @CRLF)
6262

6363
; Main Loop

examples/014-pdfJS-Static_PDF_Viewer.au3

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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
112121
EndFunc ;==>__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()

examples/_WV2_ExtensionPicker.au3

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Global $__sExtSourcePath, $__sActiveExtensionsBase
2727
; $sUserDataPath - Path to the current WebView2 User Data Folder
2828
; ===============================================================================================================================
2929
Func _WV2_ShowExtensionPicker($iWidth = 500, $iHeight = 650, $hWND = 0, $sExtSourcePath = "", $sUserDataPath = "")
30-
ConsoleWrite("$sUserDataPath=" & $sUserDataPath & @CRLF)
30+
If Not @compiled Then ConsoleWrite("$sUserDataPath=" & $sUserDataPath & @CRLF)
3131

3232
If Not FileExists($sExtSourcePath) Then Return MsgBox(48, "Extension Manager", "Extension Libpary is empty" & _
3333
@CRLF & "or path not exist")
@@ -249,4 +249,4 @@ Func __ExtensionPickerEvents()
249249
$__oWeb.NavigateToString(__ExtensionPickerLoad())
250250

251251
EndSwitch
252-
EndFunc ;==>__ExtensionPickerEvents
252+
EndFunc ;==>__ExtensionPickerEvents

0 commit comments

Comments
 (0)