Limitations

Limitations when using JXA

JXA does not run in the browser. So all global objects available there are not accessible in JXA scripts. This concerns window (with methods like alert()), document (with all DOM methods like getElementById()) and navigator.

Most importantly, you cannot use XHR calls from JXA scripts (XMLHttpRequest(), fetch() etc.). Following that, there’s no (easy) way to work with the DOM of a web document.

You can download a web document with (for example) curl and then access its content with the methods for strings and regular expressions. Alternatively, you could use the Objective-C bridge:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const urlStr = "https://stackoverflow.com/";
const htmlStr = getHTMLSource(urlStr);

function getHTMLSource(URL) {  
  const nsURL     = $.NSURL.URLWithString(URL);
  const nsHTML     = $.NSData.dataWithContentsOfURL(nsURL);
  const nsHTMLStr = $.NSString.alloc.initWithDataEncoding(
    nsHTML,  $.NSUTF8StringEncoding);

   return ObjC.unwrap(nsHTMLStr);
} 

This code was originally posted in the JXA Cookbook on Github

Regardless of the method to download a web document, you will not have access to its DOM from JXA.

About