-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta name="robots" content="noindex,follow" /> | ||
<meta charset="utf-8" /> | ||
<!-- | ||
An iframe analytics handler hosted on a shared domain: | ||
e.g. https://shared-domain.com/iframe.html?origin=https://example.com | ||
--> | ||
<script> | ||
(() => { | ||
if (self !== parent) { | ||
const VISITOR_KEY = "_vstr"; | ||
|
||
const actions = { | ||
HANDSHAKE_ACTION: "handshake", | ||
GET_VISITOR_ACTION: "getVisitor", | ||
INCREMENT_VIEWS_ACTION: "incrementViews", | ||
BLA_BLA_BLA_ACTION: "bla", | ||
FOO_ACTION: "bar", | ||
}; | ||
|
||
const getCookie = (/** @type {string} */ name) => { | ||
const pair = document.cookie | ||
.split("; ") | ||
.find((cookie) => cookie.startsWith(name + "=")); | ||
|
||
const cookie = (pair || "").split("=")[1]; | ||
if (cookie) { | ||
try { | ||
return JSON.parse(atob(decodeURIComponent(cookie))); | ||
} catch (ex) { | ||
console.error("[IFRAME] Coult not parse cookie:", cookie, ex); | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
const setCookie = ( | ||
/** @type {string} */ name, | ||
/** @type {Object} */ value | ||
) => { | ||
const encoded = encodeURIComponent(btoa(JSON.stringify(value))); | ||
const cookie = [ | ||
`${encodeURIComponent(name)}=${encoded}`, | ||
`expires=${new Date(Date.now() + 86400e3 * 365).toUTCString()}`, | ||
`domain=${document.domain}`, | ||
"path=/; samesite=none; secure", | ||
].join("; "); | ||
document.cookie = cookie; | ||
console.debug("[IFRAME] setCookie:", document.cookie); | ||
}; | ||
|
||
const getVisitor = () => { | ||
let visitor = getCookie(VISITOR_KEY); | ||
if (!visitor) { | ||
visitor = { | ||
id: window.crypto.getRandomValues(new Uint32Array(1))[0], | ||
views: 0, | ||
}; | ||
setCookie(VISITOR_KEY, visitor); | ||
} | ||
return visitor; | ||
}; | ||
|
||
window.addEventListener("load", (event) => { | ||
console.log("[IFRAME] load:event:", event); | ||
const url = new URL(document.referrer || location.href); | ||
const origin = document.referrer | ||
? url.origin | ||
: url.searchParams.get("origin"); | ||
if (origin) { | ||
const action = actions.HANDSHAKE_ACTION; | ||
const source = window.parent; | ||
const visitor = getVisitor(); | ||
source.postMessage({ visitor, action, actions }, origin); | ||
} | ||
}); | ||
|
||
window.addEventListener("message", (event) => { | ||
console.log("[IFRAME] message:event:", event); | ||
if (event.data) { | ||
const action = event.data.action; | ||
const origin = event.origin; | ||
const source = event.source; | ||
const visitor = getVisitor(); | ||
if ( | ||
action === actions.HANDSHAKE_ACTION || | ||
action === actions.GET_VISITOR_ACTION | ||
) { | ||
source.postMessage({ visitor, action, actions }, origin); | ||
} else if (action === actions.INCREMENT_VIEWS_ACTION) { | ||
visitor.views += 1; | ||
setCookie(VISITOR_KEY, visitor); | ||
source.postMessage({ visitor, action, actions }, origin); | ||
} | ||
} | ||
}); | ||
} | ||
})(); | ||
</script> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copy of https://komito.net/demo/iframe.js | ||
|
||
(() => { | ||
const IFRAME_DOMAIN = "komito.net"; | ||
const IFRAME_ORIGIN = `https://${IFRAME_DOMAIN}`; | ||
const IFRAME_SOURCE = `${IFRAME_ORIGIN}/demo/iframe.html`; | ||
const RANDOM_NUMBER = (+new Date()).toString(36).slice(-5); | ||
const IFRAME_ID = `iframe-${RANDOM_NUMBER}`; | ||
|
||
const updateCSP = () => { | ||
let meta = | ||
document.querySelector('meta[http-equiv="Content-Security-Policy"]') || | ||
document.querySelector('meta[http-equiv="content-security-policy"]'); | ||
|
||
if (!meta) { | ||
meta = document.head.appendChild(document.createElement("meta")); | ||
meta.setAttribute("http-equiv", "content-security-policy"); | ||
} | ||
|
||
let content = meta.getAttribute("content") || ""; | ||
// TODO: update the "frame-src" policy directive if exisits: | ||
content = `frame-src 'self' ${IFRAME_ORIGIN}; ${content}`; | ||
meta.setAttribute("content", content); | ||
}; | ||
|
||
const initIframe = () => { | ||
// updateCSP(); | ||
const body = document.body || document.documentElement; | ||
const iframe = body.appendChild(document.createElement("iframe")); | ||
iframe.style.position = "absolute"; | ||
iframe.style.width = "9px"; | ||
iframe.style.height = "9px"; | ||
iframe.style.top = "-9px"; | ||
iframe.style.left = "-9px"; | ||
iframe.style.border = 0; | ||
iframe.sandbox = "allow-scripts allow-same-origin"; | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/csp | ||
// iframe.csp = `frame-src 'self' ${IFRAME_ORIGIN}`; | ||
iframe.id = IFRAME_ID; | ||
iframe.src = `${IFRAME_SOURCE}?origin=${location.origin}&nc=${IFRAME_ID}`; | ||
return iframe; | ||
}; | ||
|
||
window.addEventListener("load", (event) => { | ||
console.log("[PARENT] load:event:", event); | ||
initIframe(); | ||
}); | ||
|
||
window.addEventListener("message", (event) => { | ||
console.log("[PARENT] message:event:", event); | ||
|
||
const actions = event.data && event.data.actions; | ||
const origin = event.origin; | ||
if (actions && origin === IFRAME_ORIGIN) { | ||
const iframe = document.getElementById(IFRAME_ID) || initIframe(); | ||
const action = event.data && event.data.action; | ||
if (action === actions.HANDSHAKE_ACTION) { | ||
console.log("[PARENT] HANDSHAKE_ACTION"); | ||
iframe.contentWindow.postMessage( | ||
{ action: actions.INCREMENT_VIEWS_ACTION }, | ||
IFRAME_ORIGIN | ||
); | ||
} else if (action === actions.INCREMENT_VIEWS_ACTION) { | ||
console.log("[PARENT] INCREMENT_VIEWS_ACTION"); | ||
} else { | ||
console.error("[PARENT] Unknowon action:", action); | ||
} | ||
} | ||
}); | ||
})(); |