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.

2015-10-27

Facebook


I recently left Groupon, and I am as surprised as people who know me about where I am heading next. :-)

Starting Monday, I'll be doing Facebook bootcamp.

Interviewing for various companies was interesting. My litmus test proved pretty useful, and was a great answer to the standard question about what kind of impact I'd like to have, which I otherwise rarely have anything in particular to say about. This time, I could assess cultural fit, apples to apples, as everybody and their grandmother seem to have this issue.

At Google, I got the impression each time I brought it up with an interviewer, that this was Somebody Else's Problem, and likely hard to do anything about without ending up on exactly the right team, or working against the grain of bureaucracy.

At Facebook, everybody I talked to got really interested, lit up when they got that localStorage is a handy cross-window message bus, not just a storage mechanism, and the interviewer working closest to teams poking at that kind of stuff started scribbling profusely in his notebook. A little later, after describing new hire procedures, he mentioned that I probably could end up getting to fix that myself during bootcamp.

Sold! :-)

2015-09-23

Notifications on the Web

Hey, web developers!

Have you ever clicked that GMail, Google+, Github, Facebook, or other web site icon or browser tab title that says you have some unread notification waiting for you, only to find that nope, you don't? Because you already saw it, in another open browser tab?

Yes, you have. You, and millions of other people that suffer this every day. In 2015. There is a very simple cure, shipping since 2009. Yes, even IE8 has it. Try opening this example page demonstrating it in two or more browser tabs or windows, and click the buttons in it, and observe how they all update together, instantly. For convenience, the example describes how to do it, too.

It's easy, and supported everywhere. A localStorage event listener is all you need.

Now please sneak this into your next site update, if you love your users. Notifications that lie to us make us all sad.