-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-share-api.html
88 lines (84 loc) · 2.57 KB
/
web-share-api.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Web Share API Demo</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
text-align: center;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Web Share API Demo</h1>
<button id="shareText">Share Text</button>
<button id="shareURL">Share URL</button>
<button id="shareFile">Share File</button>
<script>
document
.getElementById("shareText")
.addEventListener("click", async () => {
if (navigator.share) {
try {
await navigator.share({
title: "Example Text",
text: "Hello World! Sharing this text from the Web Share API.",
});
console.log("Text shared successfully");
} catch (err) {
console.error("Error sharing text:", err);
}
} else {
alert("Web Share API is not supported in this browser.");
}
});
document
.getElementById("shareURL")
.addEventListener("click", async () => {
if (navigator.share) {
try {
await navigator.share({
title: "Web Patterns",
url: "https://achingachris.github.io/web-patterns/",
});
console.log("URL shared successfully");
} catch (err) {
console.error("Error sharing URL:", err);
}
} else {
alert("Web Share API is not supported in this browser.");
}
});
document
.getElementById("shareFile")
.addEventListener("click", async () => {
const file = new File(["Hello World! You just made this file"], "file.txt", {
type: "text/plain",
});
if (navigator.canShare && navigator.canShare({ files: [file] })) {
try {
await navigator.share({
files: [file],
title: "Example File",
text: "Check out this file!",
});
console.log("File shared successfully");
} catch (err) {
console.error("Error sharing file:", err);
}
} else {
alert("File sharing not supported or file not shareable.");
}
});
</script>
</body>
</html>