How to call JS code in a script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

How to call JS code in a script?

21 Apr 2019, 12:50

Hello,

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            }"
Thanks.
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to call JS code in a script?

21 Apr 2019, 14:39

This is too complicated, I don't understand at all. Is there a simple line of code to do this?
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to call JS code in a script?

21 Apr 2019, 14:56

You can follow teadrinker (Using Google Translate to automate text translation) and use a function that returns a bound func obj binding the eval method to a dummy window object that contains a html document:

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")
}
my scripts
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: How to call JS code in a script?

21 Apr 2019, 15:01

here is an example script, which uses ActiveX:

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
Last edited by wolf_II on 21 Apr 2019, 18:10, edited 1 time in total.
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to call JS code in a script?

22 Apr 2019, 01:44

Thank you very much. If do not display a interface, how to achieve it?

Such as this example,
Find the value of
a := s(a1, a2)


Code: Select all

a1 := 1
a2 := 2
a3 := "function s(a,b){return a+b};"
Last edited by afe on 22 Apr 2019, 03:10, edited 1 time in total.
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to call JS code in a script?

22 Apr 2019, 03:03

Where is the following code wrong?

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
}
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: How to call JS code in a script?

22 Apr 2019, 06:21

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)
Last edited by wolf_II on 22 Apr 2019, 06:35, edited 1 time in total.
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to call JS code in a script?  Topic is solved

22 Apr 2019, 06:35

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")
}
wolf_II, you don't need to create the window. You get WebBrowser object which is more heavy than htmlfile object.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: How to call JS code in a script?

22 Apr 2019, 06:42

teadrinker wrote:
22 Apr 2019, 06:35
wolf_II, you don't need to create the window. You get WebBrowser object which is more heavy than htmlfile object.
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)
@afe: please ignore my posts.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ReyAHK and 274 guests