-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
64 lines (60 loc) · 1.96 KB
/
index.html
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Fetch Data on Button Click</title>
</head>
<body>
<p id="statusText"></p>
<button id="copyButton">Async Copy</button>
<button id="safariCopyButton">Safari Async Copy</button>
<script>
// URL of the JSONPlaceholder API endpoint for fetching a single post
const apiUrl = 'https://jsonplaceholder.typicode.com/posts/1'
const statusText = document.getElementById('statusText')
const loadingTextStatus = () => {
statusText.textContent = 'Loading...'
}
const failedTextStatus = (errorText) => {
statusText.textContent = errorText || 'Failed to copy text to clipboard'
}
const succeededTextStatus = () => {
statusText.textContent = 'Text copied to clipboard successfully'
}
document.getElementById('copyButton').addEventListener('click', () => {
loadingTextStatus()
setTimeout(() => {
fetch(apiUrl)
.then((response) => {
navigator.clipboard
.writeText('Copied Text')
.then(succeededTextStatus)
.catch((e) => {
failedTextStatus(e)
})
})
.catch(failedTextStatus)
}, 1000)
})
document
.getElementById('safariCopyButton')
.addEventListener('click', () => {
loadingTextStatus()
const clipboardItem = new ClipboardItem({
'text/plain': fetch(apiUrl)
.then(() => {
return new Blob(['Safari Copied Text'])
})
.catch(failedTextStatus),
})
navigator.clipboard
.write([clipboardItem])
.then(succeededTextStatus)
.catch((e) => {
failedTextStatus(e)
})
})
</script>
</body>
</html>