Is there a trick to sending javascript to a browser window?

I know that you have to enable js from appleevents in the dev menu. But beyond that, its still not working.

I’m trying to open a web page, and load some text into a textarea on that page.

The raw Applescript does work when I type it in to the console. But when Applescript tries to send it, nothing happens.

	activate
	make new document with properties {URL:"https://www.spamcop.net"}
	do JavaScript "document.getElementsByName('spam')[0].value = 'Test';" in document 1
end tell

Ok in the process of making this post, I tried adding a delay between making the new document, and doing the javascript, and now it works.

So I guess the question has changed. Now I want to know how I can make applescript wait for the document to be done loading before it sends the javascript?

OR do I do that entirely in javascript, on documentready or whatever, in the script itself?

Ok so playing around with it a little bit. My code

do JavaScript "document.getElementsByName('spam')[0].value = 'Test';" in document 1

Works every time if the window is already open.

When opening a new window, it does not work (probably because the page is still loading).

So I tried this javascript code instead but it also does not work.

set JSTestCode to "window.onload = function()
	{
	document.getElementsByName('spam')[0].value = 'Test';
	};"

tell application "Safari"
	do JavaScript JSTestCode in document 1
end tell

Ok from another post on this forum, I’m trying to create a loop that checks documnet.readyState on ‘document 1’ and only proceeds when the readyState is “complete”

New problem is, it seems to return “complete” right away, as soon as you ask the first time. Even though it is NOT done loading and not yet able to accept other scripts yet.

tell application "Safari"
	activate
	make new document with properties {URL:"https://www.spamcop.net"}
	
	repeat 1000 times
		set WindowStatus to do JavaScript "document.readyState" in document 1
		log WindowStatus
		if WindowStatus is equal to "complete" then
			exit repeat
		end if
		delay 0.25
	end repeat
	do JavaScript "document.getElementsByName('spam')[0].value = 'Test';" in document 1
	
end tell

WindowStatus is equal to “complete” instantly.
I just realized the post I was reading from is 14 years old, that might explain why its not working as expected.

Often properties like that (readyState) seem to respond before the page has even started to load.
You could use a try…end try block around JavaScript that checks for the object you actually want.
Maybe something like the following inside a loop will help you find out how the element selector code works or gets an error when not yet ready:
try
set startValue to do JavaScript “document.getElementsByName(‘spam’)[0].value“ in document 1
display dialog startValue
on error errMsg number errNum
display dialog errNum
end try

Once you know the error for “not available yet”, you could ignore those and exit the loop once the object is available, then set it after the loop.

Alright I checked for the element, not exactly the way you suggested but kind of similar in spirit. This is the code and it seems to be working:

tell application "Safari"
	activate
	make new document with properties {URL:"https://www.spamcop.net"}
	
	repeat 1000 times
		set TextArea to do JavaScript "document.getElementsByName('spam').length" in document 1
		log TextArea
		if (TextArea as integer) is greater than 0 then
			exit repeat
		end if
		delay 0.25
	end repeat
	do JavaScript "document.getElementsByName('spam')[0].value = 'Test';" in document 1
	
end tell

So its twiddling it’s thumbs until there is at least one object on the page named ‘spam’, which for this web page is all I need. I’ll try to expand out the rest of the script now and see what I can come up with.

Ok here is HOPEFULLY the last problem:

I’m getting a block of raw email source from mail, and I’m sending it to javascript, inside of Safari.

I’m getting javascript errors because, of course, this data needs lots of escaping. But what kind of escaping? I probably only need to escape single and double quotes right? Is there a way to do this in Applescript?