Skip to content

Commit 73ccfcd

Browse files
authored
Refactor SaveDemo.au3 for improved functionality
1 parent 02d5874 commit 73ccfcd

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

examples/005-SaveDemo.au3

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,26 @@ Func _Example()
6262
#EndRegion ; HTML
6363

6464
#Region ; MHTML
65-
; This trick is because Responsive Design (CSS) stored inside MHTML, and loading="lazy"
65+
; # NOTE # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66+
; This sequence ensures that responsive design (CSS) and lazy-loaded assets
67+
; We use Zoom 0.5 to trigger Ultra-Wide assets (>1408px).
68+
; Note: This might cause missing assets if the MHTML is viewed in a very small window later,
69+
; as the browser only bundles assets active during the current (Wide) session.
70+
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71+
6672
$oWebV2M.ZoomFactor = 0.5
67-
Sleep(500)
73+
_ForceAntiLazy($oWebV2M)
6874
Local $s_MHTML_content = _NetWebView2_ExportPageData($oWebV2M, 1, "")
69-
$oWebV2M.ZoomFactor = 1
75+
$oWebV2M.ZoomFactor = 1.0
76+
77+
; Save to file
78+
Local $s_MHTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.mhtml'
79+
If FileExists($s_MHTML_FileFullPath) Then FileDelete($s_MHTML_FileFullPath)
80+
FileWrite($s_MHTML_FileFullPath, $s_MHTML_content)
7081

71-
Local $s_MHTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.mhtml'
72-
If FileExists($s_MHTML_FileFullPath) Then FileDelete($s_MHTML_FileFullPath)
73-
FileWrite($s_MHTML_FileFullPath, $s_MHTML_content)
74-
ShellExecute($s_MHTML_FileFullPath)
75-
#EndRegion ; MHTML
82+
; Open the result in the default browser (Edge)
83+
ShellExecute($s_MHTML_FileFullPath)
84+
#EndRegion ; MHTML
7685

7786
#Region ; GUI Loop
7887
; Main Loop
@@ -90,3 +99,29 @@ Func _Example()
9099

91100
EndFunc ;==>_Example
92101

102+
; #FUNCTION# ====================================================================================================================
103+
; Name ..........: _ForceAntiLazy
104+
; Description ...: Forces the browser to load all lazy-loaded images and assets before exporting data.
105+
; Syntax ........: _ForceAntiLazy($oWebV2M)
106+
; Parameters ....: $oWebV2M - The WebView2 Manager object.
107+
; Return values .: Returns the result of the JavaScript execution.
108+
; Author ........: ioa747
109+
; ===============================================================================================================================
110+
Func _ForceAntiLazy($oWebV2M)
111+
Local $sJS = ""
112+
$sJS &= "(function() {" & @CRLF
113+
$sJS &= " document.querySelectorAll('img').forEach(img => {" & @CRLF
114+
$sJS &= " img.setAttribute('loading', 'eager');" & @CRLF
115+
$sJS &= " if (img.dataset.srcset) img.srcset = img.dataset.srcset;" & @CRLF
116+
$sJS &= " if (img.dataset.src) img.src = img.dataset.src;" & @CRLF
117+
$sJS &= " });" & @CRLF
118+
$sJS &= " window.scrollTo(0, document.body.scrollHeight);" & @CRLF
119+
$sJS &= " setTimeout(() => { window.scrollTo(0, 0); }, 150);" & @CRLF
120+
$sJS &= " return 'AntiLazy: Multi-layout triggered.';" & @CRLF
121+
$sJS &= "})();"
122+
123+
Local $sResult = _NetWebView2_ExecuteScript($oWebV2M, $sJS, 2)
124+
Sleep(500) ; Slightly more sleep to handle the 1408px transition
125+
Return $sResult
126+
EndFunc
127+

0 commit comments

Comments
 (0)