It's a shame that it does not look possible to do in JXA (at least I could not see any way to create the DOM document)
JXA doesn't have a built-in reference to a browser name-space, but it can, of course, still pass code over to Chrome or Safari, opening a window if there isn't one.
Here's a quick draft of a JXA example:
// passing the link HTML directly here (at bottom), but it could be from the value of a KM VAR
(function(strLinkHTML) {
// This function will be converted to a string and
// evaluated in the browser context, with an argument supplied
// by .apply()
function linkParse(strLinkHTML) {
var oDiv, oNode;
(oDiv = document.createElement('div')
).innerHTML = strLinkHTML;
return (
oNode = oDiv.firstChild
) ? "[" + oNode.text + "](" + oNode.href + ")" : ""
}
var appChrome = Application("Google Chrome");
lstWins = appChrome.windows();
lngWins = lstWins.length,
// If no window is open we make one
oWin = lngWins ? lstWins[0] : appChrome.Window().make(),
// Compose the .js we need ...
strJS = '(' + linkParse.toString() + ').apply(null, [\'' + strLinkHTML + '\'])';
// and run it in Chrome - the Safari syntax is slightly different here
return (
oWin.activeTab.execute({
javascript: strJS
})
);
})(
'<a href="http://www.google.com/?q=purescript">purescript</a>'
);