Skip to content

Instantly share code, notes, and snippets.

@angrycub
Created March 20, 2026 12:57
Show Gist options
  • Select an option

  • Save angrycub/b797fbc2f3f3f05d5c32befaa3c41497 to your computer and use it in GitHub Desktop.

Select an option

Save angrycub/b797fbc2f3f3f05d5c32befaa3c41497 to your computer and use it in GitHub Desktop.
Simple NodeJS Referrer/Opener Test Page
const http = require("http");
const PORT = 3000;
const html = (referer) => `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Referrer & Opener Info</title>
<style>
body { font-family: monospace; max-width: 800px; margin: 40px auto; padding: 0 20px; background: #1a1a1a; color: #e0e0e0; }
h1 { color: #7eb8f7; }
h2 { color: #a8d8a8; border-bottom: 1px solid #444; padding-bottom: 8px; }
.card { background: #2a2a2a; border: 1px solid #444; border-radius: 6px; padding: 16px; margin-bottom: 20px; }
.label { color: #888; font-size: 0.85em; text-transform: uppercase; letter-spacing: 0.05em; }
.value { color: #f0c040; word-break: break-all; margin-top: 4px; }
.none { color: #666; font-style: italic; }
pre { background: #111; border-radius: 4px; padding: 12px; overflow-x: auto; color: #ccc; }
.open-btn { background: #3a6ea8; color: white; border: none; padding: 10px 18px; border-radius: 4px; cursor: pointer; font-family: monospace; font-size: 1em; margin-top: 8px; }
.open-btn:hover { background: #4a7eb8; }
</style>
</head>
<body>
<h1>Referrer &amp; Opener Info</h1>
<h2>HTTP Referer Header (server-side)</h2>
<div class="card">
<div class="label">Referer</div>
<div class="value" id="server-referer">${referer ? escapeHtml(referer) : '<span class="none">none</span>'}</div>
</div>
<h2>document.referrer (client-side)</h2>
<div class="card">
<div class="label">document.referrer</div>
<div class="value" id="doc-referrer"></div>
</div>
<h2>window.opener (client-side)</h2>
<div class="card">
<div class="label">window.opener exists</div>
<div class="value" id="opener-exists"></div>
<div class="label" style="margin-top:12px">window.opener details</div>
<pre id="opener-details"></pre>
</div>
<h2>Test: Open this page as a popup</h2>
<div class="card">
<p>Click to open this page in a new window — the new window will have <code>window.opener</code> set.</p>
<button class="open-btn" onclick="openChild()">Open child window</button>
</div>
<script>
// document.referrer
const docRef = document.getElementById("doc-referrer");
docRef.innerHTML = document.referrer
? document.referrer
: '<span class="none">none (empty string)</span>';
// window.opener
const openerExists = document.getElementById("opener-exists");
const openerDetails = document.getElementById("opener-details");
if (window.opener) {
openerExists.textContent = "yes";
const info = {};
try { info.href = window.opener.location.href; } catch (e) { info.href = "(cross-origin, access denied)"; }
try { info.origin = window.opener.location.origin; } catch (e) { info.origin = "(cross-origin, access denied)"; }
try { info.title = window.opener.document.title; } catch (e) { info.title = "(cross-origin, access denied)"; }
try { info.closed = window.opener.closed; } catch (e) { info.closed = "(unknown)"; }
openerDetails.textContent = JSON.stringify(info, null, 2);
} else {
openerExists.innerHTML = '<span class="none">no (null)</span>';
openerDetails.innerHTML = '<span class="none">window.opener is null — page was not opened via window.open() or target="_blank"</span>';
}
function openChild() {
window.open(window.location.href, "_blank", "width=700,height=600");
}
</script>
</body>
</html>`;
function escapeHtml(str) {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;");
}
const server = http.createServer((req, res) => {
if (req.url !== "/" && req.url !== "/favicon.ico") {
res.writeHead(404);
res.end("Not found");
return;
}
if (req.url === "/favicon.ico") {
res.writeHead(204);
res.end();
return;
}
const referer = req.headers["referer"] || req.headers["referrer"] || null;
res.writeHead(200, { "Content-Type": "text/html" });
res.end(html(referer));
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment