How to get url of selected image in google images

automatorsafari

I write Automator workflow which will attach google image miniature, able to be selected on context menu or hotkey enabled.

Now I am trying to figure out which type of object sees Automator when I right-click on image in safari. I tried web-content, image, documents, everything else, but nothing works.

Also I tried actions like Get opened safari document + load images from web-content, but it is not my case.

Is it possible at all, to add context menu option to google image?

Best Answer

Okay, my way is to ask Safari do javascript which will get all what I need (I mean urls of full-images) for current page, when user presses hotkey.

The trick is that when you select (hower) image, you see a black box at bottom of mini-image. Certanly, it means that HTML has been changed, so I just figured out that changes (display property changes from none to block) and ask JS to get any blocks with that property, when user presses key.

See code below for details.

tell application "Safari"
set result to (do JavaScript " 
               function f() 
               { 
                   var imageHolderElements =  document.getElementsByClassName('_aOd rg_ilm'); 
                   for (var i = 0; i < imageHolderElements.length; i++) 
                   { 
                       var style = getComputedStyle(imageHolderElements[i], null); 
                       var display = style.getPropertyValue('display'); 
                       if (display == 'block') 
                       { 
                           var src = imageHolderElements[i].parentNode.getAttribute('href'); 
                           var parameter = 'imgurl='; 
                           var index = src.indexOf(parameter) + parameter.length; 
                           console.log(index); 
                           var result = src.substring(index, src.length); 
                           console.log(result); 
                           var paramDelim = result.indexOf('&'); 
                           if (paramDelim > 0){ 
                                var result_result = ''; 
                               result_result = result.substring(0, paramDelim); 
                           } 
                           console.log(result_result); 
                           return result_result; 
                       } 
                   }
               } 
                var x = 1; 
               var x = '' + f(); x" in current tab of first window) as string
return result
end tell