aboutsummaryrefslogtreecommitdiff
path: root/test-suite/challenges/10-user-activation.html
blob: 1a12e39d41d01de010fdfb2c3862e0a8283d9b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!doctype html>
<meta charset="utf-8">
<title>10 user activation gates</title>
<link rel="stylesheet" href="../_style.css">
<script src="../_lib.js"></script>
<body>
<main>
  <p>Goal: click the button. Handler then tries gated APIs (clipboard.writeText, fullscreen).
     Success requires <code>navigator.userActivation.isActive</code> and at least one gated API to succeed.</p>
  <button id="go" style="padding:20px 32px;font-size:18px;background:#1f7a1f;color:#fff;border:0;border-radius:6px">Click me</button>
</main>
<script>
Challenge.init({ id: "user-activation", instructions: "click button; user-activation must unlock gated APIs" });
document.getElementById("go").addEventListener("click", async () => {
  // Capture activation state synchronously before any await (activation can be consumed by gated APIs).
  const ua = navigator.userActivation;
  const wasActive = !!ua?.isActive;
  const hadBeenActive = !!ua?.hasBeenActive;
  Challenge.log("activation", { isActive: wasActive, hasBeenActive: hadBeenActive });
  let clip = "skip", fs = "skip";
  try { await navigator.clipboard.writeText("pi-chrome-test"); clip = "ok"; }
  catch (e) { clip = "err:" + e.name; }
  try { await document.documentElement.requestFullscreen(); fs = "ok"; document.exitFullscreen?.(); }
  catch (e) { fs = "err:" + e.name; }
  const bad = [];
  if (!wasActive && !hadBeenActive) bad.push("userActivation.isActive/hasBeenActive both false (synthetic click)");
  if (clip.startsWith("err")) bad.push("clipboard.writeText " + clip);
  if (fs.startsWith("err")) bad.push("requestFullscreen " + fs);
  Challenge.log("gates", { clip, fs });
  if (bad.length) Challenge.fail(...bad);
  else Challenge.pass(`activation active; clipboard=${clip}; fullscreen=${fs}`);
});
</script>
</body>