= 1 && mon !== undefined) {
if (new Date(year, mon, day).getTime() < today) {
n.parentNode.removeChild(n);
removed++;
} else { groupKept++; }
} else { groupKept++; } /* unreadable - keep it */
}
n = next;
}
/* A heading with nothing under it is worse than no heading. */
if (!groupKept && head.parentNode) head.parentNode.removeChild(head);
}
/* A title above an empty space reads as a page that failed to load,
which is the opposite of what happened. Say it plainly instead. */
var owner2 = (root.ownerDocument || document);
for (i = 0; i < lists.length; i++) {
if (!lists[i].querySelector('.ev')) {
var none = owner2.createElement('div');
none.className = 'ev none';
none.textContent = 'Nothing listed just yet.';
lists[i].appendChild(none);
}
}
return removed;
}
/* 🚨 innerHTML DOES NOT RUN SCRIPT TAGS. The app's buttons, the view
router and the video players all live in one script block, so injecting
the HTML and walking away would give a page that looks perfect and does
nothing at all - every button dead, no way to tell from a screenshot.
So the markup goes in, and each script is re-created as a real element
afterwards, which does execute. */
function inject(html) {
var doc;
try { doc = new DOMParser().parseFromString(html, 'text/html'); }
catch (e) { return false; }
if (!doc || !doc.body) return false;
/* Styles first, so nothing paints unstyled. TAGGED AND REPLACED, because
inject() can run twice - the bake first, the fresh copy after - and two
copies of a 56 KB stylesheet is waste the visitor pays for. */
var old = document.head.querySelectorAll('style[data-hnapp]');
for (var o = 0; o < old.length; o++) old[o].parentNode.removeChild(old[o]);
var head = doc.querySelectorAll('style');
for (var i = 0; i < head.length; i++) {
var st = document.createElement('style');
st.setAttribute('data-hnapp', '1');
st.textContent = head[i].textContent;
document.head.appendChild(st);
}
var scripts = [];
var nodes = doc.body.querySelectorAll('script');
for (var j = 0; j < nodes.length; j++) {
scripts.push(nodes[j].textContent);
nodes[j].parentNode.removeChild(nodes[j]);
}
/* Past days go before the paint, not after it. The cache below keeps
the unfiltered copy on purpose - it is re-filtered on every paint,
against the date of THAT paint. */
dropPast(doc.body);
host.innerHTML = doc.body.innerHTML;
for (var k = 0; k < scripts.length; k++) {
var sc = document.createElement('script');
sc.textContent = scripts[k];
document.body.appendChild(sc);
}
showing = html;
return true;
}
function cacheGet() {
try { return localStorage.getItem(LS_KEY) || ''; } catch (e) { return ''; }
}
function cachePut(html) {
/* Private windows and full storage both throw. A page that cannot save a
copy still works; it just has one layer fewer. */
try { localStorage.setItem(LS_KEY, html); } catch (e) {}
}
/* ── 1. PAINT IMMEDIATELY, FROM WHAT WE ALREADY HAVE ──────────────────
A browser that has run this page before holds something at least as new
as the bake, so it wins; otherwise the bake. Either way the resident has
a working app before a single byte leaves the phone. */
var first = cacheGet() || unbake();
if (first) inject(first);
/* Apps Script sends no CORS headers, so a cross-origin fetch() is blocked
outright. JSONP is not a preference here, it is the only route. */
function jsonp(url, cb, onErr) {
var name = 'hnApp_' + Date.now() + '_' + Math.floor(Math.random() * 1e6);
var s = document.createElement('script');
var timer = setTimeout(function () { cleanup(); onErr('timeout'); }, WAIT_MS);
function cleanup() {
clearTimeout(timer);
try { delete window[name]; } catch (e) { window[name] = undefined; }
if (s.parentNode) s.parentNode.removeChild(s);
}
window[name] = function (data) { cleanup(); cb(data); };
s.onerror = function () { cleanup(); onErr('network'); };
s.src = url + (url.indexOf('?') === -1 ? '?' : '&') + 'callback=' + name;
document.body.appendChild(s);
}
/* ── 2. REFRESH, QUIETLY ──────────────────────────────────────────────
Nothing here can take the page away. A failure is logged and dropped,
because the resident is already looking at a working app. */
function refresh(attempt) {
jsonp(FEED + '?feed=apphtml&network=' + encodeURIComponent(NETWORK),
function (res) {
if (!res || !res.ok || !res.html) return done('no page in the response', attempt);
if (res.html === showing) { cachePut(res.html); return; } /* already current */
if (inject(res.html)) cachePut(res.html);
},
function (why) { done(why, attempt); });
}
function done(why, attempt) {
if (attempt < 2) { setTimeout(function () { refresh(attempt + 1); }, 1200); return; }
/* 🚨 NO fail() ANY MORE, ON PURPOSE. The page already shows the app. A
message here would tell a resident something is wrong with a screen
that is working perfectly well. */
if (window.console && console.warn) console.warn('[hnapp] refresh gave up: ' + why);
}
/* If even the bake was missing - a truncated paste - the network is the
only hope, so try it straight away rather than on a delay. */
setTimeout(function () { refresh(1); }, first ? 400 : 0);
})();