Greasy Fork

Greasy Fork is available in English.

飞书文档标题复制助手

复制飞书文档链接时自动添加文档标题,制作人微信11208596

当前为 2025-01-17 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         飞书文档标题复制助手
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  复制飞书文档链接时自动添加文档标题,制作人微信11208596
// @author       微信11208596
// @match        *://*.feishu.cn/*
// @grant        none
// @run-at       document-end
// @license      UNLICENSED
// ==/UserScript==

(function(){
    const $={
        h: document.createElement.bind(document),
        q: document.querySelector.bind(document),
        c: (t)=>document.execCommand(t),
        g: ()=>window.getSelection().toString(),
        t: ()=>document.title.split(' - ')[0].trim()
    };

    const _i=()=>{
        const t=$.h('textarea');
        t.style.cssText='position:fixed;top:-999px;left:-999px;';
        document.body.appendChild(t);
        return t;
    };

    const _c=(t)=>{
        const i=_i();
        i.value=t;
        i.select();
        $.c('copy');
        i.remove();
    };

    document.addEventListener('click',async(e)=>{
        const b=e.target.closest('.lark-link-entry-v2__copylink, [data-test-id="copy-share-link"]');
        if(!b)return;

        await new Promise(r=>setTimeout(r,100));

        try{
            let l='';
            const i=$.q('[data-test-id="share-link-input"]');
            l=i&&i.value?i.value:await navigator.clipboard.readText();

            if(l.includes('feishu.cn/')){
                const t=$.t();
                if(!l.includes(t)){
                    _c(`${t} ${l}`);
                }
            }
        }catch(e){
            console.error('复制失败:',e);
        }
    });

    document.addEventListener('copy',(e)=>{
        const s=$.g();
        if(s.includes('feishu.cn/')){
            e.preventDefault();
            const t=$.t();
            if(!s.includes(t)){
                e.clipboardData.setData('text/plain',`${t} ${s}`);
            }else{
                e.clipboardData.setData('text/plain',s);
            }
        }
    });
})();