diff --git a/src/api.ts b/src/api.ts index e23b550..435b320 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,6 @@ import urlcat from 'urlcat' import { apiUrl, baseUrl } from './constants' -import { getChatIdFromUrl, getConversationFromSharePage, isSharePage } from './page' +import { getChatIdFromUrl, getConversationFromSharePage, getPageAccessToken, isSharePage } from './page' import { blobToDataURL } from './utils/dom' import { memorize } from './utils/memorize' @@ -450,6 +450,9 @@ async function _fetchSession(): Promise { const fetchSession = memorize(_fetchSession) async function getAccessToken(): Promise { + const pageAccessToken = getPageAccessToken() + if (pageAccessToken) return pageAccessToken + const session = await fetchSession() return session.accessToken } diff --git a/src/page.ts b/src/page.ts index 8dff3ad..d6900e4 100644 --- a/src/page.ts +++ b/src/page.ts @@ -39,6 +39,34 @@ declare global { } } } + __remixContext: { + basename: string + future: {} + state: { + loaderData: { + root: { + clientBootstrap: { + accountStatus: null + session: { + accessToken: string + authProvider: string + expires: string + user: { + email: string + group: unknown[] + id: string + image: string + intercom_hash: string + mfa: boolean + name: string + picture: string + } + } + } + } + } + } + } } } @@ -46,8 +74,12 @@ export function getHistoryDisabled(): boolean { return localStorage.getItem(KEY_OAI_HISTORY_DISABLED) === '"true"' } +export function getPageAccessToken(): string | null { + return unsafeWindow?.__remixContext?.state?.loaderData?.root?.clientBootstrap?.session?.accessToken ?? null +} + function getUserProfile() { - const user = unsafeWindow?.__NEXT_DATA__?.props?.pageProps?.user + const user = unsafeWindow?.__NEXT_DATA__?.props?.pageProps?.user ?? unsafeWindow?.__remixContext?.state?.loaderData?.root?.clientBootstrap?.session?.user if (!user) throw new Error('No user found.') return user }