AutoIt: Simple “Live reload”

So I've been recently looking for something to help with micro-debugging (mostly verifying output) of my JavaScript+HTML applications via automatically reloading test page on source change. I've found different solutions. Some rely on fetching file information. Some allow you to use a browser extension. Some only work on Mac. Some only work in single browser (when they do at all). But, well, what if I just want to test things on local filesystem in one or other browser without doing any extra juggling? Why aren't there any live reload solutions for this? Is it that hard? Well that's the point,

no it is not.

I mean, really. The whole process is as much as checking if file changed, and kindly asking browser to reload a tab if it did. Second part is slightly harder, but, fortunately, there is WinAPI with it's SendMessage support.
To keep things simple, I've just went and wrote the following micro-code in AutoIt:

$time1 = ""
While 1
	$time0 = $time1
	$time1 = fileGetTime("C:/Temp/NewProject/bin/NewProject.js", 0, 1)
	if ($time1 <> "") and ($time0 <> $time1) then
		consoleWrite("Update: " & $time1 & @CRLF)
		$p = winGetHandle("[ACTIVE]") ; get current active window
		controlSend("New Project - Google Chrome", _ ; window title
			"", "", _ ; component
			"^r")
		winActivate($p) ; switch back to current active window
	endif
	sleep(500)
wend

How to use it:

  • Open your desired JS+HTML application in a (preferably separate) browser window
  • Input window title text into code (you can guess where it goes by a comment saying "window title")
  • Input "changing" files location into code (if you are working on game, it's probably the main .js file)
  • Run the script (F5 if you are using the customized SciTE that comes with AutoIt)
  • Now, whenever the file is modified, script will refresh the browser tab, ensuring that it is of current version.
  • Once done with it, terminate the script (either via SciTE, or by adding a exit hotkey to code)

Obviously this wouldn't be a "full-blown" solution, but that's a start.
I may also publish a larger example of this later.

Related posts:

2 thoughts on “AutoIt: Simple “Live reload”

Leave a Reply

Your email address will not be published. Required fields are marked *