fix: refresh token after opening the official website to avoid expiration of authorization (#239)

This commit is contained in:
josc146
2023-04-20 23:21:38 +08:00
parent 7aa2dc8cfe
commit b11e5ce6b7
+12 -9
View File
@@ -100,18 +100,21 @@ async function getInput(inputQuery) {
}
async function overwriteAccessToken() {
if (location.hostname !== 'chat.openai.com' || location.pathname !== '/api/auth/session') return
const response = document.querySelector('pre').textContent
if (location.hostname !== 'chat.openai.com') return
let data
try {
data = JSON.parse(response)
} catch (error) {
console.error('json error', error)
return
if (location.pathname === '/api/auth/session') {
const response = document.querySelector('pre').textContent
try {
data = JSON.parse(response)
} catch (error) {
console.error('json error', error)
}
} else {
const resp = await fetch('https://chat.openai.com/api/auth/session')
data = await resp.json().catch(() => ({}))
}
if (data.accessToken) {
if (data && data.accessToken) {
await setAccessToken(data.accessToken)
console.log(data.accessToken)
}