Help with youtube download

I know how to get a RSS feed of a youtube playlist.
The script below makes a list of youtube links from the RSS feed.

property thelist : {}
tell application "Safari"
	set num_links to (do JavaScript "document.links.length" in document 1)
	repeat with i from 0 to num_links - 1
		tell application "Safari" to set current_link to do JavaScript "document.links[" & i & "].href" in document 1
		set allitems to every item in thelist
		if current_link contains "watch?v=" then
			if current_link is not in allitems then
				set the end of thelist to current_link
			end if
		end if
	end repeat
	end tell

I want to know how to apply the javascript below to the URL

(If a youtube page like http://www.youtube.com/watch?v=tZ0MYSjVwLU is opened in Safari and the above javascript is put in the address bar, then the video starts downloading in high quality)
I tried

tell application "Safari"
	repeat with aitem in thelist
		open location aitem
		 
		set javaURL to "javascript:window.location.href%20=%20'http://youtube.com/get_video?video_id='%20+%20swfArgs['video_id']%20+%20"&fmt=18"%20+%20"&l="%20+%20swfArgs['l']%20+%20"&sk="%20+%20swfArgs['sk']%20+%20'&t='%20+%20swfArgs['t'];"
		
		delay 10
		set the URL of current tab to javaURL
	end repeat
end tell

I get the error:

and one of the double quotes in the javascript is highlighted.

You need to escape the " with a \

example "

But you may run into other issues with that javascript. thing this may work out better.

tell application "Safari"
	make new document with properties {URL:"http://www.youtube.com/watch?v=tZ0MYSjVwLU"}
	delay 2
	do JavaScript ("if(document.location.href.match(/http:\\/\\/[a-zA-Z\\.]*youtube\\.com\\/watch/)){document.location.href='http://www.youtube.com/get_video?fmt='+(isHDAvailable?'22':'18')+'&video_id='+swfArgs['video_id']+'&t='+swfArgs['t']}") in document 1
end tell

Thanks Mark.
The “do javascript” will work only in Safari. It won’t work in Camino. Is there any alternate solution for browsers(or atleast for Camino) which do not support “do javascript”?

When I use this script,

set aitem to "http://www.youtube.com/watch?v=RHKCrmKASjU&feature=response_watch"
tell application "Camino"
	open location aitem
	set javaURL to "javascript:window.location.href%20=%20'http://youtube.com/get_video?video_id='%20+%20swfArgs['video_id']%20+%20\"&fmt=18\"%20+%20\"&l=\"%20+%20swfArgs['l']%20+%20\"&sk=\"%20+%20swfArgs['sk']%20+%20'&t='%20+%20swfArgs['t'];"
	
	delay 4
	tell window 1 to set URL of current tab to javaURL
end tell

I get the following error:

??? worked on mine

Ahh, just updated my Camino, and now it does not work.

Seems camino does not like javascript.

If you use a normal URl it works?

It is a bug in Camino. As other test show me I can get Javascript working with some changes and copy and paste.

So a work around would be this.

set aitem to "http://www.youtube.com/watch?v=RHKCrmKASjU&feature=response_watch"
tell application "Camino"
	open location aitem
	set javaURL to "javascript:document.location.href='http://www.youtube.com/get_video?fmt='+(isHDAvailable?'22':'18')+'&video_id='+swfArgs['video_id']+'&t='+swfArgs['t'];"
	delay 4
	tell application "System Events"
		
		tell process "Camino"
			set value of text field 1 of splitter group 1 of group 1 of tool bar 1 of window 1 to javaURL
			perform action "AXConfirm" of text field 1 of splitter group 1 of group 1 of tool bar 1 of window 1
		end tell
	end tell
	
end tell

Yes, it works for me too. Thanks a lot, Mark.