Skip to content

Commit

Permalink
added NavComplete_call
Browse files Browse the repository at this point in the history
  • Loading branch information
joedf committed Aug 14, 2016
1 parent c97bcb0 commit 5ec67cc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 76 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ A Webapp.ahk project must have a `webapp.json` configuration file.

```JSON
{
"name": "My App",
"width": 640,
"height": 480,
"protocol": "app",
"protocol_call": "app_call",
"html_url": "index.html"
"name": "My App",
"width": 640,
"height": 480,
"protocol": "app",
"protocol_call": "app_call",
"html_url": "index.html",
"NavComplete_call": "app_page"
}
```

- `Width` & `Height`: Webapp.ahk allows window resizing just like any other application. The `width` and `height` option here is to set the application's starting size.
- `Width` & `Height`: Webapp.ahk allows window resizing just like any other application. The `width` and `height` options here are to set the application's starting size.
- The `name` option is the text that will be displayed in the title bar. It can be changed on run-time with `setAppName()`.
- The `html_url` option is the starting HTML file for the App to launch with. It defaults to `index.html` if none is specified.
- The `protocol` option is for filter URL prefixes such as whether to run a function when a link like `app://msgbox/hello` is clicked.
- The `protocol_call` option is the name of the function in your AutoHotkey that will run in these cases.
- The `protocol` option is for filtering URL prefixes such as whether to run a function when a link like `app://msgbox/hello` is clicked.
- The `protocol_call` option is the name of the function in your AutoHotkey that will run in these cases. It defaults to `app_call()` if none is specified.
- The `NavComplete_call` option is the name of the function in your AutoHotkey that will run when a page is finished loading. Its first argument is the new page's URL. It defaults to `app_page()` if none is specified.

Note: For example, if `protocol` is set to `myapp` and a `myapp://test/1234` link is clicked, the set `protocol_call` funtion will be called and will receive `test/1234` as its first argument. If `protocol` is set to `*`, the set `protocol_call` funtion will run for **ANY** link clicked and it will receive `myapp://test/1234` (the whole URL) as its first argument. This is not recommended for most cases, as links with `href="#"` will also trigger the function (usually unwanted behaviour).
Note: For example, if `protocol` is set to `myapp` and a `myapp://test/1234` link is clicked, the set `protocol_call` function will be called and will receive `test/1234` as its first argument. If `protocol` is set to `*`, the set `protocol_call` function will run for **ANY** link clicked and it will receive `myapp://test/1234` (the whole URL) as its first argument. This is not recommended for most cases, as links with `href="#"` will also trigger the function (usually unwanted behaviour).

## Special thanks
A special thanks to Coco, VxE, Lexikos, Phatricko and the AutoHotkey community.
15 changes: 15 additions & 0 deletions src/Example.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ app_call(args) {
SoundPlay, %A_WinDir%\Media\ding.wav
}

; function to run when page is loaded
app_page(NewURL) {
wb := getDOM()

if InStr(NewURL,"index.html") {
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() {
Expand All @@ -31,6 +43,9 @@ Hello() {
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
Expand Down
68 changes: 9 additions & 59 deletions src/Lib/Webapp.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ try {
if !IsFunc(__Webapp_protocol_call)
throw "Function Name '" . __Webapp_protocol_call . "' does not exist."
__Webapp_protocol_call:=Func(__Webapp_protocol_call)
__Webapp_NavComplete_call := __Webapp_DefaultVar(j.NavComplete_call,"app_page")
if !IsFunc(__Webapp_NavComplete_call)
throw "Function Name '" . __Webapp_NavComplete_call . "' does not exist."
__Webapp_NavComplete_call:=Func(__Webapp_NavComplete_call)
__Webapp_html_url := __Webapp_DefaultVar(j.html_url,"index.html")
if !FileExist(__Webapp_html_url)
throw "File '" . __Webapp_html_url . "' does not exist."
Expand All @@ -31,7 +35,7 @@ Gui __Webapp_:Margin, 0, 0
Gui __Webapp_:+LastFound +Resize
OnMessage(0x100, "gui_KeyDown", 2)
Gui __Webapp_:Add, ActiveX, v__Webapp_wb w%__Webapp_Width% h%__Webapp_height%, Shell.Explorer
;SetWBClientSite() ; not working
SetWBClientSite()
__Webapp_wb.silent := true ;Surpress JS Error boxes
;__Webapp_wb.Navigate("about:<!DOCTYPE html><meta http-equiv='X-UA-Compatible' content='IE=edge'>")
__Webapp_wb.Navigate("file://" . __Webapp_html_url)
Expand Down Expand Up @@ -74,11 +78,11 @@ class __Webapp_wb_events
;for more events and other, see http://msdn.microsoft.com/en-us/library/aa752085

;blocked all navigation, we want our own stuff happening
/*NavigateComplete2 not needed.
NavigateComplete2(wb) {
wb.Stop()
NavigateComplete2(wb, NewURL) {
; wb.Stop() ;not needed in this one.
global __Webapp_NavComplete_call
__Webapp_NavComplete_call.call(NewURL)
}
*/
DownloadComplete(wb, NewURL) {
wb.Stop()
}
Expand Down Expand Up @@ -170,60 +174,6 @@ getFileFullPath(f) {
}
}

_InitUI() {
local w
SetWBClientSite()
;gosub DefineUI
wb.Silent := true
wb.Navigate("about:blank")
while wb.ReadyState != 4 {
Sleep 10
if (A_TickCount-initTime > 2000)
throw 1
}
wb.Document.open()
wb.Document.write(html)
wb.Document.close()
w := wb.Document.parentWindow
if !w || !w.initOptions
throw 1
w.AHK := Func("JS_AHK")
if (!CurrentType && A_ScriptDir != DefaultPath)
CurrentName := "" ; Avoid showing the Reinstall option since we don't know which version it was.
w.initOptions(CurrentName, CurrentVersion, CurrentType
, ProductVersion, DefaultPath, DefaultStartMenu
, DefaultType, A_Is64bitOS = 1)
if (A_ScriptDir = DefaultPath) {
w.installdir.disabled := true
w.installdir_browse.disabled := true
w.installcompiler.disabled := !DefaultCompiler
w.installcompilernote.style.display := "block"
w.ci_nav_install.innerText := "apply"
w.install_button.innerText := "Apply"
w.extract.style.display := "None"
w.opt1.disabled := true
w.opt1.firstChild.innerText := "Checking for updates..."
}
w.installcompiler.checked := DefaultCompiler
w.enabledragdrop.checked := DefaultDragDrop
w.separatebuttons.checked := DefaultIsHostApp
; w.defaulttoutf8.checked := DefaultToUTF8
if !A_Is64bitOS
w.it_x64.style.display := "None"
if A_OSVersion in WIN_2000,WIN_2003,WIN_XP ; i.e. not WIN_7, WIN_8 or a future OS.
w.separatebuttons.parentNode.style.display := "none"
;w.switchPage("start")
w.document.body.focus()
; Scale UI by screen DPI. My testing showed that Vista with IE7 or IE9
; did not scale by default, but Win8.1 with IE10 did. The scaling being
; done by the control itself = deviceDPI / logicalDPI.
logicalDPI := w.screen.logicalXDPI, deviceDPI := w.screen.deviceXDPI
if (A_ScreenDPI != 96)
w.document.body.style.zoom := A_ScreenDPI/96 * (logicalDPI/deviceDPI)
;if (A_ScriptDir = DefaultPath)
; CheckForUpdates()
}

/* Complex workaround to override "Active scripting" setting
* and ensure scripts can run within the WebBrowser control.
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ <h1>Webapp.ahk</h1>
</p>
<h2>Special thanks</h2>
<p>A special thanks to Coco, VxE, Lexikos, Phatricko and the AutoHotkey community.</p>
<p><small id="ahk_info"><small></p>
</body>
</html>
3 changes: 2 additions & 1 deletion src/page1.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<style>
html{margin:0;padding:0;overflow:auto;}
body{font-family:sans-serif;background-color:#dde4ec;}
Expand All @@ -21,7 +22,7 @@
<a href="NOPE://msgbox/hello">Click me for nothing</a>&nbsp;-&nbsp;
<a href="app://soundplay/ding">Click me for a ding sound!</a><br>
<a href="#" onclick="AHK('Hello')">Link using onlick instead</a>&nbsp;-&nbsp;
<a href="index.html">Goto index</a>
<a href="index.html">Goto index</a>&nbsp;-&nbsp;
<a href="page2.html">Goto Page 2</a>
<span style="display:block;height:14px;">&nbsp;</span>
<input type="button" id="MyButton1" value="Show Content in AHK MsgBox" onclick="AHK('MyButton1')">
Expand Down
13 changes: 7 additions & 6 deletions src/webapp.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "My App",
"width": 640,
"height": 480,
"protocol": "app",
"protocol_call": "app_call",
"html_url": "index.html"
"name": "My App",
"width": 655,
"height": 480,
"protocol": "app",
"protocol_call": "app_call",
"html_url": "index.html",
"NavComplete_call": "app_page"
}

0 comments on commit 5ec67cc

Please sign in to comment.