-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (112 loc) · 3.28 KB
/
script.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const clickAdd = () => {
const params = new URLSearchParams(window.location.search);
const queries = Object.fromEntries(params.entries());
if ("history" in queries) {
window.location.href = `?history=${parseInt(queries.history) + 1}`;
} else {
window.location.href = "?history=1";
}
};
const clickShowAlert = () => {
window.alert("Alert Test");
};
const clickShowConfirm = () => {
const result = window.confirm("Confirm Test");
window.alert(`Result: ${result ? "OK" : "Cancel"}`);
};
const clickShowPrompt = () => {
const result = window.prompt("Prompt Test", "Placeholder");
window.alert(`Result: ${result}`);
};
const clickCheckUserAgent = () => {
const userAgent = window.navigator.userAgent;
window.alert(userAgent);
};
const clickCheckCookies = () => {
const cookies = document.cookie
.split("; ")
.map((item) => item.replace("=", " = "))
.join("\n");
window.alert(cookies);
};
const clickCustomScheme = (element) => {
window.location = element.innerText;
};
const selectFile = (event) => {
const file = event.target.files[0];
const blobURL = URL.createObjectURL(file);
const img = document.createElement("img");
img.onload = function () {
window.alert(`width: ${img.width}\nheight: ${img.height}`);
};
img.src = blobURL;
};
const errorHandler = (error) => {
window.alert(`Error(${error.name}): ${error.message}`);
};
const clickSavePhotoLibrary = () => {
if (typeof window.navigator.share !== "function") {
window.alert("Error: navigator.share() is not implemented.");
return;
}
const img = document.getElementById("sample");
const canvas = document.createElement("canvas");
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const context = canvas.getContext("2d");
context.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
const file = new File([blob], "sample.png");
const data = {
files: [file],
};
window.navigator.share(data).catch(errorHandler);
}, "image/png");
};
const clickDeviceLocation = () => {
const successHandler = (position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
window.alert(`Latitude: ${latitude}\nLongitude: ${longitude}`);
};
const options = {
enableHighAccuracy: true,
timeout: 50000,
maximumAge: 0,
};
window.navigator.geolocation.getCurrentPosition(successHandler, errorHandler, options);
};
const clickCameraMicrophone = () => {
const video = document.querySelector("video");
const constraints = {
audio: true,
video: {
width: 400,
height: 300,
facingMode: {
exact: "environment",
},
},
};
window.navigator.mediaDevices
.getUserMedia(constraints)
.then((mediaStream) => {
video.srcObject = mediaStream;
})
.catch(errorHandler);
video.play();
};
(() => {
window.onload = () => {
const perfEntries = window.performance.getEntriesByType("navigation");
if (perfEntries[0].type === "reload") {
history.replaceState(null, "", "?history=0");
return;
}
const params = new URLSearchParams(window.location.search);
const queries = Object.fromEntries(params.entries());
if ("history" in queries) {
document.getElementById("history").innerText = queries.history;
}
};
})();