From ada5dbf5bf286fdf2397bf2c7f2e953a90c4ad30 Mon Sep 17 00:00:00 2001 From: josc146 Date: Sat, 27 May 2023 12:14:20 +0800 Subject: [PATCH] fix: sse json error --- .../site-adapters/youtube/index.mjs | 2 +- src/utils/fetch-sse.mjs | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/content-script/site-adapters/youtube/index.mjs b/src/content-script/site-adapters/youtube/index.mjs index 3029ac1..09ac827 100644 --- a/src/content-script/site-adapters/youtube/index.mjs +++ b/src/content-script/site-adapters/youtube/index.mjs @@ -54,7 +54,7 @@ export default { return cropText( `Provide a structured summary of the following video in markdown format, focusing on key takeaways and crucial information, and ensuring to include the video title. The summary should be easy to read and concise, yet comprehensive.` + - `The video title is "${title}". The subtitle content is as follows:\n${subtitleContent}`, + `The video title is "${title}". The subtitle content is as follows:\n${subtitleContent}`, ) } catch (e) { console.log(e) diff --git a/src/utils/fetch-sse.mjs b/src/utils/fetch-sse.mjs index a212859..7194bbb 100644 --- a/src/utils/fetch-sse.mjs +++ b/src/utils/fetch-sse.mjs @@ -22,13 +22,19 @@ export async function fetchSSE(resource, options) { if (!str.startsWith('{') && !str.startsWith('"{')) { parser.feed(str) } else { - const formattedStr = - 'data: ' + - JSON.stringify( - JSON.parse(str.replace(/^"|"$/g, '').replaceAll('\\"', '"').replaceAll('\\\\u', '\\u')), - ).replaceAll('\\\\n', '\\n') + - '\n\ndata: [DONE]\n\n' - parser.feed(formattedStr) + try { + const formattedData = JSON.parse( + str + .replace(/^"|"$/g, '') + .replaceAll('\\"', '"') + .replaceAll('\\\\u', '\\u') + .replaceAll('\\\\n', '\\n'), + ) + const formattedStr = 'data: ' + JSON.stringify(formattedData) + '\n\ndata: [DONE]\n\n' + parser.feed(formattedStr) + } catch (error) { + console.debug('json error', error) + } } if (!hasStarted) {