summaryrefslogtreecommitdiff
path: root/source/github-helpers/get-user-avatar.ts
blob: 510066e289792fbd2cb2a1101d9594d4c398dab9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

export default function getUserAvatar(username: string, size: number): string | void {
	const cleanName = username.replace('[bot]', '');

	// Find image on page. Saves a request and a redirect + add support for bots
	const existingAvatar = select(`img[alt="@${cleanName}"]`);
	if (existingAvatar) {
		return existingAvatar.src;
	}

	// If it's not a bot, use a shortcut URL #2125
	if (cleanName === username) {
		const url = pageDetect.isEnterprise()
			? `/${username}.png`
			: `https://avatars.githubusercontent.com/${username}`;
		// Why use a 2x size: https://github.com/refined-github/refined-github/pull/4973#discussion_r735133613
		return url + `?size=${size * 2}`;
	}
}