Notion auto copy text on select

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

目前為 2025-05-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
    }
})();