Greasy Fork

Greasy Fork is available in English.

飞书小报童生财有术文档标题复制助手Plus(可复制文字)

让文档更好用:添加标题和时间、解除复制限制、支持右键菜单

当前为 2025-02-21 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         飞书小报童生财有术文档标题复制助手Plus(可复制文字)
// @namespace    http://tampermonkey.net/
// @version      3.11
// @description  让文档更好用:添加标题和时间、解除复制限制、支持右键菜单
// @author       微信11208596
// @match        *://*.feishu.cn/*
// @match        *://*.larkoffice.com/*
// @match        *://scys.com/*
// @match        *://xiaobot.net/*
// @grant        none
// @run-at       document-start
// @license      UNLICENSED
// ==/UserScript==

// 版权所有 © 2025 微信11208596
// 本脚本仅供个人使用,禁止修改、复制或分发。

(function() {
    'use strict';

    const a = () => {
        const b = EventTarget.prototype.addEventListener;
        EventTarget.prototype.addEventListener = function (c, d, e) {
            if (c === 'copy') {
                b.call(this, c, event => {
                    event.stopImmediatePropagation();
                    return null;
                }, e);
                return;
            }
            if (c === 'contextmenu') {
                b.call(this, c, event => {
                    event.stopImmediatePropagation();
                    return d(event);
                }, e);
                return;
            }
            b.call(this, c, d, e);
        };
    };

    const x = () => {
        const y = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function (m, n, ...o) {
            this.addEventListener('readystatechange', function () {
                if (this.readyState === 4 && n.includes('space/api/suite/permission/document/actions/state/')) {
                    let p = this.responseText;
                    try {
                        p = JSON.parse(p);
                        if (p.data && p.data.actions && p.data.actions.copy !== 1) {
                            p.data.actions.copy = 1;
                            Object.defineProperty(this, 'responseText', { value: JSON.stringify(p) });
                            Object.defineProperty(this, 'response', { value: p });
                        }
                    } catch (e) {
                        console.log('修改响应失败:', e);
                    }
                }
            }, false);
            y.call(this, m, n, ...o);
        };
    };

    function getTime() {
        const now = new Date();
        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0');
        const day = String(now.getDate()).padStart(2, '0');
        const hours = String(now.getHours()).padStart(2, '0');
        const minutes = String(now.getMinutes()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}`;
    }

    function processLink(link) {
        return link.split('?')[0];
    }

    function formatText(title, link) {
        return `📄 文件「${title}」\n🔗 链接:\n${processLink(link)}\n🕐 时间「${getTime()}」\n💡 文件已分享,欢迎查阅,有任何问题都可以随时交流~`;
    }

    const enableCopyFunctionality = () => {
        const style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = `
            .toast-wrap {
                display: none !important;
            }
        `;
        document.head.appendChild(style);

        document.addEventListener('contextmenu', function(e) {
            e.stopPropagation();
        }, true);

        document.addEventListener('selectstart', function(e) {
            e.stopPropagation();
        }, true);

        document.addEventListener('copy', function(e) {
            e.stopPropagation();
        }, true);

        document.addEventListener('contextmenu', function(event) {
            event.preventDefault();
            const existingMenu = document.getElementById('custom-context-menu');
            if (existingMenu) {
                existingMenu.remove();
            }

            const menu = document.createElement('div');
            menu.id = 'custom-context-menu';
            menu.style.position = 'fixed';
            menu.style.top = event.clientY + 'px';
            menu.style.left = event.clientX + 'px';
            menu.style.backgroundColor = '#fff';
            menu.style.border = '1px solid #ccc';
            menu.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.2)';
            menu.style.zIndex = '10000';
            menu.style.padding = '5px';
            menu.style.cursor = 'pointer';

            const copyOption = document.createElement('div');
            copyOption.innerText = '复制';
            copyOption.style.padding = '5px';
            copyOption.style.whiteSpace = 'nowrap';

            copyOption.addEventListener('click', function() {
                document.execCommand('copy');
                menu.remove();
            });

            menu.appendChild(copyOption);
            document.body.appendChild(menu);

            document.addEventListener('click', function onClickOutside() {
                menu.remove();
                document.removeEventListener('click', onClickOutside);
            });
        });

        document.addEventListener('copy', function(event) {
            const selection = document.getSelection();
            event.clipboardData.setData('text/plain', selection.toString());
            event.preventDefault();
        });
    };

    document.addEventListener('DOMContentLoaded', () => {
        document.addEventListener('copy', function(e) {
            try {
                const text = window.getSelection().toString();
                if (!text || !text.includes('feishu.cn/')) return;
                
                const title = document.title.split(' - ')[0].trim();
                if (!title || text.includes(title)) return;
                
                e.preventDefault();
                e.clipboardData.setData('text/plain', formatText(title, text));
                
                console.log('已添加标题和时间到链接');
            } catch (err) {
                console.log('复制处理出错:', err);
            }
        });

        document.addEventListener('click', function(e) {
            const target = e.target;
            if (!target) return;
            
            if (target.textContent?.includes('复制链接') || 
                target.closest('.lark-link-entry-v2__copylink') ||
                target.closest('[data-test-id="copy-share-link"]')) {
                
                setTimeout(function() {
                    try {
                        const title = document.title.split(' - ')[0].trim();
                        if (!title) return;
                        
                        navigator.clipboard.readText().then(function(text) {
                            if (!text || !text.includes('feishu.cn/') || text.includes(title)) return;
                            
                            navigator.clipboard.writeText(formatText(title, text)).catch(function(err) {
                                console.log('写入剪贴板失败:', err);
                            });
                        }).catch(function(err) {
                            console.log('读取剪贴板失败:', err);
                        });
                    } catch (err) {
                        console.log('处理复制按钮点击失败:', err);
                    }
                }, 100);
            }
        });

        enableCopyFunctionality();
    });

    overrideEventListeners();
    overrideXHR();

    console.log('飞书文档标题复制助手Plus已加载,版本3.1');
})();