(username: string, size: number)
| 2 | import {$optional} from 'select-dom'; |
| 3 | |
| 4 | export default function getUserAvatar(username: string, size: number): string | void { |
| 5 | let cleanName = username.replace('[bot]', ''); |
| 6 | |
| 7 | if (/[^\w-]/.test(cleanName)) { |
| 8 | throw new TypeError(`Expected a username, got ${cleanName}`); |
| 9 | } |
| 10 | |
| 11 | // Find image on page. Saves a request and a redirect + add support for bots |
| 12 | const existingAvatar = $optional(`[href="/${cleanName}" i] img`); |
| 13 | if (existingAvatar) { |
| 14 | return existingAvatar.src; |
| 15 | } |
| 16 | |
| 17 | if ( |
| 18 | [ |
| 19 | 'Copilot', |
| 20 | 'copilot-coding-agent-docs', |
| 21 | 'copilot-swe-agent', |
| 22 | ].includes(cleanName) |
| 23 | ) { |
| 24 | cleanName = 'in/1143301'; |
| 25 | } |
| 26 | |
| 27 | // Bots don't have a /$username.png URL |
| 28 | // Enterprise can only use /$username.png |
| 29 | const isBot = username.endsWith('[bot]') || cleanName.includes('/'); |
| 30 | const url = pageDetect.isEnterprise() || !isBot |
| 31 | // Use full URLs: https://github.com/refined-github/refined-github/issues/9571 |
| 32 | ? `${location.origin}/${cleanName}.png` |
| 33 | : `https://avatars.githubusercontent.com/${cleanName}`; |
| 34 | // Why use a 2x size: https://github.com/refined-github/refined-github/pull/4973#discussion_r735133613 |
| 35 | return url + `?size=${size * 2}`; |
| 36 | } |
no test coverage detected