Chat GPT 粘贴格式问题解决

在复制时清理特定的HTML样式并支持多种粘贴目标

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
// ==UserScript==
// @name         Chat GPT 粘贴格式问题解决
// @namespace    http://tampermonkey.org/
// @version      0.2
// @description  在复制时清理特定的HTML样式并支持多种粘贴目标
// @author       smartblack
// @match        https://chatgpt.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('copy', function(e) {
        let selection = window.getSelection();
        if (!selection.rangeCount) return;

        let container = document.createElement('div');
        container.style.position = 'fixed';
        container.style.left = '-99999px';
        container.style.top = '0';
        container.style.opacity = '0';
        document.body.appendChild(container);

        container.appendChild(selection.getRangeAt(0).cloneContents());

        // 清理HTML元素的样式
        cleanStyles(container);

        try {
            // 为Word等富文本编辑器准备HTML格式
            e.clipboardData.setData('text/html', container.innerHTML);
            // 为文本编辑器和网页表单准备纯文本格式
            e.clipboardData.setData('text/plain', container.textContent);
            e.preventDefault();  // 防止默认的复制行为
        } catch (err) {
            console.error('Failed to set clipboard data:', err);
        }

        document.body.removeChild(container);
    });

    function cleanStyles(element) {
        element.querySelectorAll('*').forEach(el => {
            el.removeAttribute('style');  // 移除所有样式
        });
    }
})();