Jump to content

Value of JS variable in IE


Recommended Posts

On 10/11/2016 at 4:47 PM, vagabond719 said:

Chimp,

Do you know of a way to bind to an open IE window after creating the ObjCreate("Shell.Explorer.2")?

Hi @vagabond719, sorry, i don't know how,
If your page is already opened in the "Internet explorer" browser I don't know how could be "bind" to a "browser control" (Shell.Explorer.2) at the same time. I think those are 2 distinct worlds. Did you try to open your web page directly into the Shell.Explorer.2 obj? Maybe you could try to open your page directly into a "Shell.Explorer.2", then pull JS variable values into Autoit by using the $ohJS reference. Anyway, perhaps the rendering engine of the "Browser Control" will not behave exactly as the "internet Explorer" engine and this could result in "unwanted"  results.... You could try it and then see what happens..
Here is a "minimalistic" browser that relies on the "Browser Control Shell.Explorer.2" just to do some tests  if you want. Try to open your page in this "browser" (enter the link in the upper input box and hit enter). See lines 55 to 66 to see how to get variables content or fire function ....say what happens..

#include <ie.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
; create a "browser" with Shell.Explorer.2 as rendering engine
Local $hGUIMain = GUICreate("Internet Shell.Explorer.2", @DesktopWidth / 2, @DesktopHeight / 2, -1, -1, $WS_SIZEBOX + $WS_SYSMENU + $WS_MAXIMIZEBOX + $WS_MINIMIZEBOX)
Local $hGetURL = GUICtrlCreateInput("", 0, 0, @DesktopWidth / 2, 20)
GUISetState() ;Show GUI
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
Global $oIE = ObjCreate("Shell.Explorer.2")
$hIE = GUICtrlCreateObj($oIE, 2, 22, @DesktopWidth / 2 - 6, @DesktopHeight / 2 - 48) ; <- embedd $oIE in the AutoIt GUI
GUICtrlSetResizing($hIE, 102)
; --------------------------------------------------------------------------
; Setup the "Enter" key management (navigate to typed url on enter keypress)
Global $getEnter = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, "gotEnter") ; to call the function gotEnter() when Dummy control receives an "Enter"
Global $a_AccelKeys[1][2] = [["{ENTER}", $getEnter]] ; send the "Enter" keypresses to the Dummy control
GUISetAccelerators($a_AccelKeys)
; $oIE.navigate('www.google.it') ; starting page
$oIE.navigate('www.google.com') ; starting page

Do
    ConsoleWrite('.')
    Sleep(250)
Until (String($oIE.readyState) = "complete" Or $oIE.readyState = 4)
ConsoleWrite(@CRLF)
Do
    ConsoleWrite('.')
    Sleep(250)
Until (String($oIE.document.readyState) = "complete" Or $oIE.document.readyState = 4)
ConsoleWrite(@CRLF)
; --------------------------------------------------------------------------
; get reference to the <html> tag
Local $oTag = _IETagNameGetCollection($oIE, "head", 0)

; insert code in page
; Local $sJSCode_0 = '<a id="mylink"; href=' & "'javascript:var JSglobal = (1,eval)(" & '"this");  alert("Debug: javascript executed"' & ");  void(0);'>"
 Local $sJSCode_0 = '<a id="mylink"; href=' & "'javascript:var JSglobal = (1,eval)(" & '"this")' & ";  void(0);'>"
_IEDocInsertHTML($oTag, $sJSCode_0, "afterbegin")
Local $oLink
Sleep(1000)
Do
; get a reference to proper <a> elements
    Sleep(250)
    $oLink = _IEGetObjById($oIE, "mylink")
Until IsObj($oLink)

; insert javascript via click on <a>
; (this is a workaround instead of using ExecScript method)
$oLink.click()

; --------------------------------------------------------------------------
; this is to be used for testing purpose
Global $ohJS = $oIE.document.parentwindow.JSglobal ; $ohJS is a reference to the javascript Global Obj
;
; to get values from variable use something like this:
; $Result = $ohJS.MyJSVariable
;
; to call a function use something like this:
; Local $result = $ohJS.eval('parseInt( 10000 ).toLocaleString();')


; this is a test to see if the Javascript Global Object has been referenced
; Local $result = $ohJS.eval('parseInt( 10000 ).toLocaleString();')
; MsgBox(0, "Debug", $result)
; --------------------------------------------------------------------------
; Loop until the user exits.
While 1
    Sleep(250)
WEnd

Func gotEnter() ; Here we manage the enter key
    If _GuiCtrlGetFocus($hGUIMain) = $hGetURL Then ; was cursor in control $hGetURL?
        $oIE.navigate(ControlGetText('', '', $hGetURL)); if yes then call my enter function
    Else ; prosecute ahead the "Enter" key to the proper control
        GUISetAccelerators("")
        ControlSend($hGUIMain, "", _GuiCtrlGetFocus($hGUIMain), "{ENTER}")
        GUISetAccelerators($a_AccelKeys)
    EndIf
EndFunc   ;==>gotEnter

; Retrieves the internal handle of the control with the focus
Func _GuiCtrlGetFocus($hGui = "")
    Local $InputID = ControlGetHandle($hGui, "", ControlGetFocus($hGui))
    Return _ControlGetGuiID($InputID)
EndFunc   ;==>_GuiCtrlGetFocus

Func _ControlGetGuiID($hCtrl) ; Transforms from Handle to ID
    Local $Result = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hCtrl)
    If @error = 0 Then Return $Result[0]
    Return SetError(1, 0, '')
EndFunc   ;==>_ControlGetGuiID

Func _Exit()
    GUIDelete($hGUIMain)
    Exit
EndFunc   ;==>_Exit

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

@Chimp its a weird problem to not be able to get the globalobject 

Some directions but no solution

https://msdn.microsoft.com/en-us/library/52f50e9t(v=vs.94).aspx

http://stackoverflow.com/questions/26023915/accessing-javascript-global-object-from-ie-com

windows.self maybe window.self.eval

document.script returns an idispatch maybe use in objcreateinterface command to get scriptcontrol no clue what type script further is

parentwindow.eval(...)   

parentwindow.execscript(...)
 

$ie.Document.parentWindow.execScript($code, "javascript");

http://stackoverflow.com/questions/11924016/decompiled-htmldocuments-invokescript-not-working interesting but far above my knowledge

Link to comment
Share on other sites

A nice oneline to call short scripts thru the addressbar

$oIE.Navigate2("javascript:console.log(123);void(0);")

So this should set a global object reference variable

;~$quote=""""
;~ $js="javascript:var JSglobal = (1,eval)(" & $quote & "this" &  $quote & ");void(0);"
$js="javascript:var JSglobal = typeof self === 'object' && self.self === self && self;void(0);"
consolewrite($js & @CRLF)
$oIE.Navigate2($js)

@Chimp

and this adds a nice helloworld function

$js="javascript:document.hello=function(){alert('Really hello world')};void(0);"
consolewrite($js & @CRLF)
$oIE.Navigate2($js)

and this to proof that function exists

$js="javascript:document.body.hello();void(0);"
consolewrite($js & @CRLF)
$oIE.Navigate2($js)

and then when called from AutoIT (at least with W10, IE11)

consolewrite("Hocus pocus is coming " & @CRLF)
$oie.document.hello();

If this logic would work its a small step to get global object returned

$js="javascript:alert(JSglobal.ScriptEngine());void(0);"
$oIE.Navigate2($js)

and then returning it

$js="javascript:document.getGlobalJS=function(){return JSglobal;};void(0);"
$oIE.Navigate2($js)
sleep(2000)

Global $ohJS = $oie.document.getGlobalJS()

if isobj($ohJS) Then
   consolewrite("Seems to have retrieved a global object " & @CRLF)
EndIf

but then failing when doing more in AutoIT

consolewrite("Seems to have retrieved a global object " & $ohJS.ScriptEngine() & @CRLF)

 

edit: but it solves at least the issue on not having eval/execscript as you can create your own function returning var values. Alternative could be to set with javascript a document.body.setattribute('whatever', <JS variable>) and do a document.body.getattribute('whatever') 

Edited by junkew
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...