|
| 1 | +#WIP - this Example is imported from 1.5.0 UDF - and is in "WORK IN PROGRESS" state |
| 2 | + |
| 3 | +#AutoIt3Wrapper_UseX64=n |
| 4 | +#AutoIt3Wrapper_Run_AU3Check=Y |
| 5 | +#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y |
| 6 | +#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 |
| 7 | +#Au3Stripper_Ignore_Funcs=__NetWebView2_WebEvents_*,__NetWebView2_JSEvents_* |
| 8 | + |
| 9 | +; 014-pdfJS-Static_PDF_Viewer.au3 |
| 10 | +; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 11 | +; ⚠️ to make this work, download pdfJS library from https://mozilla.github.io/pdf.js/ |
| 12 | +; for example: |
| 13 | +; https://github.com/mozilla/pdf.js/releases/download/v5.4.530/pdfjs-5.4.530-dist.zip |
| 14 | +; and unzip to: @ScriptDir & "\JS_Lib\pdfjs\" |
| 15 | +; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | + |
| 17 | +#include <GUIConstantsEx.au3> |
| 18 | +#include <WindowsConstants.au3> |
| 19 | +#include "..\NetWebView2Lib.au3" |
| 20 | + |
| 21 | +; Global objects |
| 22 | +Global $oBridge |
| 23 | +Global $hGUI |
| 24 | + |
| 25 | +_Example() |
| 26 | + |
| 27 | +Func _Example() |
| 28 | + Local $oMyError = ObjEvent("AutoIt.Error", __NetWebView2_COMErrFunc) |
| 29 | + #forceref $oMyError |
| 30 | + |
| 31 | + #Region ; GUI CREATION |
| 32 | + |
| 33 | + ; Create the GUI |
| 34 | + $hGUI = GUICreate("WebView2 .NET Manager", 800, 1000) |
| 35 | + |
| 36 | + ; Get the WebView2 Manager object and register events |
| 37 | + Local $oWeb = _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", _ |
| 38 | + "", "--allow-file-access-from-files") ; 👈 |
| 39 | + |
| 40 | + ; initialize browser - put it on the GUI |
| 41 | + Local $sProfileDirectory = @ScriptDir & "\NetWebView2Lib-UserDataFolder" |
| 42 | + _NetWebView2_Initialize($oWeb, $hGUI, $sProfileDirectory, 0, 0, 0, 0, True, False, False, 0.7) |
| 43 | + |
| 44 | + ; Get the bridge object and register events |
| 45 | + _NetWebView2_GetBridge($oWeb, "__MyEVENTS_Bridge_") |
| 46 | + |
| 47 | + ; show the GUI after browser was fully initialized |
| 48 | + GUISetState(@SW_SHOW) |
| 49 | + |
| 50 | + #EndRegion ; GUI CREATION |
| 51 | + |
| 52 | + ; navigate to the page |
| 53 | + __SetupStaticPDF($oWeb, @ScriptDir & "\invoice-plugin-sample.pdf", True, True) |
| 54 | + |
| 55 | + _NetWebView2_ExecuteScript($oWeb, "extractPDFText();", $NETWEBVIEW2_EXECUTEJS_MODE0_FIREANDFORGET) |
| 56 | + |
| 57 | + ; Main Loop |
| 58 | + While 1 |
| 59 | + Switch GUIGetMsg() |
| 60 | + Case $GUI_EVENT_CLOSE |
| 61 | + ExitLoop |
| 62 | + EndSwitch |
| 63 | + WEnd |
| 64 | + |
| 65 | + GUIDelete($hGUI) |
| 66 | + |
| 67 | + _NetWebView2_CleanUp($oWeb, $oBridge) |
| 68 | +EndFunc ;==>_Example |
| 69 | + |
| 70 | +; Handles custom messages from JavaScript (window.chrome.webview.postMessage) |
| 71 | +Func __MyEVENTS_Bridge_OnMessageReceived($sMsg) |
| 72 | + ConsoleWrite(">>> [__EVENTS_Bridge]: " & (StringLen($sMsg) > 150 ? StringLeft($sMsg, 150) & "..." : $sMsg) & @CRLF) |
| 73 | + Local $sFirstChar = StringLeft($sMsg, 1) |
| 74 | + |
| 75 | + If $sFirstChar = "{" Or $sFirstChar = "[" Then ; 1. JSON Messaging |
| 76 | + ConsoleWrite("+> : Processing JSON Messaging..." & @CRLF) |
| 77 | + Local $oJson = ObjCreate("NetJson.Parser") |
| 78 | + If Not IsObj($oJson) Then Return ConsoleWrite("!> Error: Failed to create NetJson object." & @CRLF) |
| 79 | + |
| 80 | + $oJson.Parse($sMsg) |
| 81 | + Local $sJobType = $oJson.GetTokenValue("type") |
| 82 | + |
| 83 | + Switch $sJobType |
| 84 | + Case "COM_TEST" |
| 85 | + ConsoleWrite("- COM_TEST Confirmed: " & $oJson.GetTokenValue("status") & @CRLF) |
| 86 | + EndSwitch |
| 87 | + |
| 88 | + Else ; 2. Legacy / Native Pipe-Delimited Messaging |
| 89 | + ConsoleWrite("+> : Legacy / Native Pipe-Delimited Messaging..." & @CRLF) |
| 90 | + Local $sCommand, $sData, $iSplitPos |
| 91 | + $iSplitPos = StringInStr($sMsg, "|") - 1 |
| 92 | + |
| 93 | + If $iSplitPos < 0 Then |
| 94 | + $sCommand = StringStripWS($sMsg, 3) |
| 95 | + $sData = "" |
| 96 | + Else |
| 97 | + $sCommand = StringStripWS(StringLeft($sMsg, $iSplitPos), 3) |
| 98 | + $sData = StringTrimLeft($sMsg, $iSplitPos + 1) |
| 99 | + EndIf |
| 100 | + |
| 101 | + Switch $sCommand |
| 102 | + Case "COM_TEST" |
| 103 | + ConsoleWrite("- Status: Legacy COM_TEST: " & $sData & @CRLF) |
| 104 | + |
| 105 | + Case "PDF_TEXT" |
| 106 | + ConsoleWrite("- PDF_TEXT: " & @CRLF & $sData & @CRLF) |
| 107 | + |
| 108 | + Case "ERROR" |
| 109 | + ConsoleWrite("! Status: " & $sData & @CRLF) |
| 110 | + EndSwitch |
| 111 | + EndIf |
| 112 | +EndFunc ;==>__MyEVENTS_Bridge_OnMessageReceived |
| 113 | + |
| 114 | +Func __SetupStaticPDF(ByRef $oWeb, $s_PDF_Path, $bBlockLinks = False, $bBlockSelection = False) |
| 115 | + ; 🏆 https://mozilla.github.io/pdf.js/ |
| 116 | + |
| 117 | + Local $sBlockLinksJS = "" |
| 118 | + Local $sBlockLinksCSS = "" |
| 119 | + Local $sSelectionCSS = "" |
| 120 | + |
| 121 | + ; 1. Configuration for BlockLinks |
| 122 | + If $bBlockLinks Then |
| 123 | + $sBlockLinksJS = _ |
| 124 | + "window.addEventListener('click', function(e) {" & _ |
| 125 | + " if (e.target.tagName === 'A' || e.target.closest('a')) {" & _ |
| 126 | + " e.stopImmediatePropagation();" & _ |
| 127 | + " e.preventDefault();" & _ |
| 128 | + " }" & _ |
| 129 | + "}, { capture: true });" |
| 130 | + $sBlockLinksCSS = " .annotationLayer { pointer-events: none !important; } " |
| 131 | + EndIf |
| 132 | + |
| 133 | + ; 2. Configuration for Text Selection |
| 134 | + If $bBlockSelection Then |
| 135 | + $sSelectionCSS = " .textLayer, body { -webkit-user-select: none !important; user-select: none !important; cursor: default !important; } " |
| 136 | + EndIf |
| 137 | + |
| 138 | + ; 3. Final Script Construction |
| 139 | + Local $sCleanupJS = _ |
| 140 | + "/* 1. Block Zoom (Wheel & Keys) */ " & _ |
| 141 | + "window.addEventListener('wheel', function(e) {" & _ |
| 142 | + " if (e.ctrlKey) {" & _ |
| 143 | + " e.stopImmediatePropagation();" & _ |
| 144 | + " e.preventDefault();" & _ |
| 145 | + " }" & _ |
| 146 | + "}, { passive: false, capture: true });" & _ |
| 147 | + "window.addEventListener('keydown', function(e) {" & _ |
| 148 | + " if (e.ctrlKey && ['+', '-', '=', '0'].includes(e.key)) {" & _ |
| 149 | + " e.stopImmediatePropagation();" & _ |
| 150 | + " e.preventDefault();" & _ |
| 151 | + " }" & _ |
| 152 | + "}, { capture: true });" & _ |
| 153 | + $sBlockLinksJS & _ |
| 154 | + "/* 2. PDF Text Extraction Function with Auto-Wait */ " & _ |
| 155 | + "window.extractPDFText = async function() {" & _ |
| 156 | + " const runExtraction = async () => {" & _ |
| 157 | + " try {" & _ |
| 158 | + " if (typeof PDFViewerApplication !== 'undefined' && PDFViewerApplication.pdfDocument) {" & _ |
| 159 | + " const pdf = PDFViewerApplication.pdfDocument;" & _ |
| 160 | + " let text = '';" & _ |
| 161 | + " for (let i = 1; i <= pdf.numPages; i++) {" & _ |
| 162 | + " const page = await pdf.getPage(i);" & _ |
| 163 | + " const content = await page.getTextContent();" & _ |
| 164 | + " text += content.items.map(item => item.str).join(' ') + '\n';" & _ |
| 165 | + " }" & _ |
| 166 | + " window.chrome.webview.postMessage('PDF_TEXT|' + text);" & _ |
| 167 | + " } else {" & _ |
| 168 | + " setTimeout(runExtraction, 500);" & _ |
| 169 | + " }" & _ |
| 170 | + " } catch (e) {" & _ |
| 171 | + " window.chrome.webview.postMessage('ERROR|' + e.message);" & _ |
| 172 | + " }" & _ |
| 173 | + " };" & _ |
| 174 | + " runExtraction();" & _ |
| 175 | + "};" & _ |
| 176 | + "/* 3. Style Injection */ " & _ |
| 177 | + "window.addEventListener('DOMContentLoaded', () => {" & _ |
| 178 | + " const style = document.createElement('style');" & _ |
| 179 | + " style.innerHTML = '#toolbarContainer, #sidebarContainer { display: none !important; } ' + " & _ |
| 180 | + " '#viewerContainer { top: 0 !important; bottom: 0 !important; overflow: hidden !important; } ' + " & _ |
| 181 | + " '" & $sBlockLinksCSS & "' + " & _ |
| 182 | + " '" & $sSelectionCSS & "' + " & _ |
| 183 | + " ' ::-webkit-scrollbar { display: none !important; }';" & _ |
| 184 | + " document.head.appendChild(style);" & _ |
| 185 | + "});" |
| 186 | + |
| 187 | + $oWeb.AddInitializationScript($sCleanupJS) |
| 188 | + |
| 189 | + ; Fix slashes in Path for URL |
| 190 | + Local $s_PDF_URL = StringReplace($s_PDF_Path, "\", "/") |
| 191 | + $s_PDF_URL = StringReplace($s_PDF_URL, ' ', '%20') |
| 192 | + Local $s_PDF_JS_URL = StringReplace(@ScriptDir & "\JS_Lib\pdfjs\web\viewer.html" & "?file=", "\", "/") |
| 193 | + Local $s_Viewer_URL = "file:///" & $s_PDF_JS_URL & $s_PDF_URL |
| 194 | + ConsoleWrite("- $s_Viewer_URL= " & $s_Viewer_URL & @CRLF) |
| 195 | + |
| 196 | + $oWeb.Navigate($s_Viewer_URL) |
| 197 | + |
| 198 | + ; $oWeb.IsZoomControlEnabled = False ; <--- It doesn't work in PDF. 👈 |
| 199 | + $oWeb.DisableBrowserFeatures() |
| 200 | + $oWeb.LockWebView() |
| 201 | +EndFunc ;==>__SetupStaticPDF |
0 commit comments