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.1
// @description  让飞书文档更好用:添加标题和时间、解除复制限制、支持右键菜单
// @author       微信11208596
// @match        *://*.feishu.cn/*
// @match        *://*.larkoffice.com/*
// @grant        none
// @run-at       document-start
// @license      UNLICENSED
// ==/UserScript==

(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💡 文件已分享,欢迎查阅,有任何问题都可以随时交流~`;
    }

    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);
            }
        });
    });

    a();
    x();

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