Skip to content

Instantly share code, notes, and snippets.

@Be1zebub
Created October 17, 2025 09:27
Show Gist options
  • Select an option

  • Save Be1zebub/2e58d70c28839966489dabecaffff34a to your computer and use it in GitHub Desktop.

Select an option

Save Be1zebub/2e58d70c28839966489dabecaffff34a to your computer and use it in GitHub Desktop.
DOMParser based avatarURL scrapper, no api creds needed - designed for clientside apps.
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