Jump to content

Autoit <-> Javascript roundtrip


Recommended Posts

How can I do to let return a value from javascript to autoit?

I would like to use some javascript functions, passing values from AutoIt and get result back to autoit,
for example, this little snippet formats a number passed from autoit and show the result in an javascript alert box, but nothing is returned back to autoit.

#include <ie.au3>
Global $oIE = _IECreate("about:blank", 0, 0, 1, 0) ; use an hidden ie instance as a javascript "exposer"
$r = 1000000
$result = $oIE.document.parentwindow.execscript('alert(parseInt( ' & $r & ' ).toLocaleString());')
MsgBox(0, 0, $result)

any suggestions on how to have the result returned back to autoit is welcome
(avoiding temporary storage on phantom hidden fields or such alchemies please)
Thank you

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

Hi Iczer
I had a look in your udf, but have not found there a way to easly call a javascript function and get back the result to AutoIt.
(even the function _ffcmd() from the ff.au3 seems a bit "hard coded" to me)
thanks for the replay

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

I assume with execscript that in the javascript you have to do a return "hello world".

I am not sure what you mean with your alchemy part but this is another way of having your things done

with AutoIT you can enter in any browser in any addressbar a bookmarklet/favlet short piece of javascript

javascript:<script>;void(0);

the void(0) is there that the active page does not get screwed

as you can do anything in those short scripts you can add an inputbox at the bottom of the page of pixelsize 1 by 1.

in the javascript you put your information in that inputbox which you then can read with regular autoit functions.

if you do not like that approach you allways can do an alert function in the addressbar and then read the modal dialog and click away the ok button

so type on any page this in your addressbar (with copy paste you loose javascript: so reenter that manually)

javascript:alert(document.body.innerHTML);void(0);

 

Link to post
Share on other sites

Thanks @junkew for your tips, I will give it a try.
...I still wonder if there is some other way to get results from javascript functions returned directly from the javascript function itself instead of from a "middleman" field (inputbox, alert.. or similar brokers)...

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites
  • 1 year later...

..... I had given up on this concept, but coming again on this, I see that I was very close to the goal,

I've a way to build a bridge between AutoIt and Javascript.

in this way also the many external javascript library could be easly used from AutoIt.

here a basic example on how to use native javascript functions from AutoIt:

#include <IE.au3>
; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD><script>" & @CRLF ; html opening tags
; here, within the HEAD tag, we insert the javascript code
; sence the .execscript method is no longer supported (https://msdn.microsoft.com/en-us/library/ms536420(v=vs.85).aspx)
; we have to use the eval method instead. A reference to the global object is needed to access the eval method. See here:
; http://stackoverflow.com/questions/9642491/getting-a-reference-to-the-global-object-in-an-unknown-environment-in-strict-mod
; http://perfectionkills.com/unnecessarily-comprehensive-look-into-a-rather-insignificant-issue-of-global-objects-creation/#ecmascript_5_strict_mode
$sHTML &= "var global = (1,eval)('this');" & @CRLF ; the 'global' variable get a handle to the global object

$sHTML &= "</script></HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = _IECreate("about:blank",0,0,0) ; an hidden browser (used as javascript engine provider)

_IEDocWriteHTML($oIE, $sHTML) ; inject above html (and library) in browser
_IEAction($oIE, "refresh")

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.global ; $ohJS is a reference to the Global Obj
; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; example of use:
$i = 1000000 ; an unformatted number
; we use a javascript method to format our number
; and we get the result from javascript directly in an AutoIt variable by the javascript eval method ...
Local $result = $ohJS.eval('parseInt( ' & $i & ' ).toLocaleString();')
MsgBox(0,"Debug", $result)

_IEQuit($oIE)

Here, another example on how to use an external javascript library called moment.js.
it's a library similar to the #include <date.au3> but written in javascript.
you have to download the library and save it in the same path of this script before you run it:

#include <IE.au3>
;
; to load the moment.js library, get it from the site: http://momentjs.com/
; and save it to the script path prior to run this script
;
Local $sJScript = FileRead(".\moment.js") ; load library from disk
; you could also get it 'on the fly' from the net using InetRead() like this:
; Local $dJScript = InetRead("http://momentjs.com/downloads/moment.js")
; Local $sJScript = BinaryToString($dJScript)

; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD><script>" & @CRLF ; html opening tags
;
; here, within the HEAD tag, we insert the javascript code
;
; sence the .execscript method is no longer supported (https://msdn.microsoft.com/en-us/library/ms536420(v=vs.85).aspx)
; we have to use the eval method instead. A reference to the global object is needed to access the eval method.
; see here:
; http://stackoverflow.com/questions/9642491/getting-a-reference-to-the-global-object-in-an-unknown-environment-in-strict-mod
; http://perfectionkills.com/unnecessarily-comprehensive-look-into-a-rather-insignificant-issue-of-global-objects-creation/#ecmascript_5_strict_mode
$sHTML &= "var global = (1,eval)('this');" & @CRLF ; the 'global' variable get a handle to the global object

; here we include the moment.js library within the HEAD tag (something like #include in AutoIt)
$sHTML &= $sJScript  & @CRLF

$sHTML &= "</script></HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = _IECreate("about:blank",0,0,0) ; an hidden browser (used as javascript engine provider)

_IEDocWriteHTML($oIE, $sHTML) ; inject above html (and library) in browser
_IEAction($oIE, "refresh")

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.global ; $ohJS is a reference to the Global Obj

; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; example of use:

$i = 1000000 ; an unformatted number

; we use a javascript method to format our number
; and we get the result from javascript directly in an AutoIt variable by the javascript eval method ...
Local $result = $ohJS.eval('parseInt( ' & $i & ' ).toLocaleString();')
MsgBox(0,"Debug", $result)

; here we use a function from the external moment.js library included above...
$result = $ohJS.eval("moment().format('LLLL');")
MsgBox(0, "Debug", $result)

_IEQuit($oIE)

I've tested those 2 draft scripts only on ie9 at the moment, but should work even on ie11 since I use the eval method instead of the now unsupported execScript method.

those 2 examples are just a proof of concept, and I think that can be improved, but in my opinion this "bridge" is an interesting tool....

improvements and suggestions are welcome.
Thank You everybody.

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

If you are only trying to get JavaScript returns with AutoIt I would suggest ScriptControl.

Example:

$oSC = ObjCreate("ScriptControl")
$oSC.Language = "JScript"
$oSC.AddCode("var testvar={a:123,b:456,c:789}"); or could be FileRead like: $oSC.AddCode(FileRead("script.js"))
$testvar = $oSC.Eval("testvar")
MsgBox(0, "", "testvar.a: "&$testvar.a&@CRLF&"testvar.b: "&$testvar.b&@CRLF&"testvar.c: "&$testvar.c)
MsgBox(0, "", "testvar.a+testvar.b+testvar.c = "&$oSC.Eval("testvar.a+testvar.b+testvar.c"))

Additional information here: Using the ScriptControl

Here's a finished solution:

 

Link to post
Share on other sites

Waw! definitely interesting! Thanks alot @genius257  for the valuable information, the useful example (and the very interesting links). appreciated very much!

nonetheless, I also like the 'bridge' across Javascript and AutoIt as built ln my post above.

...What pro and/or cons (if any) on using the ScriptControl vs the explorer object and viceversa....? :think:

Just to not leave that script from post#7 'incomplete', I've to say that it was not working on win7 x64 + ie11, so here is a modified version that worked also on ie11.

Local $dJScript = InetRead("http://momentjs.com/downloads/moment.js") ; get a library from the net
Local $sJScript = BinaryToString($dJScript)
; Local $sJScript = FileRead(".\moment.js") ; or load library from disk

; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD>" & @CRLF
$sHTML &= "<script>" & @CRLF ; Javascripts goes here
; $sHTML &= '"use strict";' & @CRLF ;
$sHTML &= 'var JSglobal = (1,eval)("this");' & @CRLF ; the 'global' variable get a handle to the javascript global object
$sHTML &= $sJScript & @CRLF ; #include <moment.js>
$sHTML &= "</script>" & @CRLF
$sHTML &= "</HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = ObjCreate("Shell.Explorer.2")
GUICreate("", 10, 10, @DesktopWidth + 10, @DesktopHeight + 10) ; place the gui out of screen
GUICtrlCreateObj($oIE, 0, 0, 10, 10) ; this render $oIE usable
GUISetState(@SW_HIDE) ; hide GUI

$oIE.navigate('about:blank')
While Not String($oIE.readyState) = 'complete' ; wait for about:blank
    Sleep(100)
WEnd

$oIE.document.Write($sHTML) ; inject lising directly to the HTML document:
$oIE.document.close() ; close the write stream
$oIE.document.execCommand("Refresh")

; this waits till the document is ready to be used (portion of code from IE.au3)
While Not (String($oIE.readyState) = "complete" Or $oIE.readyState = 4)
    Sleep(100)
WEnd
While Not (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4)
    Sleep(100)
WEnd

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.JSglobal ; $ohJS is a reference to the javascript Global Obj
; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; simple example of use:
Local $i = 1000000 ; an unformatted number
; we use native javascript method to format our number
; and we get the result from javascript directly in an AutoIt variable by the javascript eval method ...
Local $result = $ohJS.eval('parseInt( ' & $i & ' ).toLocaleString();')
MsgBox(0, "Debug", $result)

; here we use a function from the 'external' moment.js library (included above...)
$result = $ohJS.eval("moment().format('LLLL');")
MsgBox(0, "Debug", $result)

 

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites
43 minutes ago, Chimp said:

What pro and/or cons (if any) on using the ScriptControl vs the explorer object and viceversa....? :think:

ScriptControl is missing normal stuff from browser js sutch as native JSON (you can add it with a script). I remember having issues with trying to implement a script for string compression, but a few line changes to scriptcontrol here and there and it worked :) (It was lz-string.js and i believe it was some bitwise functionality).

A big pro with scriptcontrol is the functionallity with something like AutoIt. Using AutoItObject.Au3, you can call AutoIt functions from within Javascript functions. you are also able to add elements from AutoIt in Javascript, however things like AutoIt arrays are Unknown type in Javascript.

But if you are looking for webpage manipulation or such, IE is best.

I would advise to play around with it if you wish :)

Personally i love importing javascript elements into AutoIt, though in some cases the speed of AutoIt is more important than the flexibility of Javascript

 

Edit:

Here is a pdf export of my onenote page with scriptcontrol (seems better than browsing 3-4 pages for the same info)

 

ScriptControl.pdf

Edited by genius257
Link to post
Share on other sites

Javascript Array to AutoIt

... thanks to this JavaScript Object JSON, and to this method specifically JSON.stringify, it's possible (from IE9  and newer) to transfer an array from Javascript to AutoIt. (and eventually  using this other nice Json.au3 udf you can then recreate the same array in AutoIt)

This following simple script performs what said above, but, running the same script more times, it happens that sometime it works well, and sometime it fails on the JSON.stringify method on line 60. (I'm testing on win7 x64 IE9)

Anyone have any idea why this random error happens or what I'm doing wrong?
Thanks for any hint.

#include <AutoItConstants.au3>
; #include <array.au3>
; #include <Json.au3>

; *** create a minimal 'html' page listing for the browser
Local $sHTML = "<HTML><HEAD>" & @CRLF
$sHTML &= "<script>" & @CRLF ; Javascripts goes here

$sHTML &= 'var JSglobal = (1,eval)("this");' & @CRLF ; the 'global' variable get a handle to the javascript global object

; ============================ this Javascript returns an array
$sHTML &= '   function makeArray() {' & @CRLF
$sHTML &= 'var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];' & @CRLF
$sHTML &= 'return myArray;' & @CRLF
$sHTML &= '}' & @CRLF
; =============================================================

$sHTML &= "</script>" & @CRLF
$sHTML &= "</HEAD></HTML>" & @CRLF ; html closing tags
; *** end of html page listing

Local $oIE = ObjCreate("Shell.Explorer.2")
GUICreate("", 10, 10, @DesktopWidth + 10, @DesktopHeight + 10) ; place the gui out of screen
GUICtrlCreateObj($oIE, 0, 0, 10, 10) ; this render $oIE usable
GUISetState(@SW_HIDE) ; hide GUI

$oIE.navigate('about:blank')
While Not String($oIE.readyState) = 'complete' ; wait for about:blank
    Sleep(100)
WEnd

$oIE.document.Write($sHTML) ; inject lising directly to the HTML document:
$oIE.document.close() ; close the write stream
$oIE.document.execCommand("Refresh")

; this waits till the document is ready to be used (portion of code from the _IELoadWait() function in IE.au3)
While Not (String($oIE.readyState) = "complete" Or $oIE.readyState = 4)
    Sleep(100)
WEnd
While Not (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4)
    Sleep(100)
WEnd

; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx
Local $ohJS = $oIE.document.parentwindow.JSglobal ; $ohJS is a reference to the javascript Global Obj
; ---- now the javascript engine can be used in our AutoIt script using the $ohJS reference ----

; This call a javascript function that returns an javascript array
$result1 = $ohJS.eval('makeArray();')

; this obj is (should be) a javascript array (not recognized as is outside the javascript)
_ObjDescription($result1) ; this javascript array have to be 'serialized' in JSON

; Serializes a JavaScript value into 'JavaScript Object Notation' (JSON) text.
; https://msdn.microsoft.com/en-us/library/cc836459(v=vs.85).aspx
$result2 = $ohJS.JSON.stringify($result1) ; this sometime works and sometime fail ...?????? why <--- ??????

MsgBox(0, "Debug JSON string", $result2) ; a json string representing the array

; Local $Data1 = Json_Decode($result2)
; _ArrayDisplay($Data1)

Func _ObjDescription(ByRef $oObj)
    If IsObj($oObj) Then
        $sObjInfos = 'The name of the Object:' & @CRLF & ObjName($oObj, $OBJ_NAME) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'Description string of the Object:' & @CRLF & ObjName($oObj, $OBJ_STRING) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'The ProgID of the Object:' & @CRLF & ObjName($oObj, $OBJ_PROGID) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'The file that is associated with the object in the Registry:' & @CRLF & ObjName($oObj, $OBJ_FILE) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= 'Module name in which the object runs:' & @CRLF & ObjName($oObj, $OBJ_MODULE) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= "CLSID of the object's coclass:" & @CRLF & ObjName($oObj, $OBJ_CLSID) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
        $sObjInfos &= "IID of the object's interface:" & @CRLF & ObjName($oObj, $OBJ_IID) & @CRLF
        $sObjInfos &= '------------------------------------------------------------' & @CRLF
    Else
        $sObjInfos = "Is not an object)"
    EndIf
    MsgBox(0, "Debug: Obj info", $sObjInfos)
EndFunc   ;==>_ObjDescription

 

Edited by Chimp
corrections on the _ObjDescription() function

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

When i try to run your code it always fails.

Also the JSON object returns 'undefined' when i try to extract it with eval like below:

MsgBox(0, "", $ohJS.eval("typeof JSON"))

I hate to sound like a broken record, but why "Shell.Explorer.2" and not "ScriptControl"? Some requirements IE is better suited for in your scenario? :)

 

Edit:

Here's an example:

$oSC = ObjCreate("ScriptControl")
$oSC.Language = "JScript"
$oSC.AddCode(BinaryToString(InetRead("https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js")))
$oSC.Eval("Array.prototype.Item = function(i){return this[i];}"); enable fetching of array item with Array.Item function
$myArray = $oSC.Eval('["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]')
For $i=0 To $myArray.length-1
    ConsoleWrite( $myArray.Item($i) & @CRLF )
Next
MsgBox(0, "", $oSC.Eval("JSON").stringify($myArray))

 

Edited by genius257
Link to post
Share on other sites
10 minutes ago, genius257 said:

When i try to run your code it always fails.

what IE version are you using?

om my system it works 50% of runs (....??)

12 minutes ago, genius257 said:

I hate to sound like a broken record, but why "Shell.Explorer.2" and not "ScriptControl"? Some requirements IE is better suited for in your scenario? :)

II would interact with objects on the embedded IE obj transfer arrays and eventually also interact with canvas... seems that this is not allowed with "ScriptControl", right?

p.s.

you don't sound at all like a broken record :)

 

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites
11 minutes ago, Chimp said:

what IE version are you using?

Version: 11.633.10586.0

11 minutes ago, Chimp said:

II would interact with objects on the embedded IE obj transfer arrays and eventually also interact with canvas... seems that this is not allowed with "ScriptControl", right?

The interaction with a canvas is indeed impractical, if not impossible with "ScriptControl"

11 minutes ago, Chimp said:

you don't sound at all like a broken record :)

Thank you ;)

Link to post
Share on other sites

... both

EDIT:

or better, I think you have first to attch to the IE obj and then using it to 'navigate' into it's DOM, or there is a way to directly attach to objects created within it?

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

Then something like the embedded object?

Opt("GuiOnEventMode", 1)

$oIE = ObjCreate("Shell.Explorer.2")
$hGui = GUICreate("Title", 700, 320)
GUISetOnEvent(-3, "_MyExit", $hGui)
GUISetState()
$hIE = GUICtrlCreateObj($oIE, 10, 10, 680, 300)
$oIE.navigate('about:blank')
$oIE.document.Write('<!DOCTYPE HTML><html><head><style>body{background-color:#010;color:#FFF;}</style></head><body>this is the body</body></html>')
While 1
    Sleep(10)
WEnd

Func _MyExit()
    Exit
EndFunc

 

Link to post
Share on other sites

@genius257 ...sorry for the delay...

... that listing for example could be the 'basic' gui, then, in short, I would 'write' for example this page (a timeline from this site) into the embbedded IE, and finally use "ScriptControl" (or whatever) to interact with the timeline so to use it as a graphic frontend where to display data, while data is managed by AutoIt on an SQLite database for example...
I was trying now to do something like that, but I'm having problems on opening the above page into the embedded IE (the timeline is not shown...)
I will come back to this tomorrow.... 02:58 AM here....

.....thanks for any help.... :)

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Chimp
      This script is an alternative (improvement) to the script published at this link (www.autoitscript.com/forum/topic/186225-interesting-effect-with-transparency). In that post a flash animation was used and therefore you need the flash player to work, while here only javascript is used.
      Here, too, the aim is to show an interesting effect that can be obtained by exploiting transparency, but can also be used as it is just for fun.
      The animation consists of some fish wandering around the screen. You can continue working on the computer by letting them swim.
      The javascript source was not written by me, but I got it from this link: https://github.com/lrusso/Aquarium. I shrunk it down a bit and tweaked it so that fish can swim across the entire screen even with different sized monitors.
      The fish images and html and javascript sources have been bundled into a single "monolithic" AutoIt script, so it's easier to run.
      I hope you find it relaxing.
      Have fun
      fishMonolitic.zip
    • By mdwerne
      Hello,
      Before Broadcom took over Symantec, I was able to use the following code as the base to scrape daily definition downloads from the web. Since the pages were moved over to the Broadcom web servers, I get a page full of what I believe may be JavaScript instead of the fully rendered page that lies behind it. Does anyone have any suggestions as to how I can read the full rendered webpage using AutoIt?
      #include <IE.au3> $oIE_DEFS = _IECreate("https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14", 0, 0, 1, 1) $sString_DEFS = _IEBodyReadHTML($oIE_DEFS) MsgBox(0, "https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14", $sString_DEFS) The above code will show the JavaScript, but if you go to the URL in a browser that has JavaScript enabled, you will see the fully rendered page that I would like to access. I hope my question makes sense, and I would appreciate any suggestions to get this working again.
      All the best,
      -Mike
    • By Chimp
      Making the integration between AutoIt and the BrowserControl easier may facilitate the development of interesting programs. Just think of all those (fantastic) libraries and frameworks available for javascript that could be integrated and exploited in an AutoIt program. For example, to graphically present the results of an AutoIt processing in the GUI, ... and many other possibilities. Providing the basic functions to implement this synergistic interaction is the purpose of this post (if for no other reason, just even to collect some tests done over time here and there, so as not to leave them scattered and easily find them in just one place)
      In this UDF there are only a few functions (or better if called wrappers), but I hope to add more as I go and, even better, to receive suggestions and advice on new features and techniques to improve and expand it.
      _WebBrowser_GUICtrlCreate Create an Internet Explorer 'Browser Control' _WebBrowser_SetHTML Set a new HTML listing in the BrowserControl _WebBrowser_CSS_Inject Creates into the html document a CSS node element with embedded the passed CSS _WebBrowser_JS_Inject Creates into the html document a javascript node element with embedded the passed javascript _WebBrowser_JS_Eval Evaluates a passed string as JavaScript code and executes it _WebBrowser_JS_setTimeout Calls a javascript function or executes a javascript code snippet [option after a specified delay] _WebBrowser_JS_ElementGetRef Retrieves a reference to an element suitable to be used from AutoIt _WebBrowser_ExecuteDotNotation Get a reference to an object's child member or the value of a property, by means of a dotted path _WebBrowser_BasicHTML Returns a basic html page listing (a little enhanced than about:blank) The three examples provided in the attached ziped file are a bit 'improvised' and do not take advantage of all the possibilities offered by the underlying javascript libraries used. They are just three "hello world" scripts to test the ABC of the interaction with the "BrowserControl".
      (ToDo: Interaction with javascript custom events)
      Bug reports, creative criticisms and suggestions (particularly regarding the interaction with javascript custom events) are welcome
      I hope you can have fun with the Browser Control
      BrowserControl.zip
    • By Chimp
      This is a little experiment that makes use of a "Browser Control" embedded in a GUI in order to be able to use AutoIt, HTML, JavaScript and CSS all together.
      This little toy will only work on systems with IE11.
      The purpose is to drag all the names of the scientists & drop on the right ones. (among scientists it has also infiltrated an intruder). I hope you find it entertaining.
      I've posted it here in the hope to have some suggestions on how to improve it all (I mean the interaction between Autoit Javascript html and css). Thanks
      ; this works on systems with IE11 ; ------------------------------- #include <GUIConstantsEx.au3> #include <array.au3> Global $oIE, $oDocument, $ohJS, $sDroping Global $iCorrect = 0, $iGoal = 11 Example() Exit Func Example() Local $aScientists[12][2] = _ [["Schrodinger", "Schrodinger"],["Planck", "Planck"],["Pauli", "Pauli"],["Einstein", "Einstein"], _ ["Chimp", "Chimp"],["Dirac", "Dirac"],["Heisenberg", "Heisenberg"],["Born", "Born"], _ ["De Broglie", "De_Broglie"],["Bohr", "Bohr"],["Sommerfeld", "Sommerfeld"],["", "empty"]] Local $oIE = ObjCreate("Shell.Explorer.2") ; Create a BrowserControl Local $hGUI = GUICreate("", 660, 600, 30, 30) GUICtrlCreateObj($oIE, 0, 0, 660, 600) ; Place BrowserControl on the GUI GUISetState() ;Show GUI $oIE.navigate('about:blank') ; file:///' & @ScriptDir & '\WhoIsWho.html') While Not String($oIE.readyState) = 'complete' ; wait for about:blank Sleep(100) WEnd ; this waits till the document is ready to be used (portion of code from IE.au3) While Not (String($oIE.readyState) = "complete" Or $oIE.readyState = 4) Sleep(100) WEnd While Not (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4) Sleep(100) WEnd $oIE.document.Write(_GetHTML()) ; inject lising directly to the HTML document: $oIE.document.close() ; close the write stream $oIE.document.execCommand("Refresh") While Not String($oIE.readyState) = 'complete' ; wait for readyState after a refresh Sleep(100) WEnd ; https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx ; $ohJS is a reference to the javascript Global Obj ; ------------------------------------------------- $ohJS = $oIE.document.parentwindow.JSglobal $oDocument = $oIE.document Local $oTable1 = $ohJS.table1 Local $oTable2 = $ohJS.table2 _Randomize($aScientists, $oTable1) ; --- Setup events ------------ ; https://msdn.microsoft.com/en-us/library/hh801967(v=vs.85).aspx Local $aoEventObject[2] $aoEventObject[0] = ObjEvent($oTable1, "IEEvent_", "HTMLTableEvents2") $aoEventObject[1] = ObjEvent($oTable2, "IEEvent_", "HTMLTableEvents2") ; ----------------------------- ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch If $iCorrect = $iGoal Then _Goal() _Randomize($aScientists, $oTable1) $iCorrect = 0 EndIf WEnd ; the end For $i = 0 To UBound($aoEventObject) - 1 $aoEventObject[$i] .stop Next $aoEventObject = 0 ; Kill the Event Object $oIE = 0 ; Remove IE from memory (not really necessary). GUIDelete($hGUI) ; Remove GUI EndFunc ;==>Example ; --- event management zone --- ; following functions are fired by events occurred in the browser control Volatile Func IEEvent_onDragstart($oEvent) ; we save the ID of the dragged obj into the $sDroping variable ; for a later use in the drop function $sDroping = $oEvent.srcElement.ID EndFunc ;==>IEEvent_onDragstart Volatile Func IEEvent_onDragOver($oEvent) $ohJS.event.preventDefault() EndFunc ;==>IEEvent_onDragOver Volatile Func IEEvent_onDrop($oEvent) $ohJS.event.preventDefault() If $sDroping <> "" Then If $oDocument.getElementById($sDroping).innerText <> $oEvent.srcElement.innerText Then If $oEvent.srcElement.ClassName = $oDocument.getElementById($sDroping).ClassName Then $oEvent.srcElement.innerText = $oDocument.getElementById($sDroping).innerText $oDocument.getElementById($sDroping).innerText = "" $oDocument.getElementById($sDroping).draggable = False $oDocument.getElementById($sDroping).setAttribute("style") = "background-color: #80ff80;" $iCorrect += 1 Else For $i = 1 To 3 $oDocument.getElementById($sDroping).setAttribute("style") = "background-color: #ff0000;" Sleep(125) $oDocument.getElementById($sDroping).setAttribute("style") = "background-color: #ffffff;" Sleep(125) Next EndIf EndIf $sDroping = "" EndIf EndFunc ;==>IEEvent_onDrop Func _Randomize(ByRef $aScientists, ByRef $oTable) Local $iRows = ($oTable.rows.length) - 1 Local $iCols = ($oTable.rows.item(0).cells.length) - 1 Local $index _ArrayShuffle($aScientists) For $y = 0 To $iRows For $x = 0 To $iCols $index = ($y * ($iCols + 1)) + $x $oTable.rows.item($y).cells.item($x).innerText = $aScientists[$index][0] ; text $oTable.rows.item($y).cells.item($x).className = $aScientists[$index][1] ; class $oTable.rows.item($y).cells.item($x).draggable = $aScientists[$index][0] <> "" Next Next EndFunc ;==>_Randomize Func _Goal() Local $oTable1 = $ohJS.table1 ; names Local $oTable2 = $ohJS.table2 ; photos Local $iRows = ($oTable1.rows.length) Sleep(250) Local $iCols = 6 ; ($oTable1.rows.item(0).cells.length) Local $aIndex[$iRows * $iCols], $sTemp For $i = 0 To UBound($aIndex) - 1 $aIndex[$i] = $i ; + 1 Next _ArrayShuffle($aIndex) For $i = 0 To UBound($aIndex) - 1 $oTable2.rows.item(Int($aIndex[$i] / $iCols)).cells.item(Mod($aIndex[$i], $iCols)).innerText = "" $oTemp0 = $oTable2.rows $oTemp1 = $oTemp0.item(Int($aIndex[$i] / $iCols)).cells $oTemp2 = $oTemp1.item(Mod($aIndex[$i], $iCols)).getAttribute("style") $oTable2.rows.item(Int($aIndex[$i] / $iCols)).cells.item(Mod($aIndex[$i], $iCols)).setAttribute("style") = "background-color: " & _rndColor() Sleep(100); MsgBox(0,"Debug",$sTemp) $oTable2.rows.item(Int($aIndex[$i] / $iCols)).cells.item(Mod($aIndex[$i], $iCols)).setAttribute("style") = $oTemp2 Next For $x = 1 To 2 For $i = 0 To UBound($aIndex) - 1 $oTable1.rows.item(Int($aIndex[$i] / $iCols)).cells.item(Mod($aIndex[$i], $iCols)).setAttribute("style") = "background-color: " & _rndColor() Sleep(100) $oTable1.rows.item(Int($aIndex[$i] / $iCols)).cells.item(Mod($aIndex[$i], $iCols)).setAttribute("style") = "background-color: #ffffff;" Next Next EndFunc ;==>_Goal Func _rndColor() Return String("#" & Hex(Random(0, 255, 1), 2) & Hex(Random(0, 255, 1), 2) & Hex(Random(0, 255, 1), 2) & ";") EndFunc ;==>_rndColor Func _GetHTML() Local $sHTML = _ "<!DOCTYPE HTML>" & @CRLF & _ "<html>" & @CRLF & _ "<head>" & @CRLF & _ "<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"" />" & @CRLF & _ " <script type=""text/javascript"">" & @CRLF & _ " var JSglobal = (1,eval)(""this"");" & @CRLF & _ " </script>" & @CRLF & _ "</head>" & @CRLF & _ "<body>" & @CRLF & _ "<h2>Who is who?</h2>" & @CRLF & _ "<p>Drag&Drop names on the right scientist</p>" & @CRLF & _ "<img src=""https://i.imgur.com/STWql7w.jpg""" & @CRLF & _ "height=""394"" width=""640""" & @CRLF & _ "style=""position: absolute; left: 10px; top: 100px;"">" & @CRLF & _ "" & @CRLF & _ "<style>" & @CRLF & _ ".target td {width: 100px; height: 160px; text-align: center; color: white; font-weight: bold; vertical-align: bottom; border: 0px solid grey;}" & @CRLF & _ ".source td {width: 100px; height: 30px; text-align: center; border: 1px solid red;}" & @CRLF & _ "</style>" & @CRLF & _ "" & @CRLF & _ "<table class=""target"" style=""position: absolute; left: 25px; top: 100px;"" id=""table2"">" & @CRLF & _ " <tr><td class=""Schrodinger""></td><td class=""Planck""></td><td class=""Pauli""></td><td class=""Einstein""></td><td class=""Chimp""></td><td class=""Dirac""></td></tr>" & @CRLF & _ " <tr><td class=""Heisenberg""></td><td class=""Born""></td><td class=""De_Broglie""></td><td class=""Bohr""></td><td class=""Sommerfeld""></td><td class=""empty""></td></tr>" & @CRLF & _ "</table>" & @CRLF & _ "" & @CRLF & _ "<table class=""source"" style=""position: absolute; left: 10px; top: 504px;"" id=""table1"">" & @CRLF & _ " <tr><td ID=""td1""></td><td ID=""td2""></td><td ID=""td3""></td><td ID=""td4"" ></td><td ID=""td5"" ></td><td ID=""td6"" ></td></tr>" & @CRLF & _ " <tr><td ID=""td7""></td><td ID=""td8""></td><td ID=""td9""></td><td ID=""td10""></td><td ID=""td11""></td><td ID=""td12""></td></tr>" & @CRLF & _ "</table>" & @CRLF & _ "</body>" & @CRLF & _ "</html>" Return $sHTML EndFunc ;==>_GetHTML  
    • By Phil1991
      Hi,
      I'm working on an interface and I have a problem with zoom management. In desktop mode it works and in android cell mode it seems to work fine too. However, on a Windows tablet, it seems to have a problem ... Basically what I'm trying to do is display a modal in bootstrap 4. But when on the tablet I zoom, my modal s really big and out of my screen. On the android mobile, I remove the zoom before displaying the modal but on the Windows tablet I am unable to reproduce the same behavior.
       
      My toggle function to reset zoom before display modal  ( Yeah it maybe sketches but I'm not find other solutions)
      function resetZooming() { if (zoomOut) { $('meta[name=viewport]').attr('content', 'width=device-width, initial-scale=1.0, maximum-scale=1'); setTimeout(resetZooming, 1000); zoomOut = false; }else{ $('meta[name=viewport]').attr('content', 'width=device-width, initial-scale=1.0'); zoomOut = true; } } And I call this function under my event
      $("#modalCreationForm").modal("show"); resetZooming();  
      If you have any ideas
      Thank you
×
×
  • Create New...