🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2025/12/30 06:03:47
🔄 下次更新时间:2025/12/30 07:03:47
手动刷新缓存

Gemini AI Stream Parser

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

ეს სკრიპტი არ უნდა იყოს პირდაპირ დაინსტალირებული. ეს ბიბლიოთეკაა, სხვა სკრიპტებისთვის უნდა ჩართეთ მეტა-დირექტივაში // @require https://update.greasyfork.org/scripts/538628/1613479/Gemini%20AI%20Stream%20Parser.js.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

window.parseGeminiStream = async function(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 binary chunk to string and append to 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; //Toggle string state on unescaped quotes to handle code blocks with {} symbols in JSON
        if (!inString) balance += (buffer[closeBrace] === '{') - (buffer[closeBrace] === '}'); //Count braces only outside strings to avoid breaking on code symbols like {}
      }
      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
  }
}