Created
October 17, 2025 09:27
-
-
Save Be1zebub/2e58d70c28839966489dabecaffff34a to your computer and use it in GitHub Desktop.
DOMParser based avatarURL scrapper, no api creds needed - designed for clientside apps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export async function fetchSteamAvatar(steamID64, preferredSize = "full") { | |
| try { | |
| const endpoint = `https://steamcommunity.com/profiles/${steamID64}/?xml=1` | |
| const response = await fetch(endpoint) | |
| if (!response.ok) { | |
| console.error("Error fetching avatar:", response.statusText) | |
| return null | |
| } | |
| const text = await response.text() | |
| const parser = new DOMParser() | |
| const doc = parser.parseFromString(text, "application/xml") | |
| if (doc.querySelector("parsererror")) { | |
| console.error("Error parsing avatar:", text) | |
| return null | |
| } | |
| const nodes = { | |
| full: doc.querySelector("avatarFull"), | |
| medium: doc.querySelector("avatarMedium"), | |
| icon: doc.querySelector("avatarIcon"), | |
| } | |
| const avatars = { | |
| full: nodes.full ? nodes.full?.textContent.trim() : null, | |
| medium: nodes.medium ? nodes.medium?.textContent.trim() : null, | |
| icon: nodes.icon ? nodes.icon?.textContent.trim() : null, | |
| } | |
| let avatarURL = avatars[preferredSize] | |
| if (avatarURL) return avatarURL | |
| return avatarURL.full || avatarURL.medium || avatarURL.icon | |
| } catch (err) { | |
| console.error("Error fetching avatar:", err) | |
| return null | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment