If a JS function source code is assigned to a variable, how to call this JS function in a script?
For example, the JS (x, y) function is JS code, how to call it?
Code: Select all
JS := "JS(x, y){ JS code here }"
Code: Select all
JS := "JS(x, y){ JS code here }"
Code: Select all
jsObj := CreateScriptObj(), %jsObj%("alert('Привет, мир!')")
CreateScriptObj() {
static doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
return ObjBindMethod(doc.parentWindow, "eval")
}
Code: Select all
JS = function JS(x, y) { alert("x: " + x + "\ny: " + y) }
HTML := "<script>" JS "</script>"
Gui, Add, ActiveX, w200 h200 vWB, about:<!DOCTYPE html>
WB.Document.Open(), WB.Document.Write(HTML), WB.Document.Close()
HTMLWindow := WB.Document.parentWindow ; <<< we use this to run js code
F1:: HTMLWindow.JS(10, 20) ; hotkey to call JS function
Code: Select all
a1 := 1
a2 := 2
a3 := "function s(a,b){return a+b};"
Code: Select all
a1 := "1"
a2 := "2"
a3 := "function s(a,b){return a+b};"
JS := GetJScripObject(a3)
msgbox % JS.("s(a1, a2)")
return
GetJScripObject(a3)
{
FileAppend,
(
<component>
<public><method name='eval'/></public>
<script>%a3%</script>
</component>
), tmpFile
JS := ObjBindMethod(ComObjGet("script:tmpFile"), "eval")
Return JS
}
Code: Select all
a1 := 1
a2 := 2
a3 := "function s(a,b){return a+b};"
HTML := "<script>" a3 "</script>"
Gui, Add, ActiveX, vWB, about:<!DOCTYPE html>
WB.Document.Open(), WB.Document.Write(HTML), WB.Document.Close()
MsgBox, % WB.Document.parentWindow.s(a1, a2)
Code: Select all
JS := GetJScripObject()
code := "function s(a,b){return a+b}; var a1 = 1, a2 = 2; s(a1, a2)"
MsgBox, % JS.(code)
JS := CreateScriptObj()
MsgBox, % JS.(code)
GetJScripObject() { ; Here we create the temp file to get the custom COM server using Windows Script Components (WSC) technology.
VarSetCapacity(tmpFile, ((MAX_PATH := 260) - 14) << !!A_IsUnicode, 0)
DllCall("GetTempFileName", Str, A_Temp, Str, "AHK", UInt, 0, Str, tmpFile)
FileAppend,
(
<component>
<public><method name='eval'/></public>
<script language='JScript'></script>
</component>
), % tmpFile
JS := ObjBindMethod( ComObjGet("script:" . tmpFile), "eval" ) ; ComObjGet("script:" . tmpFile) is the way to invoke com-object without registration in the system
FileDelete, % tmpFile
Return JS
}
CreateScriptObj() {
static doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
return ObjBindMethod(doc.parentWindow, "eval")
}
OK, I see, thanks. I will change my approach. Currently I am tempted by "ease-of-use" maybe caused by habit. (Never used JS outside of canvas)teadrinker wrote: ↑22 Apr 2019, 06:35wolf_II, you don't need to create the window. You get WebBrowser object which is more heavy than htmlfile object.
Users browsing this forum: Google [Bot], ReyAHK and 274 guests