Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.8 — More Browser APIs
JavaScript · Example 3 of 3

A copy button that always ends well

Press Copy. In Hectal's sandboxed preview the clipboard isn't allowed, so you'll see the fallback: the code gets selected and the message explains what to do. On a normal HTTPS page the same button copies.

Powerful browser features such as the clipboard, the user's location and system notifications are guarded by permissions: they need a secure (HTTPS) page, often a user action, and the user's consent, so good code detects support, asks at the right moment and has a fallback when the answer is no.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write supports() that returns an object saying whether clipboard, geolocation, notifications and share are available, then log it.

02

Write copyText(text) that resolves to "copied" or "manual" (when the Clipboard API is missing or rejects), never throwing.

Explain it without notes

01

What layers of protection do browsers put around powerful APIs?

02

How do you use the Geolocation API with async/await, and what are its error codes?

03

What is the right way to ask for notification permission?

04

What can and can't the Permissions API do?

A copy button that always ends well

Preview

Press Run to see the page.

Console

Nothing logged yet.