From b11e5ce6b70b56e21a2c652cc8688bdd6eae9364 Mon Sep 17 00:00:00 2001 From: josc146 Date: Thu, 20 Apr 2023 23:21:38 +0800 Subject: [PATCH] fix: refresh token after opening the official website to avoid expiration of authorization (#239) --- src/content-script/index.jsx | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/content-script/index.jsx b/src/content-script/index.jsx index 7f1b6dc..96ab31f 100644 --- a/src/content-script/index.jsx +++ b/src/content-script/index.jsx @@ -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) }