Skip to content

Commit

Permalink
fix: error handling for avatar fetching
Browse files Browse the repository at this point in the history
closes #73
  • Loading branch information
pionxzh committed Mar 15, 2023
1 parent 796899e commit d8af0ec
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/userscript/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { unsafeWindow } from 'vite-plugin-monkey/dist/client'
import { getBase64FromImageUrl } from './utils/dom'
import { getBase64FromImageUrl, getBase64FromImg } from './utils/dom'

declare global {
interface Window {
Expand Down Expand Up @@ -55,13 +55,23 @@ export function getConversationChoice() {

const defaultAvatar = 'data:image/svg+xml,%3csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20version=%271.1%27%20width=%2730%27%20height=%2730%27/%3e'
export async function getUserAvatar(): Promise<string> {
// const avatars = Array.from(document.querySelectorAll<HTMLImageElement>('img[alt]:not([aria-hidden])'))
// // starts with data: means it's not (lazy)loaded yet
// const avatar = avatars.find(avatar => !avatar.src.startsWith('data:'))
// if (avatar) return getBase64FromImg(avatar)
try {
const { picture } = getUserProfile()
if (picture) return await getBase64FromImageUrl(picture)
}
catch (e) {
console.error(e)
}

const { picture } = getUserProfile()
if (picture) return getBase64FromImageUrl(picture)
try {
const avatars = Array.from(document.querySelectorAll<HTMLImageElement>('img[alt]:not([aria-hidden])'))
// starts with data: means it's not (lazy)loaded yet
const avatar = avatars.find(avatar => !avatar.src.startsWith('data:'))
if (avatar) return getBase64FromImg(avatar)
}
catch (e) {
console.error(e)
}

return defaultAvatar
}
Expand Down

0 comments on commit d8af0ec

Please sign in to comment.