Ever notice how you can go browsing through the AHK forums and go off on a completely unexpected tangent? Maybe it's just me but I doubt it seriously.
Now that I've got
Chrome.ahk in my head (it will leave soon I'm sure), I thought I'd share this code that sets a custom download folder, creates a blob with the text of a webpage element in it, and then downloads that blob to a named file on my system. I'm using
@teadrinker's tdChrome.ahk (see post above), which as an interesting side effect lets Chrome preserve that notification (bottom left in the Chrome window) about the downloaded file.
Code: Select all
#Include tdChrome.ahk
SetTitleMatchMode, 2
;FileCreateDir, ChromeProfile
SetBatchLines, -1
;****************
; Fix these paths for your system
; or the world will explode
; without notice
;****************
dlPath := "C:\ResearchDownloads\Testing"
If (FileExist("C:\ResearchDownloads\Testing\abc.txt"))
FileDelete, C:\ResearchDownloads\Testing\abc.txt
webpage := "https://www.autohotkey.com"
ChromeInst := new Chrome("ChromeProfile")
Sleep, 3000
PageInstance := ChromeInst.GetPage()
PageInstance.WaitForLoad()
WinMaximize, ahk_exe chrome.exe
PageInstance.Call("Page.navigate", {"url": webpage})
;****************
; Make Chrome download to a specific directory
; @gregster mentions this at https://www.autohotkey.com/boards/viewtopic.php?f=76&t=77637&hilit=page.setdownloadbehavior
; I didn't see that till after I tried this, my syntax is marginally different (quotes around behavior and downloadPath)
; Works both ways.
;****************
PageInstance.Call("Page.setDownloadBehavior", {"behavior" : "allow", "downloadPath": dlPath})
PageInstance.WaitForLoad()
;****************
; This JS will grab the innerHTML of title at autohotkey.com, set the mime_type,
; create a blob of mime_type (i.e. text), create a hidden anchor, set the href
; of the anchor to the blob location,
; grab the href of that blob, stringify it,
; alert with the stringified href, download the blob as 'abc.txt' to the
; custom download directory in 'dlPath'.
;****************
js =
(
var myData = document.getElementById("MainTitle").innerHTML
var mime_type = "text/plain";
var blob = new Blob([myData], {type: mime_type});
let hidden = document.createElement('a');
hidden.href = window.URL.createObjectURL(blob);
var stringy = hidden.href.toString();
alert(stringy);
hidden.download = 'abc.txt';
hidden.click();
hidden.remove();
)
PageInstance.Evaluate(js)
;~ A search for 'window.URL.createObjectURL(blob)'
;~ may be useful to you.
Sleep, 10000
PageInstance.Call("Browser.close")
ChromeInst.Disconnect()
PageInstance.Disconnect()
; Overkill, no doubt. Works.
PageInstance := ""
ChromeInst := ""
ChromeInstance.Kill()
WinKill, chrome.exe
; Kill conhost.exe so it doesn't hang around.
;
DetectHiddenWindows, On
; From GeekDude's tips and tricks: https://www.autohotkey.com/boards/viewtopic.php?f=7&t=7190
Run, cmd,, Hide, PID
WinWait, ahk_pid %PID%
DllCall("AttachConsole", "UInt", PID)
; Run another process that would normally
; make a command prompt pop up, such as
; RunWait, cmd /c dir > PingOutput.txt
Runwait, taskkill /im conhost.exe /f
; Thanks to @flyingDman post from 2010,
; https://autohotkey.com/board/topic/49732-kill-process/
; Close the hidden command prompt process
Process, Close, %PID%
ExitApp
Regards,
burque505