2016-11-22

Unchoking

I have almost stopped writing about tech stuff in recent years, despite web APIs and javascript features catching up with sanity ever faster. What used to be a very horrible hack a few back to fetch a web page and produce a DOM you could query from it is at the moment both pretty readable and understandable:
const wget = async(url) => {
  try {
    const res = await fetch(url), html = await res.text();
    return (new DOMParser).parseFromString(html, 'text/html');
  } catch (e) {
    console.error(`Failed to parse ${url} as HTML`, e);
  }
};

wget('/').then(doc => alert(doc.title));
This already works in a current Google Chrome Canary (57). Sadly no javascript console support for doc = await wget('/'); you still have to use the "raw" promise API directly for interactive code, rather than syntax sugared blocking behaviour – but it's still a lot nicer than things used to be. And you can of course assign globals and echo when it's done:
wget('/').then(d => console.log(window.doc = d));
doc.title;
Querying a DOM with an XPath selector and optional context node is still as ugly as it always was (somehow only the first making it to the Chrome js console):
const $x = (xpath, root) => {
  const doc = root ? root.evaluate ? root : root.ownerDocument : document;
  const got = doc.evaluate(xpath, root||doc, null, 0, null);
  switch (got.resultType) {
    case got.STRING_TYPE:  return got.stringValue;
    case got.NUMBER_TYPE:  return got.numberValue;
    case got.BOOLEAN_TYPE: return got.booleanValue;
    default:
      let res = [], next;
      while ((next = got.iterateNext())) {
        res.push(next);
      }
      return res;
  }
};

const $X = (xpath, root) => Array.prototype.concat($x(xpath, root))[0];
...but for the corresponding css selector utilities ($$ and $ respectively), we can now say document.querySelectorAll() and document.querySelector(), respectively. Nice-to-haves. Like the lexically bound arrow functions. I guess web page crafters overall rarely if ever use XPath, and that it is an XML vestige we should be really happy that we have at all, through happy accidents of history, even though it needs a bit of shimming love to become lovable.
blog comments powered by Disqus