do javascript made easy

Hi, I have developed a simple script that helps me locate the active elements of a web page in safari.


tell application "Safari"
	activate
	repeat with formIndex from 1 to 5
		repeat with elementIndex from 1 to 5
			set X to formIndex & "-" & elementIndex as text
			do JavaScript "document.forms[" & formIndex & "].elements[" & elementIndex & "].value = '" & X & "'" in document 1
		end repeat
	end repeat
end tell
say "done"

This Script works fine, bet the element number si sure to change with almost any adjustments to the page. So when doing a do javascript command I prefer to set or get the element by its name example:


tell application "Safari"
	activate
	do JavaScript "document.getElementById('User_name').value = 'John'" in document 1
end tell

The User_Name will not change so easy. So my question is… Is there a way of getting the ElementName with a do java script that gets the value with the element by number? the idea is to activate a script and get the element names I can use as a result.

any idea will help

Thank you

brian

Hi Brian,

I’m not good at javascript, but I think that you need to get the name attribute of the element. Each element may have several attributes I think, so you need to find which one holds the name that you want.

gl,
kel

Hi Brian,

I made my own form and got the name of the second attribute of the first element.

tell application "Safari"
	activate
	-- get number of forms
	set num_forms to (do JavaScript "document.forms.length" in document 1)
	-- get index of last form (0 based)
	set last_form_index to num_forms - 1
	-- get number of elements
	set num_elements to (do JavaScript "document.forms[" & last_form_index & "].elements.length" in document 1)
	-- get index of last element
	set last_element_index to num_elements - 1
	-- get name of second attribute of first element of first form
	do JavaScript "document.forms[0].elements[0].attributes[1].name" in document 1
end tell

It’s just in the last line. The other stuff was from trying to get a form from the internet.

Edited: I didn’t think that you wanted the node name of the element.

Edited: no, that gets the name of the attribute. What you want is the value of the attribute:

tell application "Safari"
	activate
	-- get number of forms
	set num_forms to (do JavaScript "document.forms.length" in document 1)
	-- get index of last form (0 based)
	set last_form_index to num_forms - 1
	-- get number of elements
	set num_elements to (do JavaScript "document.forms[" & last_form_index & "].elements.length" in document 1)
	-- get index of last element
	set last_element_index to num_elements - 1
	-- get name of second attribute of first element of first form
	do JavaScript "document.forms[0].elements[0].attributes[1].value" in document 1
end tell

gl,
kel

Hi Brian,

Think I have the plan! Only problem is you might need 3 nested repeat loops.

Let me get this straight though. You wanted to find the attribute name along with the element and form that matches the user name. Firstly, is the whole name in one attribute or is it first name in one attribute and last name in another? Since the repeat loops are nested, you need to set a flag when the user name is found so that it exits all repeat loops. Am I on the right track to what you’re trying to do?

Note that you can write a javascript script to do this also. But, it’s more fun with AppleScript. Hoping to hear from you.

gl,
kel

BTW, here’s the example form I got from somewhere on this site:
http://www.w3schools.com/js/js_examples.asp

Edited: Note that elements may not have attributes, so need to check on that with a javascript method.