what can "do JavaScript" do in a browser?


I am sorry to say I could not google up an answer for this, so your help will be greatly appreciated.

I am still struggling with scripting some interaction with an ASP-based “web app” (basically a database), and as it seems, many queries don’t go through the URLs. They might result in a special URL for the file downloads I want, but each has a guid (unique identifier), which I cannot reverse engineer and automate, so I think I need to script the search queries themselves. But those go through JavaScript.

And here is my question : I know so little of JS, that from the “AppleScripting JavaScript” examples on the Web, I could not parse whether AppleScript can script the use of JS functions that are defined on the page itself, not general commands. Needless to say, my attempts to use the function that seems to communicate the search query to the site failed, and I got suspicious that problem might be the deeper in scripting : that I cannot access JavaScript coded up on the loaded site itself.

Sorry for this simple question, and thank you for your help!

Model: MacBook (early 2006)
AppleScript: 2.1.2
Browser: Safari 534.2
Operating System: Mac OS X (10.6)

Here’s an example.

The following JavaScript from Arc90 "makes reading on the web more enjoyable by removing the clutter around what you're reading" and the Readability site allows you to set Style, Size and Margin in the material. It is normally used by dragging a link from the Readability page to your Bookmarks Bar, and that puts the JavaScript below as the content of the bookmark. The page thus created in your browser has a link on it to return to the original source.

Visit "http://lab.arc90.com/experiments/readability/" for your own copy.

Because I'm an avid Quicksilver fan (but there are lots of alternatives), I prefer to set up a trigger to run the script below so I can enhance the readability of an article I'm looking at with a simple key combination. Although it cannot render every site I visit, it does remarkably well.
-- Note that the script below has been altered for readability. The original is all one string.

set JS to "readStyle='style-novel';
	_readability_print_css.rel='stylesh  eet';

tell application "System Events" to set last_app to item 1 of (get name of processes whose frontmost is true)
if last_app is "Safari" then
	tell document 1 of application "Safari" to do JavaScript JS
else if last_app is "NetNewsWire" then
	tell document 1 of application "NetNewsWire" to do JavaScript JS
	beep 3
end if

Thanks, Adam!

So you seem to imply that I can “do” a whole script if I write it from scratch, but you don’t think I can use what I coded up on the loaded/active page itself?

Perhaps I could still find, copy and paste the code from the source, but even that has references to further functions, and it gets messy.

As I’ve shown, you can write a JS and run it from an AppleScript, but you’re correct that you can’t insert a JS into the source of an active window directly from a script. Though I’ve never done it, you might grab the source of a site (using cURL to get it as text in your script), parse it to find the right place for your JS fragment, insert that in the source and then display it as HTML. e.g.:

set dottedFormOfMYIP to (do shell script "/usr/bin/curl -s http://checkip.dyndns.org/ | /usr/bin/grep -Eo '([[:digit:]]{1,3}\\.?){3}[[:digit:]]{1,3}'")