-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (33 loc) · 1.49 KB
/
script.js
File metadata and controls
41 lines (33 loc) · 1.49 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
const form = document.getElementById('github_form');
const user_name_input = document.getElementById('user_name_input');
const name = document.getElementById('user_name');
const followers = document.getElementById('followers');
const profile_picture = document.getElementById('profile_picture');
let copyText;
form.addEventListener('submit', (e) => {
e.preventDefault();
const user_name = user_name_input.value.trim();
if (!user_name) return;
const requestUrl = `https://api.github.com/users/${user_name}`;
const xhr = new XMLHttpRequest();
xhr.open('GET', requestUrl);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
const data = JSON.parse(this.responseText);
name.innerHTML = data.name || "No Name";
profile_picture.src = data.avatar_url;
followers.innerHTML = `followers: ${data.followers}`;
copyText = function () {
const text = `{ name : '${data.name}'; avatar_url : '${data.avatar_url}'; followers : ${data.followers}; }`;
navigator.clipboard.writeText(text)
.then(() => alert("Copied!"))
.catch(err => console.error("Failed to copy: ", err));
};
} else if (xhr.readyState === 4 && xhr.status !== 200) {
name.innerHTML = "User not found";
profile_picture.src = "profile_image.jpg";
followers.innerHTML = "";
}
};
xhr.send();
});