ChatGPT output HTML direct

ChatGPT直接輸出HTML

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ChatGPT output HTML direct
// @namespace    http://tampermonkey.net/
// @version      0.8
// @description  ChatGPT直接輸出HTML
// @description:en  ChatGPT output HTML direct
// @author       cat-no-war
// @match        https://chat.openai.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=openai.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    setInterval(function(){
        var codes= document.querySelectorAll("code:not([add_frame])")
        for(let i=0;i<codes.length;i++){
            let code=codes[i];
            //wait completed
            let pre=code.closest("pre");
            if(pre==null){
                 code.setAttribute("add_frame",true);
                continue;
            }
            //if(codeParentIndex==codeParentLength-1)continue;
            code.setAttribute("add_frame",true);
            let lang=code.parentElement.parentElement.children[0].children[0].innerText;
            let oldText;
            if(lang=="html" || lang=="php"){
                let newFrame=document.createElement("iframe");
                newFrame.style.width="100%";
                newFrame.style.height="512px";
                oldText=code.innerHTML;
                newFrame.src = "data:text/html;charset=utf-8," + encodeURIComponent(code.innerText);
                setInterval(()=>{
                    if(oldText!=code.innerHTML){
                        newFrame.src="";
                        newFrame.src = "data:text/html;charset=utf-8," + encodeURIComponent(code.innerText);
                        //newFrame.contentWindow.location.reload(true);
                    }
                    oldText=code.innerHTML
                },500);
                code.parentElement.parentElement.parentElement.appendChild(newFrame);
            }else if(lang="javascript"){
                let button = document.createElement("button");
                button.innerHTML="Play";
                oldText=code.innerText;
                var r=`eval(\`${code.innerText}\`);`
                button.setAttribute("onclick",r)
                eval(code.innerText);
                setInterval(()=>{
                    if(oldText!=code.innerText){
                        r=`eval(\`${code.innerText}\`);`
                        button.setAttribute("onclick",r)
                          eval(code.innerText);
                    }
                    oldText=code.innerText
                },500);
                code.parentElement.parentElement.parentElement.appendChild(button);
            }
        }
    }, 500);
})();