Greasy Fork

Greasy Fork is available in English.

chatGPT tools Plus ++

chatGPT in bing &Google!!

目前为 2022-12-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         chatGPT tools Plus ++
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  chatGPT in bing &Google!!
// @author       Onion
// @match        https://cn.bing.com/*
// @match      https://chat.openai.com/chat
// @match      https://www.google.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=openai.com
// @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
// @grant       GM_xmlhttpRequest
// @grant       GM_addStyle

// @license    MIT
// ==/UserScript==

(function() {
    'use strict';
    //重要声明:感谢隔壁作者:zhengbangbo (https://github.com/zhengbangbo/chat-gpt-userscript)的请求处理方式,之前一直不知道怎么处理SSE响应,原来直接就可以用一般的方式来接收,那么对返回数据处理切割就直接借鉴啦!(反正MIT不是嘛)
    //cookie 大家自己想办法弄到,别霍霍我了呜呜
    //对返回结果增加了markdown解析成html
    // 请在上面添加一句:油猴因为策略问题没法增加这个js
    // @require    https://cdn.jsdelivr.net/npm/marked/marked.min.js
    //顶级配置

    //目前谷歌布局支持很烂,隔壁布局真好!
    const your_cookie=`Bearer ` //这里自己去控制台抓一个包选cookie找到__Secure-next-auth.session-token这个cookie加进来,注意和Bearer直接有一个空格
    var your_qus

    if (window.location.href.indexOf("cn.bing.com") > -1) {
        creatBox_and_addEventlis(0)
        GM_add_box_style(0)
    }
    if (window.location.href.indexOf("chat.openai.com") > -1) {
        //$.cookie('yourCookie','dumplings', {domain:'qq.com',path:'/'});
        console.log("httpOnly保护,没法拿到cookie,自己复制粘贴")
    }
    if (window.location.href.indexOf("www.google.com") > -1) {
        creatBox_and_addEventlis(1)
        GM_add_box_style(1)
    }

    //顶级函数
    function uuid() { //uuid 产生
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        var uuid = s.join("");
        return uuid;
    }
    function GM_add_box_style(case_web){
        switch (case_web){
            case 0://bing
                GM_addStyle(`
    #gptAnswer{
   margin: 15px;
   border-top: solid;
    border-bottom: solid;
    }
    #gptInput{
    width:74%;
    border-radius: 4px;
    }
    #button_GPT:hover{
    background:#ffffffcc;
    }
    #gptDiv{
     border-radius: 8px;
    padding: 10px;
    margin-bottom: 9px;
    width:452px;
    translate:-20px;
    background:#ffffffcc;
    backdrop-filter: blur(5px);
    }
    #button_GPT{
    }
    #button_GPT{
    background: transparent;
    translate: 10px 3px;
    border-radius: 3px;
    }
    #gptCueBox{
        translate: 3px;
    }

#dot{
    height: 4px;
    width: 4px;
    display: inline-block;
    border-radius: 2px;
    animation: dotting 2.4s  infinite step-start;
}
@keyframes dotting {
    25%{
        box-shadow: 4px 0 0 #71777D;
    }
    50%{
        box-shadow: 4px 0 0 #71777D ,14px 0 0 #71777D;
    }
    75%{
        box-shadow: 4px 0 0 #71777D ,14px 0 0 #71777D, 24px 0 0 #71777D;
    }
}

    `)
                break;
            case 1: //google
                GM_addStyle(`
    #gptAnswer{
   margin: 15px;
   border-top: solid;
    border-bottom: solid;
        width: 80%;
    }
    #gptInput{
    width:89%;
    border-radius: 4px;
    }
    #button_GPT:hover{
    background:#dcdcdccc;
    }
    #gptDiv{
     border-radius: 8px;
    padding: 10px;
    margin-bottom: 9px;
    list-style-type: none;
    background:#ffffffcc;
    /*backdrop-filter: blur(5px);*/
    margin-bottom: 30px;
    border-radius: 8px;
    border: 1px solid #dadce0;
    padding: 7px;
    translate: 0px 6px;
    }
    #button_GPT{

    }
    #button_GPT{
    background: transparent;
    translate: 19px 3px;
    border-radius: 3px;
     font-size: 14px;
    }
    #gptCueBox{
        translate: -1px;
            width: 108px;
    }

#dot{
    height: 4px;
    width: 4px;
    display: inline-block;
    border-radius: 2px;
    animation: dotting 2.4s  infinite step-start;
}
.GyAeWb{
flex-wrap:unset !important;
}
@keyframes dotting {
    25%{
        box-shadow: 4px 0 0 #71777D;
    }
    50%{
        box-shadow: 4px 0 0 #71777D ,14px 0 0 #71777D;
    }
    75%{
        box-shadow: 4px 0 0 #71777D ,14px 0 0 #71777D, 24px 0 0 #71777D;
    }
}

    `)
                break;
            default :
                alert("参数没设定")
        }

    }
    function do_it(){
        if(!your_cookie){
            document.getElementById('gptAnswer').innerHTML=`<div>你的cookie好像不见了!<a src="https://chat.openai.com/chat">点我去登录获取</a></div>`

        }
        else{
            document.getElementById('gptAnswer').innerHTML=`<div>加载中<span id="dot"></span></div>`

        GM_xmlhttpRequest({
            method: "POST",
            url: "https://chat.openai.com/backend-api/conversation",
            headers: {
                "Content-Type": "application/json",
                Authorization: `${your_cookie}`,
            },
            data: JSON.stringify({//抓包conversation就可以看到这个结构
                action: "next",
                messages: [
                    {
                        id: uuid(),
                        role: "user",
                        content: {
                            content_type: "text",
                            parts: [your_qus],
                        },
                    },
                ],
                model: "text-davinci-002-render",
                parent_message_id: uuid(),
            }),

            onloadend: function (data) {
                if (data.response) {
                    const answer = JSON.parse(data.response.split("\n\n").slice(-3, -2)[0].slice(6)).message.content.parts[0]
                    document.getElementById('gptAnswer').innerHTML=marked.parse(answer)//markdown 解析HTML
                    /*   for(let i=0;i<=marked.parse(answer).length-1;i++){
                        document.getElementById('gptAnswer').innerHTML+=marked.parse(answer)[i]
              }*/
                }
            },

            onerror: function (err) {
                document.getElementById('gptAnswer').innerHTML=`<div>some err happends,errinfo :<br>${err}</div>`
            },
            ontimeout: function (err) {
                document.getElementById('gptAnswer').innerHTML=`<div>Opps!TimeOut,Please try again,errinfo:<br>${err}</div>`
            }
        })

        }
    }
    function creatBox_and_addEventlis(append_case){
        var divE = document.createElement('li');
        var divId = document.createAttribute("id"); //创建属性
        divId.value = 'gptDiv'; //设置属性值
        divE.setAttributeNode(divId); //给div添加属性
        var pE = document.createElement('p');
        var pClass= document.createAttribute('class');
        pClass.value = 'textClass';
        pE.setAttributeNode(pClass)
        var pText = document.createTextNode("chatGPT tools Plus ++ v0.0.1已启动");
        pE.appendChild(pText);
        divE.appendChild(pE);



        switch (append_case){
            case 0:
                document.getElementById('b_context').prepend(divE)
                break;
            case 1:
                document.getElementById("rcnt").appendChild(divE);
                break;
            default :
                document.getElementById('b_context').prepend(divE)
        }
        document.getElementById('gptDiv').innerHTML=`
<div>
<input id="gptInput" type=text><button id="button_GPT" >chat一下</button>
</div>
<div id=gptCueBox>
<p>&nbsp openAI 已就绪,请输入你的问题</p>
<div id="gptAnswer">chatGPT tools Plus ++ v0.0.1已启动</div>
</div>

`
        document.getElementById('button_GPT').addEventListener('click',()=>{
            your_qus=document.getElementById("gptInput").value
            do_it()

        })
    }



})();