|
| 1 | +const form = document.getElementById('github_form'); |
| 2 | +const user_name_input = document.getElementById('user_name_input'); |
| 3 | +const name = document.getElementById('user_name'); |
| 4 | +const followers = document.getElementById('followers'); |
| 5 | +const profile_picture = document.getElementById('profile_picture'); |
| 6 | + |
| 7 | +let copyText; |
| 8 | + |
| 9 | +form.addEventListener('submit', (e) => { |
| 10 | + e.preventDefault(); |
| 11 | + |
| 12 | + const user_name = user_name_input.value.trim(); |
| 13 | + if (!user_name) return; |
| 14 | + |
| 15 | + const requestUrl = `https://api.github.com/users/${user_name}`; |
| 16 | + |
| 17 | + const xhr = new XMLHttpRequest(); |
| 18 | + xhr.open('GET', requestUrl); |
| 19 | + |
| 20 | + xhr.onreadystatechange = function () { |
| 21 | + if (xhr.readyState === 4 && xhr.status === 200) { |
| 22 | + const data = JSON.parse(this.responseText); |
| 23 | + name.innerHTML = data.name || "No Name"; |
| 24 | + profile_picture.src = data.avatar_url; |
| 25 | + followers.innerHTML = `followers: ${data.followers}`; |
| 26 | + |
| 27 | + copyText = function () { |
| 28 | + const text = `{ name : '${data.name}'; avatar_url : '${data.avatar_url}'; followers : ${data.followers}; }`; |
| 29 | + navigator.clipboard.writeText(text) |
| 30 | + .then(() => alert("Copied!")) |
| 31 | + .catch(err => console.error("Failed to copy: ", err)); |
| 32 | + }; |
| 33 | + } else if (xhr.readyState === 4 && xhr.status !== 200) { |
| 34 | + name.innerHTML = "User not found"; |
| 35 | + profile_picture.src = "profile_image.jpg"; |
| 36 | + followers.innerHTML = ""; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + xhr.send(); |
| 41 | +}); |
0 commit comments