Building a web app GUI with chrome.ahk example

Post your working scripts, libraries and tools
Stavencross
Posts: 60
Joined: 24 May 2016, 16:42

Building a web app GUI with chrome.ahk example

Post by Stavencross » 20 May 2018, 09:44

After talking a lot with GeekDude who made the Chrome.ahk library, I wanted to post a super simple example to show how to use chrome.ahk to build a HTML GUI.

This example is extremely simple, and shows how to execute functions based on a javascript callback function. All gratitude should go to Geekdude, as all I did was simplify his existing callback example.

Code: Select all

/*
	Create an HTML page with a button. in the button, add: onclick="console.log('AHK:spitMessage,Hello World!')"
	At this point, you can see that message get logged in your console. When this happens, the callback function, which is monitoring the console will trigger.
	It will send the message to your AHK, allowing you to execute functions from within the HTML page (via javascript!). You can also add event listeners instead of onclick events.
	
*/	
	#Include Chrome.ahk
	
	FileCreateDir, ChromeProfile
	ChromeInst := new Chrome("ChromeProfile", "--app=http://localhost/index.php") ;We'll run chrome in "app" mode, which strips out all the UI buttons. We can run a reg instance by removing --app=
	PageInst := ChromeInst.GetPageByURL("http://localhost/index.php",,, "Callback") ;connect to the page we just launched.
	PageInst.Call("Console.enable") ;make sure the console is enabled!!!
	
	Callback(Event)
	{
		if (Event.Method == "Console.messageAdded"  && InStr(Event.params.message.text,"AHK:") >= 1) ;we only want to run our function if the console message is prefixed with AHK:, however, you can change this if you want.	
		{
			Text := Event.params.message.text		
			Split := StrSplit(Text, ",","AHK:") ;Turn our console message into an array, but we'll drop the AHK: from it.	
			fnName := Split[1] ;Grab the name of our function
			Split.RemoveAt(1) ;Drop function name from the array, all we need now are the params.
			%fnName%(Split*) ; Call that function, passing in the rest of the array as parameters
		}
	}
	
	spitMessage(msg) {
		msgBox, %msg% ;We'll show a messagebox with the first param you sent thru your console.
	}
	

Return to “Scripts and Functions”