AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Javascript on a webpage - possible to evaluate result ?
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
hotzenpl0tz



Joined: 22 Oct 2007
Posts: 11

PostPosted: Tue Nov 27, 2007 5:31 pm    Post subject: Javascript on a webpage - possible to evaluate result ? Reply with quote

Hi,
I am working on some automation tasks on webpages. I have gotten very far with Sean's excellent COM.ahk (thanks for that !) and have now gotten to a point, where it would be cool if I could use javascript to get a value from a page and save it to a variable. Is that possible ?

Here an example:

On http://www.mredkj.com/tutorials/tutorial005.html, I can use the following to get an alert box with the value of the first selection item:

Code:

#Include COM.ahk

COM_CoInitialize()
pweb := COM_Createobject("InternetExplorer.Application")
COM_Invoke(pweb, "Visible=", "True")
COM_Invoke(pweb, "Navigate", "http://www.mredkj.com/tutorials/tutorial005.html")
sleep 3000
COM_Invoke(pweb, "Navigate", "javascript:alert(parent.document.forms[1].elements[2].options[0].value)")

return


Now my question, is it possible to store that value somehow ?
What I need for ma automation tast, is an if - statement, that looks something like this (pseudo code):

if (selectbox has only one value)
dostuff
else
dosomething different.

Is something like that possible ?

Thanks!
Back to top
View user's profile Send private message
wOxxOm



Joined: 09 Feb 2006
Posts: 326

PostPosted: Tue Nov 27, 2007 11:40 pm    Post subject: Reply with quote

just a guess - javascript can 'documentwrite' anything probably, so make your js put the variable value into some invible tag, then read and parse current page contents via some COM Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
BoBo¨
Guest





PostPosted: Wed Nov 28, 2007 12:29 am    Post subject: Reply with quote

Code:
javascript:var o=document.documentElement.innerHTML;window.clipboardData.setData("Text",o);void(0);
Code:
javascript:var o=document.documentElement.innerText;window.clipboardData.setData("Text",o);void(0);
or
Code:
javascript:void(window.clipboardData.setData('Text', document.element.innerText));


Check/parse the clipboards content afterwards. Hope that helps Smile
Back to top
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Wed Nov 28, 2007 12:37 am    Post subject: Reply with quote

May append this at the end of the script. Wow, looks scary. Wink
Code:
Value := COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(COM_Invoke(pweb,"Document"),"forms"),"Item",1),"elements"),"Item",2),"options"),"Item",0),"value")
Back to top
View user's profile Send private message
wOxxOm



Joined: 09 Feb 2006
Posts: 326

PostPosted: Wed Nov 28, 2007 1:27 am    Post subject: Reply with quote

how about this (not tested):
Code:
Value := COM_InvokeDeep( pweb, "Document.forms.Item[1].elements.Item[2].options.Item[0].value" )

COM_InvokeDeep(obj,path)
{
   res:=obj
   loop,parse,path,.,]
   {  prop=%A_LoopField%
      value=
      StringGetPos,i,A_LoopField,[
      IfEqual,ErrorLevel,0
      {   StringLeft,prop,A_LoopField,%i%
         StringMid,value,A_LoopField,% i+2
      }
      res:=COM_Invoke( res, prop, value)
   }
   return res
}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
hotzenpl0tz



Joined: 22 Oct 2007
Posts: 11

PostPosted: Wed Nov 28, 2007 11:12 am    Post subject: Reply with quote

Wow, cool stuff. Thanks for the pointers !
Back to top
View user's profile Send private message
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Mon Dec 03, 2007 2:53 pm    Post subject: Reply with quote

Tested (adapted COM_InvokeDeep):

Code:
#Include %A_ScriptDir%\COM.ahk

COM_CoInitialize()
pweb := COM_Createobject("InternetExplorer.Application")
COM_Invoke(pweb, "Visible=", "True")
COM_Invoke(pweb, "Navigate", "http://www.mredkj.com/tutorials/tutorial005.html")
Sleep 3000

MsgBox % COM_InvokeDeep( pweb, "document.forms[1].elements[2].options[0].value" )


COM_InvokeDeep( obj, path )
{
   res := obj
   Loop, Parse, Path, ., ]
   {
         prop := A_LoopField
      value =
      StringGetPos, i, A_LoopField, [
      IfEqual, ErrorLevel, 0 ; contains index
      {
            StringLeft, prop, A_LoopField, %i%
         StringMid, value, A_LoopField, % i+2
      }
         If (value != "") ; contains index
         {
            res := COM_Invoke( res, prop )
            res := COM_Invoke( res, "Item", value )
         }
         Else
         {
           res := COM_Invoke( res, prop )
         }
   }
   Return res
}


Return

_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Mon Dec 03, 2007 3:46 pm    Post subject: Reply with quote

Hmm...

This one works:

Code:
MsgBox % COM_InvokeDeep( pweb, "document.documentElement.innerHTML" )


This one doesn't:

Code:
MsgBox % COM_InvokeDeep( pweb, "document.getElementById('topbanner').innerHTML" )


What is needed to make the second one work? COM experts?
_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Tue Dec 04, 2007 3:28 pm    Post subject: Reply with quote

Bump...

Anybody?
_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
ahklerner



Joined: 26 Jun 2006
Posts: 1381
Location: USA

PostPosted: Thu Dec 06, 2007 1:24 am    Post subject: Reply with quote

Quote:
getElementById('topbanner')
is a javascript function. i doubt it is supported by COM the way you are calling it.
_________________

ʞɔпɟ əɥʇ ʇɐɥʍ
Back to top
View user's profile Send private message
ahklerner



Joined: 26 Jun 2006
Posts: 1381
Location: USA

PostPosted: Thu Dec 06, 2007 1:33 am    Post subject: Reply with quote

2 hotzenpl0tz
The following will get the info you want into an ahk variable.

Code:
MsgBox % JSVAR := IE_InjectJS(WinExist("A"), "javascript:var ahkvar1 = parent.document.forms[1].elements[2].options[0].value;", "ahkvar1")


You can find the function here:
http://www.autohotkey.com/forum/viewtopic.php?t=25473
Back to top
View user's profile Send private message
hotzenpl0tz



Joined: 22 Oct 2007
Posts: 11

PostPosted: Thu Dec 06, 2007 5:26 pm    Post subject: Reply with quote

ahklerner: Yes, thanks alot. With the help of the examples above, I was able to adapt it to my given problem and was able to automate a process that had different routes depending of the state of a select object on the page. As soon as I am finished with everything, I am planning to find some random webpage on the net with the elements I have used and writing a simple script, that automates some things on that webpage in order to give beginners some (commentated) insight on how certain things can be done to achieve these things.
The problem is, at the moment I am learning a new trick every day, and am rewriting my script at work almost every time I have some time to do so - so this might take some time Smile But helping ahk beginners who are interested in IE automation tasks would be a good way to give back to the community here, a really great community btw. !
Back to top
View user's profile Send private message
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Thu Dec 06, 2007 5:36 pm    Post subject: Reply with quote

Quote:
is a javascript function. i doubt it is supported by COM the way you are calling it.


Darn
_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7293
Location: Australia

PostPosted: Mon Dec 17, 2007 10:45 am    Post subject: Reply with quote

daonlyfreez wrote:
Quote:
is a javascript function. i doubt it is supported by COM the way you are calling it.

Darn
I think ahklerner means "COM_InvokeDeep", which appears to be designed to invoke a string of dot-delimited object properties (not methods.)
Code:
pdoc := COM_Invoke(pweb, "document")
pelem := COM_Invoke(pdoc, "getElementById", "topbanner")
innerHTML := COM_Invoke(pelem, "innerHTML")
COM_Release(pelem)
COM_Release(pdoc)
MsgBox % innerHTML
Untested.
Back to top
View user's profile Send private message Visit poster's website
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Mon Dec 17, 2007 11:26 am    Post subject: Reply with quote

Brilliant!

Thank you very much!

Cool
_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group