Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Using JavaScript in AHK script to control web pages


  • Please log in to reply
15 replies to this topic
Pallie
  • Members
  • 56 posts
  • Last active: Feb 23 2005 11:53 PM
  • Joined: 05 Jul 2004
You can type JavaScript statements in the address bar and the browser will run the code as though it was part of the web page. If you type the following on any page
javascript:alert(document.lastModified)
it will display the date/time stamp for the page you are on (although I'm not sure how reliable this value is).

It remembered this when I read a question on the support forum.

http://www.autohotke...topic.php?t=911

but there is more that you can do with javascript in ahk scripts. I am no JavaScript expert but here are some examples.
selectaddress = !D^{HOME}{SHIFT DOWN}^{END}{SHIFT UP}^C
; get a list of links on the page
#i::
command = javascript:function ROIoiW(){var map='';DL5e=document.links;for(lKi=0;lKi<DL5e.length;lKi++){map = map + DL5e[lKi]+'|'}prompt('map',map);}ROIoiW();
gosub, sendcommand
Return

 sendcommand:
 send, %selectaddress%
 clipboard = %command%
 send, ^V{enter}
 send, ^c{enter}
 StringReplace, clipboard, clipboard, |, `n , all
 msgbox, %clipboard%
Return
; on www.msn.com, set the stock quote field to b autohotkey
#j::
 command = javascript:function wewe(){document.forms["getquote"].Symbol.value='autohotkey';}wewe();
 send, %selectaddress%
 clipboard = %command%
 send, ^V{enter}
Return
; position the cursor on the search input field
#K::
 command =  javascript:document.STWF.q.focus()
 send, %selectaddress%
 clipboard = %command%
 send, ^V{enter}

Return
; various commands (uncomment to test)
#L::
; show which page you came from
; command =  javascript:function wewe(){prompt('test',document.referrer)}wewe();
; get the URL
; command =  javascript:function wewe(){prompt('test',document.URL)}wewe();
; get the path in the URL
 command = javascript:function wewe(){prompt('test',document.location.pathname)}wewe();
 send, %selectaddress%
 clipboard = %command%
 send, ^V{enter}
 send, ^c{enter}
 msgbox, %clipboard%
Return


(you'll need to be careful about line feeds etc, some of the clode lines are quite long and need to be on one line

There is a handy web page that gives bits of code that may be useful herehttp://www.bookmarkl...ls/categor.html. They suggest adding the link to you favouries but you can right-click on a link and copy the shortcut to the clipboard and paste it into and address bar or into your script.

Mike

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks for sharing these tips.

webber
  • Members
  • 129 posts
  • Last active: Aug 13 2011 06:45 PM
  • Joined: 25 Aug 2005
how does this script get run ?

Opening the file doesn't do anything .
________
Ford Ltd History

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
You have to have a browser running with javascript enabled, then press win+i/k/j/l
These are all hotkeys form the script. I havn't tested this though.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

  • Guests
  • Last active:
  • Joined: --
Modern browsers support page specific modifications natively (putting focus to search field, etc.), so you're better off with Greasemonkey or User Javascript if you use Firefox or Opera respectively.

AHKnow
  • Members
  • 121 posts
  • Last active: May 17 2009 09:11 PM
  • Joined: 03 Jul 2004
Among the annoying things when automating web pages is being able to click certain buttons on a webpage.

There is a couple of ways of going about this:

1.
ControlSend,   ClassNN , {Enter},  Title

Note- With web pages the ClassNN will often be for the entire frame or form, nevertheless it helps. This is also more reliable than simple Send, Key.


2.
javascript:parent.fnExpressScan(event)

What does that mean?

A. If you right click on say the Windows Update page ( http://update.micros... ... x?ln=en-us ), you can select "View Source"

Note 1- Try the splash page section, near the "Express" and "Custom" buttons.

Note 2- You may also be able to get the information for the "Button" by looking at the title bar at the bottom of the browser when your mouse is over the "Button".

B. In Notepad you can do a search for onclick.

C. Often you will find things like
onclick="javascript:parent.fnExpressScan(event);"

The onclick is specifying an action/event/function when the user presses click.

D. This "action/event" can be typed in the Url/Address Bar of your web browser, thus...

javascript:parent.fnExpressScan(event)

E. You can use AutoHotkey commands like....

ControlSetText, ControlName (like Edit1 for the Address Bar), javascript:parent.fnExpressScan(event), WinTitle

... to place the javascript commands into the web browser, where you place URLs.

Another interesting thing about JScript and Javascript is that if you use and understand AutoHotkey, its not too hard to understand those scripting languages.

Some Tutorials and Informaton:


http://www.pageresource.com/jscript/ (JavaScript Lessons Online)

Microsoft Jscript Online
http://msdn.microsof...soriJScript.asp

Microsoft Windows Scripts downloads (including downloadable documentation)
http://msdn.microsof...list/webdev.asp

http://www.freeprogr...om/jscript.html (Various JavaScript Tutorials)

Special Note:

JavaScript is also used by Adobe and with their .pdfs . Check out below for examples of its use

http://www.planetpdf...?ContentID=6575

http://www.planetpdf...torial/calc.pdf (PDF Calculator)

M^ck^y
  • Members
  • 31 posts
  • Last active: Sep 28 2010 12:36 AM
  • Joined: 29 Nov 2007
What if the Address bar was not visible, is there a way to get the browser to run the javascript?

sharethewisdom
  • Members
  • 57 posts
  • Last active: Dec 18 2014 08:44 AM
  • Joined: 24 Feb 2008

Microsoft Internet Explorer has a maximum uniform resource locator (URL) length of 2,083 characters. Internet Explorer also has a maximum path length of 2,048 characters.


I have a bookmarklet that is almost to long (because it doesn't allow a seperate style sheet) to store as a bookmarklet.
if I paste my javascript in the adress bar there are 23 characters missing at the end. (2,083 -2,048 = 35)

the script displays image files from an open dir in a new html window (in a table), (extend with the choice of the amount of img on a page,..). It appeared very usefull. I hoped to solve my problem and extend my script with a ahk script, but:

(how-) Do I have to rewrite-start-all-over this script in ahk to display those images in a GUI window, (...) or is it still possible to let a ahk script automatically launch my js (without using the adress bar to paste: let it make a bookmark containing my js?...)

launch it on:
SetTitleMatchMode, 2
DetectHiddenText, On
WinWaitActive, Microsoft Internet Explorer, Index of
...

see askforhelp topic

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
just fyi this is a very old post so its unlikely any one looks at it much any more
but executing javascript and a host of other automation tasks can also be done...

Com

Automate IE7 Navigation and Scripting with Tabs
Never lose.
WIN or LEARN.

  • Guests
  • Last active:
  • Joined: --
:?: how to call javascript function in ahk file by including .js file in it

  • Guests
  • Last active:
  • Joined: --
You can't. See http://www.autohotke... ... Automation

  • Guests
  • Last active:
  • Joined: --
But i want to know how to use javascript for mathematical calculations in authotkey script... :cry:

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
And what pray tell, does performing math with javascript have to do with browsers? How are they better in javascript than ahk(I can think of a few ways but do you know?)? There is an answer to be found in the faq for browser automation but I am unsure its the right answer
Never lose.
WIN or LEARN.

  • Guests
  • Last active:
  • Joined: --
but wht i want to know is that .....suppose if i want to have simple calculation of square of any digit coded in java script .........how can i use this javascript code in ahk....this is nothing to do with browsers...plz tell

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
Your missing the point

Ahk can perform the calculation
n to e power can be calculated as n**e

Or if you simply feel the retarded need to go thru the additional complexity of mixing languages, you can use the scrit control as demonstrated in jethrows com reference.

Your basically asking for an english to portugese translation translated to english. Its stupid
Never lose.
WIN or LEARN.