// Shows the number of new messages in the browser tab title while the tab is in the background. (function () { var box = document.getElementById('chat'); if (!box) return; var title = document.title, unread = 0, seen = 0; function ids() { return Array.prototype.map.call(box.querySelectorAll('.chat-msg'), function (m) { return +m.dataset.id; }); } seen = Math.max.apply(null, ids().concat(0)); box.addEventListener('chat:updated', function () { var fresh = ids().filter(function (id) { return id > seen; }); if (!fresh.length) return; seen = Math.max.apply(null, fresh.concat(seen)); if (document.hidden) { unread += fresh.length; document.title = '(' + unread + ') ' + title; } }); document.addEventListener('visibilitychange', function () { if (!document.hidden) { unread = 0; document.title = title; } }); })();