|
| 1 | + |
| 2 | +# Logic for MHTML Export |
| 3 | + |
| 4 | +>[!INFO] |
| 5 | +> This extra logic is here to handle modern sites with lazy-loading and responsive breakpoints. |
| 6 | +> For simpler sites, it is not strictly required but highly recommended for consistency. |
| 7 | +
|
| 8 | +### The "Viewport Scaling" Logic for MHTML Export: |
| 9 | + |
| 10 | +To ensure a complete MHTML capture on modern responsive sites (like Microsoft.com), we must trigger the **High-Resolution breakpoints** (typically >1408px). |
| 11 | + |
| 12 | +1. **The Goal:** Force the browser to load assets intended for Ultra-Wide layouts, even if the user's physical window is smaller. |
| 13 | + |
| 14 | +2. **The Method:** We use `$oWebV2M.ZoomFactor = 0.5` during the pre-export phase. This expands the logical viewport (e.g., a 1000px GUI becomes a 2000px virtual canvas), triggering the CSS media queries to load all high-res assets. |
| 15 | + |
| 16 | +3. **The Scaling Factor:** We must note that these calculations assume **100% Windows Scaling**. If a user has 150% scaling, the "physical" width needed is even higher. Using Zoom 0.5 is the most reliable "universal" way to bypass local DPI settings and ensure the MHTML remains visual even when viewed on a 1920px monitor. |
| 17 | + |
| 18 | +4. **Scope:** This is specifically required for sites using **Lazy-Loaded assets** and **Responsive Breakpoints** that hide/show content based on width. |
| 19 | + |
| 20 | + |
| 21 | +This table explains how the **Zoom Factor** affects the **Virtual Viewport** |
| 22 | +assuming a typical GUI width of **1000px**: |
| 23 | + |
| 24 | +### Viewport Scaling Logic (Base GUI: 1000px) |
| 25 | + |
| 26 | +|Zoom Factor|Virtual Viewport Width|Breakpoint Targeted|Result for MHTML| |
| 27 | +|---|---|---|---| |
| 28 | +|**1.0**|1000px|Mobile / Tablet|Missing High-Res Assets| |
| 29 | +|**0.8**|1250px|Standard Desktop|Missing Ultra-Wide Assets| |
| 30 | +|**0.75**|1333px|Standard Desktop|Still below the 1408px limit| |
| 31 | +|**0.7**|1428px|**Ultra-Wide (>1408px)**|**Triggers 1408px assets**| |
| 32 | +|**0.5**|**2000px**|**4K / Ultra-Wide**|**Full Asset Capture (Safe Zone)**| |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +### Important Technical Constraint: Breakpoint Lock |
| 37 | + |
| 38 | +When using `ZoomFactor = 0.5` to ensure we capture the **Ultra-Wide / 4K assets** (>1408px), we are effectively "locking" the MHTML package to that specific responsive state. |
| 39 | + |
| 40 | +- **The Trade-off:** While this ensures the page looks perfect on high-resolution monitors (1920px and above), the browser might not include the assets required for the **Standard Desktop** layout. |
| 41 | + |
| 42 | +- **The Result:** If a user opens the resulting MHTML and resizes the window below the 1408px threshold, some images or icons might disappear because they weren't part of the "Wide" capture session. |
| 43 | + |
| 44 | +- **Conclusion:** We choose **0.5** as the default for the Demo because it covers the most common use case (Modern Desktop viewing), but users should be aware that MHTML is not truly "fluid" across all breakpoints unless all states are triggered before export. |
| 45 | + |
| 46 | + |
| 47 | +--- |
| 48 | +### Example |
| 49 | +005-SaveDemo.au3: |
| 50 | + |
| 51 | +```autoit |
| 52 | + #Region ; MHTML |
| 53 | + ; # NOTE # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 54 | + ; This sequence ensures that responsive design (CSS) and lazy-loaded assets |
| 55 | + ; We use Zoom 0.5 to trigger Ultra-Wide assets (>1408px). |
| 56 | + ; Note: This might cause missing assets if the MHTML is viewed in a very small window later, |
| 57 | + ; as the browser only bundles assets active during the current (Wide) session. |
| 58 | + ; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 59 | + |
| 60 | + $oWebV2M.ZoomFactor = 0.5 |
| 61 | + _ForceAntiLazy($oWebV2M) |
| 62 | + Local $s_MHTML_content = _NetWebView2_ExportPageData($oWebV2M, 1, "") |
| 63 | + $oWebV2M.ZoomFactor = 1.0 |
| 64 | +
|
| 65 | + ; Save to file |
| 66 | + Local $s_MHTML_FileFullPath = @ScriptDir & '\5-SaveDemo_result.mhtml' |
| 67 | + If FileExists($s_MHTML_FileFullPath) Then FileDelete($s_MHTML_FileFullPath) |
| 68 | + FileWrite($s_MHTML_FileFullPath, $s_MHTML_content) |
| 69 | +
|
| 70 | + ; Open the result in the default browser (Edge) |
| 71 | + ShellExecute($s_MHTML_FileFullPath) |
| 72 | + #EndRegion ; MHTML |
| 73 | +... |
| 74 | +... |
| 75 | +Func _ForceAntiLazy($oWebV2M) |
| 76 | + Local $sJS = "" |
| 77 | + $sJS &= "(function() {" & @CRLF |
| 78 | + $sJS &= " document.querySelectorAll('img').forEach(img => {" & @CRLF |
| 79 | + $sJS &= " img.setAttribute('loading', 'eager');" & @CRLF |
| 80 | + $sJS &= " if (img.dataset.srcset) img.srcset = img.dataset.srcset;" & @CRLF |
| 81 | + $sJS &= " if (img.dataset.src) img.src = img.dataset.src;" & @CRLF |
| 82 | + $sJS &= " });" & @CRLF |
| 83 | + $sJS &= " window.scrollTo(0, document.body.scrollHeight);" & @CRLF |
| 84 | + $sJS &= " setTimeout(() => { window.scrollTo(0, 0); }, 150);" & @CRLF |
| 85 | + $sJS &= " return 'AntiLazy: Multi-layout triggered.';" & @CRLF |
| 86 | + $sJS &= "})();" |
| 87 | +
|
| 88 | + Local $sResult = _NetWebView2_ExecuteScript($oWebV2M, $sJS, 2) |
| 89 | + Sleep(500) ; Slightly more sleep to handle the 1408px transition |
| 90 | + Return $sResult |
| 91 | +EndFunc |
| 92 | +``` |
| 93 | + |
| 94 | +--- |
0 commit comments