GPT3(intumu.com)

持续对话文字加载!

当前为 2023-05-22 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GPT3(intumu.com)
// @namespace    http://tampermonkey.net/
// @version      0.0.1
// @description  持续对话文字加载!
// @author       yeayee
// @match        https://*.intumu.com/chatgpt
// @icon        https://intumu.com/static/images/favicon.ico
// @grant       GM_xmlhttpRequest
// @grant       GM_addStyle
// @grant       GM_openInTab
// @grant      GM_registerMenuCommand
// @grant      GM_setValue
// @grant      GM_getValue
// @run-at     document-end
// @require    https://cdn.staticfile.org/jquery/3.4.0/jquery.min.js
// @require    https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js
// @require    https://unpkg.com/axios/dist/axios.min.js
// @connect    intumu.com
// @connect    anfans.cn
// @license    MIT
// @require    https://intumu.com/static/js/marked.min.js
// @require    https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_HTML
// @require    https://cdn.staticfile.org/clipboard.js/2.0.4/clipboard.min.js
// @require    https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/highlight.min.js
// @require    https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/languages/go.min.js


// ==/UserScript==

(function () {
  'use strict';

  //封装GM_xmlhttpRequest ---start---
  async function GM_fetch(details) {
    return new Promise((resolve, reject) => {
      switch (details.responseType) {
        case "stream":
          details.onloadstart = (res) => {
            resolve(res)
          }
          break;
        default:
          details.onload = (res) => {
            resolve(res)
          };
      }

      details.onerror = (res) => {
        reject(res)
      };
      details.ontimeout = (res) => {
        reject(res)
      };
      details.onabort = (res) => {
        reject(res)
      };
      GM_xmlhttpRequest(details)
    });
  }

  function GM_httpRequest(details, callBack, errorCallback, timeoutCallback, abortCallback) {
    if (callBack) {
      switch (details.responseType) {
        case "stream":
          details.onloadstart = callBack;
          break;
        default:
          details.onload = callBack
      }
    }
    if (errorCallback) {
      details.onerror = errorCallback;
    }
    if (timeoutCallback) {
      details.ontimeout = timeoutCallback;
    }
    if (abortCallback) {
      details.onabort = abortCallback;
    }
    console.log(details)
    GM_xmlhttpRequest(details);
  };

  //封装GM_xmlhttpRequest ---end---

  // 通过class定位button
  const button = document.getElementById('send-btn');


  // 创建新的button元素
  const newButton = document.createElement('button');
  newButton.setAttribute('id', 'newbutton');
  newButton.innerHTML = 'ChatGPT3';

  // 将新的button插入到button后面
  button.parentNode.insertBefore(newButton, button.nextSibling);
  const newbutton = document.getElementById('newbutton');
  newbutton.setAttribute("class", "btn btn-danger margin");
  document.getElementById('newbutton').addEventListener('click', () => {

      let text = '';
      text = document.getElementById("textarea").value;
      if (text == "") {
        alert("请输入内容");
        return;
      };
      let html = '';
      let send_time = new Date();
      let send_time_str = '';
      if (send_time.getTime() - last_time > 1000 * 60 * 5) {
        // 以'%Y年%#m月%#d日 %H:%M'格式显示时间
        html += '<div class="item item-center"><span>' + get_time_str(send_time) + '</span></div>';
        last_time = send_time.getTime();
        send_time_str = get_time_str(send_time);
      }
      html += '<div class="item item-right"><div class="bubble bubble-right markdown">' + marked.marked(text) + '</div><div class="avatar"><img src="./static/people.jpg" /></div></div>';
      $(".content").append(html);
      $("#textarea").val("");
      $(".content").scrollTop($(".content")[0].scrollHeight);
      if (text.startsWith('new:')) send_time_str = get_time_str(send_time)
      LEMURCHAT(text);

    });
    
  // 请求接口
  function LEMURCHAT(your_qus) {

    let baseURL = "http://lemurchat.anfans.cn/";
    let chat_item = $('<div class="item item-left"><div class="avatar"><img src="./static/chatgpt.png" /></div><div class="bubble bubble-left markdown">正在等待回复</div></div>')
    $(".content").append(chat_item);

    GM_fetch({
      method: "POST",
      url: baseURL + "api/chat/conversation-trial",
      headers: {
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Linux; Android 9; Redmi 4 Prime) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Mobile Safari/537.36"
      },
      data: `{"messages":"[{\\"content\\":\\"\\",\\"id\\":\\"LEMUR_AI_SYSTEM_SETTING\\",\\"isSensitive\\":false,\\"needCheck\\":false,\\"role\\":\\"system\\"},{\\"content\\":\\"${your_qus}\\",\\"isSensitive\\":false,\\"needCheck\\":true,\\"role\\":\\"user\\"}]"}`,
      responseType: "stream"
    }).then((stream) => {
      const reader = stream.response.getReader();
      let result = [];
      reader.read().then(function processText({done, value}) {
        if (done) {
            return;
        }
        try {
          let d = new TextDecoder("utf8").decode(new Uint8Array(value));
          console.log("raw:", d)
          let dd = d.replace(/data: /g, "").split("\n\n")
          console.log("dd:", dd)
          dd.forEach(item => {
            try {
              let delta = /content\\":\\"(.*?)\\"/gi.exec(item)[1]
              result.push(delta.replace(/\\\\n/g, "\n"))
              let response = result.join("");
              console.log("response:", response)
              let div = document.createElement('div');
              div.innerHTML = marked.marked(response);
              MathJax.Hub.Typeset(div);
              chat_item.find(".bubble").empty();
              chat_item.find(".bubble").append(div);          
            } catch (e) {
            }
          })

        } catch (e) {
          console.log(e)
        }

        return reader.read().then(processText);
      });
    }, function (err) {
      console.log(err)
    }).catch((ex) => {
      console.log(ex)
    });

  };




})();