-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
136 lines (119 loc) · 3.79 KB
/
basic.js
File metadata and controls
136 lines (119 loc) · 3.79 KB
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
134
135
136
// change theme
$(document).ready(function () {
localStorage.setItem("theme", "light");
$("#themedark").addClass("d-none");
$("html").attr("data-bs-theme", "light");
});
function handleClick() {
if (localStorage.getItem("theme") !== "light") {
$("#themedark").addClass("d-none");
$("#themelight").removeClass("d-none");
$("html").attr("data-bs-theme", "light");
localStorage.setItem("theme", "light");
} else {
$("#themelight").addClass("d-none");
$("#themedark").removeClass("d-none");
$("html").attr("data-bs-theme", "dark");
localStorage.setItem("theme", "dark");
}
}
// Read more handlers (show more/less content)
function handleReadMoreClick() {
if (
document.getElementById("collapsemorehttp").classList.contains("d-none")
) {
document.getElementById("collapsemorehttp").classList.remove("d-none");
} else {
document.getElementById("collapsemorehttp").classList.add("d-none");
}
}
// feedback form event handlers
function handleFeedback() {
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const phone = document.getElementById("phone").value;
const data = `Hi, ${name}.<br>Your Email is ${email} , \
<br>and phone is ${phone}. <br>Would you like to proceed and submit the message?`;
document.getElementById("feedback_info").innerHTML = data;
}
function handleConfirm() {
const success = "Your message has been sent successfully!";
alert(success);
}
// ajax: fetch gavatar image using a get request
async function handleAvatar() {
const email = document.getElementById("email").value;
if (email === "") {
return;
}
// create gavatar url
const gavartar_url = await getGavatarUrl(email);
// fetch gavatar image
try {
const gavatar_data = await fetchGavatar(gavartar_url);
if (gavatar_data instanceof Blob) {
const blobUrl = URL.createObjectURL(gavatar_data);
// display gavatar image
document.getElementById("avatarImg").src = blobUrl;
} else {
console.error("Invalid gravatar image data:", gavatar_data);
}
} catch (error) {
console.error("Error fetching gravatar image:", error);
}
}
async function getGavatarUrl(email) {
const address = email.trim().toLowerCase();
const data = new TextEncoder().encode(address);
try {
const hashBuffer = await window.crypto.subtle.digest("SHA-256", data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
return `https://www.gravatar.com/avatar/${hashHex}?d=identicon`;
} catch (error) {
console.error(error);
}
}
async function fetchGavatar(url) {
try {
const response = await fetch(url);
return await response.blob();
} catch (error) {
console.error("Error fetching gravatar image:", error);
}
}
// email input to lower case
function toLowerCase() {
const emailInput = document.getElementById("email");
emailInput.value = emailInput.value.toLowerCase();
}
// ajax: fecth message from api
const message_api =
"https://fakerapi.it/api/v1/texts?_quantity=1&_characters=100";
async function fetchMessage() {
console.log("fetching message...");
try {
const response = await fetch(message_api);
const info = await response.json();
const message = info.data[0].content;
return message;
} catch (error) {
console.error("Error fetching message:", error);
}
}
// generate message
async function handleMessage() {
const message = await fetchMessage();
console.log("??message:", message);
document.getElementById("GenerateMessage").value = message;
}
// click to github repo
function handlePjClick(id) {
let url = "";
if (id === 1) {
url = "https://github.com/Hayeensss/fpl_analysis_2223";
}
window.open(url, "_blank");
}