Thursday, January 18, 2018
  • Index
  •  » Code Exchange
  •  » Return web content with the do JavaScript command after Lion

#1 2014-12-03 12:44:26 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Return web content with the do JavaScript command after Lion

Hello.

The functionality of returning contents with do JavaScript has apparently been broken since Lion.

I found the fix over at Apple Discussusions Forum

The first example is from that page, which demonstrates how to get the current selection.

Applescript:

tell application "Safari"
   activate
   set selectedText to (do JavaScript "var x = ''+document.getSelection();x" in document 1) as string
end tell

The second example is one of my own, which illustrates how to traverse the document tree by an anynomuous self invoking function. it should return the contents of the signatures of a page from macscripter. You should press command opt u, to be able to see the page source to verify that it works. There is a good JavaScript tutorial over at w3schools.com.

Applescript:

tell application "Safari"
   activate
   set signatures to (do JavaScript "var toReturn = ''+ (function () {
    var eL = document.getElementsByClassName('postsignature') ;
   var txt = '';
   for (i=0;i<eL.length;i++){
       txt = txt + eL[i].innerHTML ;
   }
   return txt;
   })();toReturn;"
in document 1) as string
end tell
log signatures


Filed under: javascript, safari, scraping

Offline

 
  • Index
  •  » Code Exchange
  •  » Return web content with the do JavaScript command after Lion

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)