Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/md_v2/assets/cloud-strip.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* The Crawl4AI Cloud strip: click events and the anonymous id on its links.
Events go to the gate's beacon sink as one small POST per click (a click usually leaves the page, so
nothing is batched); DNT and GPC turn the events off, never the strip. */
/* The Crawl4AI Cloud strip: impression and click events and the anonymous id on its links.
Events go to the gate's beacon sink as one small POST per event (a click usually leaves the page, so
nothing is batched); DNT and GPC turn off events and anonymous id storage, never the strip. */
(function () {
"use strict";
/* the theme loads this file in <head>: wait for the markup when the page is still parsing */
Expand All @@ -10,9 +10,11 @@
var html = document.documentElement, SINK = "https://gate.crawl4ai.com/v1/t";

/* ---- the anonymous id (the same shape as the landing's beacon) and the event sender ---- */
var quiet = navigator.doNotTrack == "1" || navigator.doNotTrack == "yes" || window.globalPrivacyControl;
var quiet = navigator.doNotTrack == "1" || navigator.doNotTrack == "yes" || navigator.globalPrivacyControl === true || window.globalPrivacyControl === true;
var anon = "";
try { anon = localStorage.getItem("c4a") || ""; if (!anon) { anon = Array.from(crypto.getRandomValues(new Uint8Array(8))).map(function (b) { return b.toString(16).padStart(2, "0"); }).join(""); localStorage.setItem("c4a", anon); } } catch (e) {}
if (!quiet) {
try { anon = localStorage.getItem("c4a") || ""; if (!anon) { anon = Array.from(crypto.getRandomValues(new Uint8Array(8))).map(function (b) { return b.toString(16).padStart(2, "0"); }).join(""); localStorage.setItem("c4a", anon); } } catch (e) {}
}
function ev(name, props) {
if (quiet) return;
props = props || {}; props.pg = "docs"; props.path = location.pathname; props.ref = root.getAttribute("data-ref");
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/test_cloud_strip_privacy.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Offline regression harness for the complete documentation script.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const vm = require('node:vm');
const fs = require('node:fs');
const source = fs.readFileSync(require('node:path').resolve(__dirname, '../../docs/md_v2/assets/cloud-strip.js'), 'utf8');

function run({ gpc, legacyGpc, dnt, stored = '', beacon = true, denyStorage = false, loading = false } = {}) {
let reads = 0, writes = 0, randomCalls = 0;
const sent = [], handlers = {}, classes = new Set();
const attributes = { 'data-c4s-home': 'docs' };
const link = { href: 'https://crawl4ai.com/?ref=strip' };
const home = {
href: 'https://crawl4ai.com/', pathname: '/',
getAttribute: key => attributes[key],
setAttribute: (key, value) => { attributes[key] = value; }
};
const html = { getAttribute: () => 'full', classList: { add: s => classes.add(s), remove: s => classes.delete(s) } };
const context = {
document: {
readyState: loading ? 'loading' : 'complete',
documentElement: html,
getElementById: id => id === 'c4s' ? { getAttribute: () => 'test' } : null,
querySelectorAll: () => [link],
addEventListener: (name, handler) => { handlers[name] = handler; }
},
window: { globalPrivacyControl: legacyGpc, scrollY: 50 },
navigator: { globalPrivacyControl: gpc, doNotTrack: dnt,
...(beacon ? { sendBeacon: (url, body) => { sent.push({url, body}); return true; } } : {}) },
localStorage: {
getItem: () => { reads++; if (denyStorage) throw Error('blocked'); return stored; },
setItem: () => { writes++; if (denyStorage) throw Error('blocked'); }
},
crypto: { getRandomValues: arr => { randomCalls++; return arr.fill(42); } },
Blob, location: {pathname: '/docs/'},
fetch: (url, options) => sent.push({url, body: options.body}),
addEventListener: () => {}, requestAnimationFrame: fn => fn()
};
vm.runInNewContext(source, context);
if (loading) handlers.DOMContentLoaded();
handlers.click({target: {closest: () => ({getAttribute: () => 'cta_clicked'})}});
handlers.mousedown({target: {closest: () => home}});
return {reads, writes, randomCalls, sent, link: link.href, home: home.href, shrunk: classes.has('c4s-shrunk')};
}

for (const [name, options] of [
['standard GPC', {gpc: true}],
['GPC with existing id', {gpc: true, stored: 'old-id'}],
['legacy window GPC', {legacyGpc: true}],
['DNT 1', {dnt: '1'}],
['DNT yes', {dnt: 'yes', loading: true}],
['GPC without sendBeacon', {gpc: true, beacon: false}]
]) {
test(`${name}: no tracking, storage or id in links`, () => {
const r = run(options);
assert.equal(r.sent.length, 0);
assert.deepEqual([r.reads, r.writes, r.randomCalls], [0,0,0]);
assert.equal(r.link, 'https://crawl4ai.com/?ref=strip');
assert.equal(r.home, 'https://crawl4ai.com/?ref=docs');
assert.equal(r.shrunk, true);
});
}
for (const [name, options, expected] of [
['no opt-out', {}, [1,1,1]],
['GPC false', {gpc:false}, [1,1,1]],
['existing id', {stored:'old-id'}, [1,0,0]],
['fetch fallback', {beacon:false}, [1,1,1]],
['storage denied', {denyStorage:true}, [1,0,0]]
]) {
test(`${name}: retains existing behavior`, () => {
const r = run(options);
assert.equal(r.sent.length, 3);
assert.deepEqual([r.reads,r.writes,r.randomCalls],expected);
assert.equal(r.link.includes('&aid='), !options.denyStorage);
assert.equal(r.home.includes('&aid='), !options.denyStorage);
assert.equal(r.shrunk,true);
});
}
18 changes: 18 additions & 0 deletions tests/unit/test_cloud_strip_privacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Run the documentation strip's offline JavaScript regression tests."""

import shutil
import subprocess
from pathlib import Path

import pytest


@pytest.mark.skipif(shutil.which("node") is None, reason="Node.js is required")
def test_cloud_strip_privacy():
result = subprocess.run(
["node", "--test", str(Path(__file__).with_suffix(".cjs"))],
capture_output=True,
text=True,
timeout=30,
)
assert result.returncode == 0, result.stdout + result.stderr