Help with webapp.ahk launch function on load

Get help with using AutoHotkey and its commands and hotkeys
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Help with webapp.ahk launch function on load

16 Oct 2017, 09:58

Good morning all! I'm trying to build a super nice GUI with joedf's HTML/CSS gui wrapper WebApp.ahk (https://autohotkey.com/boards/viewtopic.php?t=4588) and I'm trying to execute a function on page load to allow my AHK functions to modify the DOM .

Basically I just need to be able to notice page load, wait until page is loaded, and then add a value to a textbox. I noticed in his example he does this with function app_page(NewURL). he pulls the url of the page you're navigating to, and runs a function there disp_info() which modifies the text to show your ahk version.

However, when I try to do this to modify my settings.html page, I noticed it actually runs twice. Once when the link is clicked, and then on page load.
using button clicking works flawlessly but I'm just looking for a way to interact with the page on load. I've also tried using the html/js "onload=" event. However, that only works if I run a javascript alert first!

Here is his example script with comments

Code: Select all

#SingleInstance off
#NoTrayIcon
SendMode Input

#Include Lib\Webapp.ahk
__Webapp_AppStart:
;<< Header End >>


;Get our HTML DOM object
iWebCtrl := getDOM()

;Change App name on run-time
setAppName("My Webapp.ahk Application")

; cut auto run main thread.
Return

; Webapp.ahk-only Sensitive hotkeys
#IfWinActive, ahk_group __Webapp_windows
!Enter::
	;!toggle
	setFullscreen(!__Webapp_FullScreen)
Return
#IfWinActive

; Our custom protocol's url event handler
app_call(args) {
	MsgBox %args%
	if InStr(args,"msgbox/hello")
		MsgBox Hello world!
	else if InStr(args,"soundplay/ding")
		SoundPlay, %A_WinDir%\Media\ding.wav
}


; function to run when page is loaded			;if put put a message box here, you'll see that it runs twice! how do I get it to execute only AFTER the actual page changes?
app_page(NewURL) {
	wb := getDOM()
	
	if InStr(NewURL,"index.html") {
		disp_info()
	}
}

disp_info() {
	wb := getDOM()
	Sleep, 10
	x := wb.document.getElementById("ahk_info")
	x.innerHTML := "<i>Webapp.ahk is currently running on " . GetAHK_EnvInfo() . ".</i>"
}

; Functions to be called from the html/js source
Hello() {
	MsgBox Hello from JS_AHK :)
}
Run(t) {
	Run, %t%
}
GetAHK_EnvInfo(){
	return "AutoHotkey v" . A_AhkVersion . " " . (A_IsUnicode?"Unicode":"ANSI") . " " . (A_PtrSize*8) . "-bit"
}
Multiply(a,b) {
	;MsgBox % a " * " b " = " a*b
	return a * b
}
MyButton1() {
	wb := getDOM()
	MsgBox % wb.Document.getElementById("MyTextBox").Value
}
MyButton2() {
	wb := getDOM()
	FormatTime, TimeString, %A_Now%, dddd MMMM d, yyyy HH:mm:ss
    Random, x, %min%, %max%
	data := "AHK Version " A_AhkVersion " - " (A_IsUnicode ? "Unicode" : "Ansi") " " (A_PtrSize == 4 ? "32" : "64") "bit`nCurrent time: " TimeString "`nRandom number: " x
	wb.Document.getElementById("MyTextBox").value := data
}

Here's my snippet

Code: Select all

; function to run when page is loaded
app_page(NewURL) {
	wb := getDOM()
	
	if InStr(NewURL,"settings.html") {                                ;This is actually going to run twice. Once when you click the link to launch "settings.html" and again just before the page loads.
		disp_info()
	}
}

disp_info() {
	wb :=getDOM()
	sleep,1000                                                               ;This sleep actually takes effect BEFORE page load.
	x := document.getElementById("settingOne")
	x.value := "testing!"
}

And finally, some HTML, also showing how body onload doesn't work

Code: Select all

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv='X-UA-Compatible' content='IE=edge'>
		<link rel="stylesheet" href="assets/css/CSS.css">
	</head>
		<body onload="executeFunction()">
		<span>Your name in SalesForce:</span> <input type ="text"  id="settingOne"> </input>
		<script>
		AHK('executeAHKFunction');             //This will not execute unless you throw up a javascript alert! first.
		<script>
		</body>
</html> 
	
	executeAHKFunction() {
	msgbox, Hello from ahk!
	}
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Re: Help with webapp.ahk launch function on load

17 Oct 2017, 05:47

Can anyone please help on this?
BoBo
Posts: 2520
Joined: 13 May 2014, 17:15

Re: Help with webapp.ahk launch function on load

17 Oct 2017, 07:05

Stavencross wrote:Can anyone please help on this?

Code: Select all

If answer !InTime
   Send.Pm(joedf)
:)
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Re: Help with webapp.ahk launch function on load

17 Oct 2017, 12:02

BoBo wrote:
Stavencross wrote:Can anyone please help on this?

Code: Select all

If answer !InTime
   Send.Pm(joedf)
:)
Bobo,

I did PM Joedf, but have not recieved a reply, I thought I would also take a shot in the forum.
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Re: Help with webapp.ahk launch function on load

17 Oct 2017, 17:50

I finally figured it out! webapp.ahk is already pulling a comobjconnect so we just need to hook into it with our function using a global. then, when we want to execute a function on load, we'll capture the "link" and execute our function instead

AHK function that goes in the "example.ahk"

Code: Select all

hijackText() {
	global __Webapp_wb	
	__Webapp_wb.navigate(A_ScriptDir . "/page2.html") ;we're hijacking the linking process here, and using COM to to navigate to the new page
	sleep,1000 ; can use a while wb busy loop here.
	__Webapp_wb.document.getElementById("title").TextContent :="Hellloo" ;Execute the change of text!
}
Here's the HTML so you can see what I mean by hijacking the link. This is page1.html in the example!

Code: Select all

<div id="title" onclick="AHK('hijackText')">Lorem Ipsum</div> <!-- instead of the href link, force our AHK function to execute -->
		<div id="corner">Welcome!</div>
		<p>The standard Lorem Ipsum passage, used since the 1500s</p>
Here's page two, we're changing "Title" to "Hellloo" with our AHK function above

Code: Select all

<div id="title">Title</div>
		<p><b>Note:</b>This is an example page.</p>
		<p>
User avatar
joedf
Posts: 7447
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: Help with webapp.ahk launch function on load

11 Dec 2019, 22:00

Wow, so sorry! Just saw this now because of this:
https://github.com/joedf/Webapp.ahk/issues/6

I must have somehow missed the PM... :think:

For future readers!
I've made a fix. I think it should work now :+1:
https://github.com/joedf/Webapp.ahk/commit/5602e59700969a1c3aabdc4b81cd3f87f8ccba86
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, joshuajclark, Odlanir, Sashkon, tom0925 and 122 guests