Notion auto copy text on select

在 Notion 中选中文本后自动复制到剪贴板

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Notion auto copy text on select
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  在 Notion 中选中文本后自动复制到剪贴板
// @author       You
// @match        https://*.notion.so/*
// @grant        GM_setClipboard
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('mouseup', function(event) {
        // 确保是鼠标左键
        if (event.button === 0) {
            // 添加小延迟确保选择已完成稳定
            setTimeout(function() {
                const selectedText = window.getSelection().toString().trim();
                if (selectedText) {
                    copyTextToClipboard(selectedText);
                }
            }, 10); // 10毫秒延迟,通常足够但不影响体验
        }
    });

    function copyTextToClipboard(text) {
        // 方法1:使用Clipboard API(现代浏览器)
        if (navigator.clipboard && navigator.clipboard.writeText) {
            navigator.clipboard.writeText(text)
                .then(() => {
                    showNotification('已复制');
                    console.log('文本已复制到剪贴板 (Clipboard API)');
                })
                .catch(err => {
                    console.error('复制失败 (Clipboard API): ', err);
                    fallbackCopy(text); // 尝试回退方法
                });
        } else {
            // 方法2:传统方法
            fallbackCopy(text);
        }
    }

    function fallbackCopy(text) {
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.style.position = 'fixed';
        textarea.style.opacity = '0';
        textarea.style.left = '0';
        textarea.style.top = '0';
        document.body.appendChild(textarea);

        try {
            textarea.focus();
            textarea.select();

            const successful = document.execCommand('copy');
            if (successful) {
                showNotification('已复制');
                console.log('文本已复制到剪贴板 (execCommand)');
            } else {
                console.error('复制失败 (execCommand)');
                showNotification('复制失败', true);
            }
        } catch (err) {
            console.error('复制失败: ', err);
            showNotification('复制失败', true);
        }

        document.body.removeChild(textarea);
    }

    function showNotification(message, isError = false) {
        const notificationDiv = document.createElement('div');
        notificationDiv.textContent = message;
        notificationDiv.style.position = 'fixed';
        notificationDiv.style.bottom = '20px';
        notificationDiv.style.right = '20px';
        notificationDiv.style.padding = '10px';
        notificationDiv.style.backgroundColor = isError ? 'rgba(220, 53, 69, 0.8)' : 'rgba(0, 0, 0, 0.7)';
        notificationDiv.style.color = 'white';
        notificationDiv.style.borderRadius = '5px';
        notificationDiv.style.zIndex = '9999';
        document.body.appendChild(notificationDiv);

        setTimeout(() => {
            document.body.removeChild(notificationDiv);
        }, 2000);
    }
})();