Gemini AI Stream Parser

A reusable function to parse streaming JSON responses from the Google Gemini API.

当前为 2025-06-06 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/538628/1602991/Gemini%20AI%20Stream%20Parser.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

async function parseGeminiStream(reader, onChunk) { //Processes the Gemini API stream
  for (let buffer = '', partialMarkdown = '';;) { //Loop indefinitely to process the stream
    const { value, done } = await reader.read(); //Read the next chunk from the stream
    if (done) break; //Exit the loop if the stream is finished
    buffer += new TextDecoder().decode(value, { stream: true }); //Decode and append the chunk to the buffer
    for (var startIdx = 0, openBrace, balance, closeBrace, item, inString; (openBrace = buffer.indexOf('{', startIdx)) !== -1;) { //Find the start of a JSON object
      for (balance = 1, closeBrace = openBrace + 1, inString = false; balance && closeBrace < buffer.length; closeBrace++) { //Find the corresponding end of the JSON object
        if (buffer[closeBrace] === '"' && buffer[closeBrace - 1] !== '\\') inString = !inString; //Handle strings that contain braces
        if (!inString) balance += (buffer[closeBrace] === '{') - (buffer[closeBrace] === '}'); //Adjust brace balance
      }
      if (balance) break; //If the object is incomplete, wait for more chunks
      item = JSON.parse(buffer.substring(openBrace, closeBrace)); //Parse the complete JSON object
      partialMarkdown += item.candidates?.[0]?.content?.parts?.[0]?.text || ''; //Append the text from the current chunk
      onChunk(item, partialMarkdown); //Execute the callback with the parsed data and accumulated text
      startIdx = closeBrace; //Move the starting position for the next search
    }
    buffer = buffer.substring(startIdx); //Discard the processed part of the buffer
  }
}