MCPcopy Index your code
hub / github.com/ChatGPTBox-dev/chatGPTBox / overwriteAccessToken

Function overwriteAccessToken

src/content-script/index.jsx:584–657  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

582}
583
584async function overwriteAccessToken() {
585 console.debug('[content] overwriteAccessToken called for hostname:', location.hostname)
586 try {
587 const isKimiHost =
588 location.hostname === 'kimi.moonshot.cn' ||
589 location.hostname === 'kimi.com' ||
590 location.hostname === 'www.kimi.com'
591 if (isKimiHost) {
592 console.log(`[content] On ${location.hostname}, attempting to save refresh token.`)
593 const refreshToken = window.localStorage.refresh_token
594 if (refreshToken) {
595 await setUserConfig({ kimiMoonShotRefreshToken: refreshToken })
596 console.log('[content] Kimi Moonshot refresh token saved.')
597 } else {
598 const config = await getUserConfig()
599 if (config.kimiMoonShotRefreshToken) {
600 await setUserConfig({ kimiMoonShotRefreshToken: '' })
601 console.log('[content] Kimi Moonshot refresh token cleared.')
602 }
603 console.warn('[content] Kimi Moonshot refresh token not found in localStorage.')
604 }
605 return
606 }
607
608 if (location.hostname !== 'chatgpt.com') {
609 console.debug('[content] Not on chatgpt.com, skipping access token overwrite.')
610 return
611 }
612
613 console.log('[content] On chatgpt.com, attempting to overwrite access token.')
614 let data
615 if (location.pathname === '/api/auth/session') {
616 console.debug('[content] On /api/auth/session page.')
617 const preElement = document.querySelector('pre')
618 if (preElement?.textContent) {
619 const response = preElement.textContent
620 try {
621 data = JSON.parse(response)
622 console.debug('[content] Parsed access token data from <pre> tag.')
623 } catch (error) {
624 console.error('[content] Failed to parse JSON from <pre> tag for access token:', error)
625 }
626 } else {
627 console.warn(
628 '[content] <pre> tag not found or empty for access token on /api/auth/session.',
629 )
630 }
631 } else {
632 console.debug('[content] Not on /api/auth/session page, fetching token from API endpoint.')
633 try {
634 const resp = await fetch('https://chatgpt.com/api/auth/session')
635 if (resp.ok) {
636 data = await resp.json()
637 console.debug('[content] Fetched access token data from API endpoint.')
638 } else {
639 console.warn(
640 `[content] Failed to fetch access token, status: ${resp.status} ${resp.statusText}`,
641 )

Callers 1

runFunction · 0.85

Calls 3

setUserConfigFunction · 0.90
getUserConfigFunction · 0.90
setAccessTokenFunction · 0.90

Tested by

no test coverage detected