Created
May 30, 2015 02:24
-
-
Save RobTrew/416ae1a896e06234c92c to your computer and use it in GitHub Desktop.
Harvest Google result links from Safari with XPATH (as MD) with //*[@Class='r']/a
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// YOSEMITE Javascript for Applications JXA | |
// Capture the main result links from a Google search page as Markdown | |
// ( Using XPath to search for <a> elements which are children | |
// of elements with class 'r' | |
var resultLinksMD = function () { | |
var r = document.evaluate( | |
"//*[@class='r']/a", | |
document, null, 0, null | |
), | |
lst = [], a; | |
while (a = r.iterateNext()) { | |
lst.push("[" + a.text + "](" + a.href + ")"); | |
} | |
return lst.join("\n"); | |
}; | |
function run() { | |
var app = Application.currentApplication(), | |
appSafari = Application("Safari"), | |
lstDocs = appSafari.documents(), | |
oDoc = lstDocs.length ? lstDocs[0] : null, | |
strJS = "(" + resultLinksMD.toString() + ")()"; | |
var strResult= (oDoc && (oDoc.name().indexOf("Google Search") !== -1)) ? | |
appSafari.doJavaScript(strJS, { in: appSafari.windows[0].currentTab }): | |
"Not a Google Search page:\n" + oDoc.url(); | |
app.includeStandardAdditions = true; | |
app.setTheClipboardTo(strResult); | |
return strResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compare with Applescript version:
https://gist.github.com/RobTrew/e06df61f257ec1168cbe