ChatGPT 代码预览助手 - Code Previewer for ChatGPT

Preview HTML code on chatgpt.com by adding an iframe below code blocks with class 'language-html'.

目前為 2024-07-15 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT 代码预览助手 - Code Previewer for ChatGPT
// @namespace    http://xyde.net.cn
// @version      1.2
// @description  Preview HTML code on chatgpt.com by adding an iframe below code blocks with class 'language-html'.
// @author       You
// @match        https://chatgpt.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create preview container
    function createPreviewContainer(htmlCode) {
        // Create container div
        const container = document.createElement('div');
        container.style.border = '1px solid #ddd';
        container.style.borderRadius = '5px';
        container.style.marginTop = '10px';
        container.style.padding = '10px';
        container.style.backgroundColor = '#f9f9f9';
        container.style.boxShadow = '0 2px 4px rgba(0,0,0,0.1)';

        // Create title
        const title = document.createElement('div');
        title.textContent = 'HTML Code Preview 网页代码预览';
        title.style.fontWeight = 'bold';
        title.style.marginBottom = '10px';
        title.style.textAlign = 'center';

        // Create iframe
        const iframe = document.createElement('iframe');
        iframe.className = 'html-preview-iframe';
        iframe.style.width = '100%';
        iframe.style.height = '300px';
        iframe.style.border = 'none';

        // Wait for iframe to load before setting content
        iframe.onload = function() {
            const doc = iframe.contentDocument || iframe.contentWindow.document;
            doc.open();
            doc.write(htmlCode);
            doc.close();
        };

        // Append title and iframe to container
        container.appendChild(title);
        container.appendChild(iframe);

        return container;
    }

    // Function to find and process code elements
    function processCodeElements() {
        // Find all code elements with class 'language-html'
        const codeElements = document.querySelectorAll('code.language-html');

        // Iterate through each code element
        codeElements.forEach(codeElement => {
            // Check if it has already been processed
            if (codeElement.dataset.previewed) return;

            // Mark as previewed
            codeElement.dataset.previewed = true;

            // Get parent element of code block
            const parentElement = codeElement.closest('.flex-col');

            // Create a preview container
            const previewContainer = createPreviewContainer(codeElement.textContent);

            // Insert container below the code element
            parentElement.appendChild(previewContainer);
        });
    }

    // Run processCodeElements initially and then every 3 seconds
    processCodeElements();
    setInterval(processCodeElements, 3000);
})();