【浏览器操控示例】示例通过创建无头模式的浏览器,来做网页截图
以下示例需要Chrome.ahk库支持才能工作
; By 空
#NoEnv
SetBatchLines, -1
#Include <Chrome>
; --- Create a new headless Chrome instance ---
ChromeInst := new Chrome("User_Data", "https://baidu.com", "--headless")
; --- Connect to the page ---
if !(PageInst := ChromeInst.GetPage())
{
MsgBox, Could not retrieve page!
ChromeInst.Kill()
}
else
{
PageInst.WaitForLoad()
; --- Export a PDF of the page ---
; Get the PDF in Base64
Base64PDF := PageInst.Call("Page.printToPDF").data
; 保存PNG截图的方法,对应文件名也应该改成.png
; Base64PNG := 标签.Call("Page.captureScreenshot").data
; Convert to a normal binary PDF
Size := Base64_Decode(BinaryPDF, Base64PDF)
; Write the binary PDF to a file
FileName := "Exported_" A_TickCount ".pdf"
FileOpen(FileName, "w").RawWrite(BinaryPDF, Size)
; Open the file
Run, %FileName%
; --- Close the Chrome instance ---
try
PageInst.Call("Browser.close") ; Fails when running headless
catch
ChromeInst.Kill()
PageInst.Disconnect()
}
ExitApp
return
Base64_Encode(ByRef Out, ByRef In, InLen)
{
DllCall("Crypt32.dll\CryptBinaryToString", "Ptr", &In
, "UInt", InLen, "UInt", 0x40000001, "Ptr", 0, "UInt*", OutLen)
VarSetCapacity(Out, OutLen * (1+A_IsUnicode))
DllCall("Crypt32.dll\CryptBinaryToString", "Ptr", &In
, "UInt", InLen, "UInt", 0x40000001, "Str", Out, "UInt*", OutLen)
return OutLen
}
Base64_Decode(ByRef Out, ByRef In)
{
DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &In, "UInt", StrLen(In)
, "UInt", 0x1, "Ptr", 0, "UInt*", OutLen, "Ptr", 0, "Ptr", 0)
VarSetCapacity(Out, OutLen)
DllCall("Crypt32.dll\CryptStringToBinary", "Ptr", &In, "UInt", StrLen(In)
, "UInt", 0x1, "Str", Out, "UInt*", OutLen, "Ptr", 0, "Ptr", 0)
return OutLen
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。

评论(0)