Javascript issue that I only partially understand.

The issue has to do with attempting to write an Applescript that opens original sized photos while browsing the site vk.com. I have created a similar script for Instagram that I am trying to create here for VK. I press a key combo that triggers Keyboard Maestro to open a higher res version of the photo that I am currently looking at on Instagram. The information on how to get to those larger files on Instagram is somewhat hidden. On VK there is a link to the original sized file, but it is not as easy as just having Javascript click a link.

Here is a link to an example page:
https://vk.com/photo3353595_431330122

And a screen shot of the page, it is the “More” link that I am trying to “click” (FWIW I am able to click the “Share” and “Save to my photos” links using Javascript with no issue.)

And lastly here is a screen shot of Safari’s web inspector, this is the state the page is in when it has been freshly reloaded.

You can see the “More” button that is nested within the .
You can also see that unlike the first two “Share” and “Save…” links there is no ID for “pv_more_actions” just the class name.

Now when you hover over the “More” link you get the following:

There is that link I am looking for!

So this works once the “More” button has been hovered over:

tell application "Safari"
	activate
do JavaScript " document.getElementById('pv_more_act_download').click()" in current tab of first window
end tell

Here is what Safari’s Web Inspector shows once the link has been hovered over:

So now there are additional Elements after “More”.

So what I am trying to do is to automate the loading of those elements so that the “pv_more_act_download” element is exposed which I can then use javascript to click on.

So far I have tried all of these javascript commands to try and get the elements for the “More” button to load with no luck.

	do JavaScript " document.getElementById('pv_more_acts_tt').click()" in current tab of first window
	do JavaScript " document.getElementByClassName('pv_more_actions' 'pv_more_shown').click()" in current tab of first window
	do JavaScript " document.getElementByClassName(''pv_more_shown'').click()" in current tab of first window
	do JavaScript " document.getElementByClassName('pv_more_actions').click()" in current tab of first window
	do JavaScript " document.getElementByClassName('pv_more_actions').mouseenter()" in current tab of first window
	do JavaScript " document.getElementByClassName('pv_more_actions').mouseleave()" in current tab of first window
	do JavaScript " document.getElementByClassName('pv_more_actions').mouseenter()" in current tab of first window
	do JavaScript " document.getElementByClassName('pv_more_actions').mouseleave()" in current tab of first window

I have also tried .hover() with no success.

I have searched high and low looking for a solution to hover over that more button to get those elements to load. I have even thought of cliclick, but give the button is on the bottom right hand corner of each window it is hard to predict where the button might be for any given photo.

So please someone tell me I am just dumb and the answer is staring me in the face!

Hey Chris,

I tried several things which didn’t work.

Here’s what did:


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/30 14:47
# dMod: 2016/08/30 14:47 
# Appl: Safari
# Task: Load Original Image Link from vk.com image.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Load, @Original, @Image, @Link, @vk.com
-------------------------------------------------------------------------------------------

set originalImageURL to doJavaScriptInSafari("document.getElementById('pv_tag_frame').innerHTML")
set AppleScript's text item delimiters to "\""
set originalImageURL to text item 2 of originalImageURL
loadSafariUrl(originalImageURL, true)

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on doJavaScriptInSafari(javascriptStr)
	try
		tell application "Safari" to do JavaScript javascriptStr in front document
	on error e
		error "Error in handler doJavaScriptInSafari() of library NLb!" & return & return & e
	end try
end doJavaScriptInSafari
-------------------------------------------------------------------------------------------
on loadSafariUrl(_url, newDocFlag)
	tell application "Safari"
		if newDocFlag = true or newDocFlag = 1 then
			make new document
			set bounds of front window to {228, 23, 1542, 1196}
		end if
		
		set URL of front document to _url
	end tell
end loadSafariUrl
-------------------------------------------------------------------------------------------


Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.6 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Thanks for trying!

Hey Chris,

You did notice that I said the above code DOES work? :cool:

-Chris

I missed that.

Thanks so much!

How were you able to figure out the tag?

One edit I made to your script was how the new document is made. Better to add in the URL to the new document request, it is just cleaner that way.

set newWindow to make new document at end of documents with properties {URL:_url}

Once again, thanks so much!

Hey Chris,

If memory serves I opened the full-size image and noted the URL.

Then I used the Web Inspector in Safari to find it in the code.

Then I backed up to the first available node.

-Chris

For the record: There is no getElementByClassName() function in Javascript only getElementsByClassName which returns always an array even if there is one item. Then the elements you’re looking for are not named ‘pv_more_actions’.

I Like ccstone’s approach of getting the original URL somewhere else (it’s twice in the document) but there is plenty of room for optimization. Once you’re in JavaScript you don’t have to leave, JavaScript itself is filled with useful functions that can handle this for you:

tell application "Safari"
	activate
	tell document 1
		do JavaScript "var loc = document.getElementById('pv_tag_frame').childNodes[0].src;
		window.open(loc, '_blank');"
	end tell
end tell

Hey DJ,

Yeah, that’s nice.

-Chris