 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
hotzenpl0tz
Joined: 22 Oct 2007 Posts: 11
|
Posted: Tue Nov 27, 2007 5:31 pm Post subject: Javascript on a webpage - possible to evaluate result ? |
|
|
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 |
|
 |
wOxxOm
Joined: 09 Feb 2006 Posts: 320
|
Posted: Tue Nov 27, 2007 11:40 pm Post subject: |
|
|
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  |
|
| Back to top |
|
 |
BoBoĻ Guest
|
Posted: Wed Nov 28, 2007 12:29 am Post subject: |
|
|
| 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  |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1397
|
Posted: Wed Nov 28, 2007 12:37 am Post subject: |
|
|
May append this at the end of the script. Wow, looks scary.
| 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 |
|
 |
wOxxOm
Joined: 09 Feb 2006 Posts: 320
|
Posted: Wed Nov 28, 2007 1:27 am Post subject: |
|
|
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 |
|
 |
hotzenpl0tz
Joined: 22 Oct 2007 Posts: 11
|
Posted: Wed Nov 28, 2007 11:12 am Post subject: |
|
|
| Wow, cool stuff. Thanks for the pointers ! |
|
| Back to top |
|
 |
daonlyfreez
Joined: 16 Mar 2005 Posts: 755 Location: Berlin
|
Posted: Mon Dec 03, 2007 2:53 pm Post subject: |
|
|
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 |
_________________ (sorry, homesite offline atm) |
|
| Back to top |
|
 |
daonlyfreez
Joined: 16 Mar 2005 Posts: 755 Location: Berlin
|
Posted: Mon Dec 03, 2007 3:46 pm Post subject: |
|
|
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? _________________ (sorry, homesite offline atm) |
|
| Back to top |
|
 |
daonlyfreez
Joined: 16 Mar 2005 Posts: 755 Location: Berlin
|
Posted: Tue Dec 04, 2007 3:28 pm Post subject: |
|
|
Bump...
Anybody? _________________ (sorry, homesite offline atm) |
|
| Back to top |
|
 |
ahklerner
Joined: 26 Jun 2006 Posts: 1249 Location: USA
|
Posted: Thu Dec 06, 2007 1:24 am Post subject: |
|
|
| Quote: | | getElementById('topbanner') | is a javascript function. i doubt it is supported by COM the way you are calling it. _________________
ʞɔпɟ əɥʇ ʇɐɥʍ |
|
| Back to top |
|
 |
ahklerner
Joined: 26 Jun 2006 Posts: 1249 Location: USA
|
Posted: Thu Dec 06, 2007 1:33 am Post subject: |
|
|
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 |
|
 |
hotzenpl0tz
Joined: 22 Oct 2007 Posts: 11
|
Posted: Thu Dec 06, 2007 5:26 pm Post subject: |
|
|
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 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 |
|
 |
daonlyfreez
Joined: 16 Mar 2005 Posts: 755 Location: Berlin
|
Posted: Thu Dec 06, 2007 5:36 pm Post subject: |
|
|
| Quote: | | is a javascript function. i doubt it is supported by COM the way you are calling it. |
Darn _________________ (sorry, homesite offline atm) |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2739 Location: Australia, Qld
|
Posted: Mon Dec 17, 2007 10:45 am Post subject: |
|
|
| 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 |
|
 |
daonlyfreez
Joined: 16 Mar 2005 Posts: 755 Location: Berlin
|
Posted: Mon Dec 17, 2007 11:26 am Post subject: |
|
|
Brilliant!
Thank you very much!
 _________________ (sorry, homesite offline atm) |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|