Greasy Fork镜像 还支持 简体中文。

Notion auto copy text on select

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

  1. // ==UserScript==
  2. // @name Notion auto copy text on select
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 在 Notion 中选中文本后自动复制到剪贴板
  6. // @author You
  7. // @match https://*.notion.so/*
  8. // @grant GM_setClipboard
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. document.addEventListener('mouseup', function(event) {
  16. // 确保是鼠标左键
  17. if (event.button === 0) {
  18. // 添加小延迟确保选择已完成稳定
  19. setTimeout(function() {
  20. const selectedText = window.getSelection().toString().trim();
  21. if (selectedText) {
  22. copyTextToClipboard(selectedText);
  23. }
  24. }, 10); // 10毫秒延迟,通常足够但不影响体验
  25. }
  26. });
  27.  
  28. function copyTextToClipboard(text) {
  29. // 方法1:使用Clipboard API(现代浏览器)
  30. if (navigator.clipboard && navigator.clipboard.writeText) {
  31. navigator.clipboard.writeText(text)
  32. .then(() => {
  33. showNotification('已复制');
  34. console.log('文本已复制到剪贴板 (Clipboard API)');
  35. })
  36. .catch(err => {
  37. console.error('复制失败 (Clipboard API): ', err);
  38. fallbackCopy(text); // 尝试回退方法
  39. });
  40. } else {
  41. // 方法2:传统方法
  42. fallbackCopy(text);
  43. }
  44. }
  45.  
  46. function fallbackCopy(text) {
  47. const textarea = document.createElement('textarea');
  48. textarea.value = text;
  49. textarea.style.position = 'fixed';
  50. textarea.style.opacity = '0';
  51. textarea.style.left = '0';
  52. textarea.style.top = '0';
  53. document.body.appendChild(textarea);
  54.  
  55. try {
  56. textarea.focus();
  57. textarea.select();
  58.  
  59. const successful = document.execCommand('copy');
  60. if (successful) {
  61. showNotification('已复制');
  62. console.log('文本已复制到剪贴板 (execCommand)');
  63. } else {
  64. console.error('复制失败 (execCommand)');
  65. showNotification('复制失败', true);
  66. }
  67. } catch (err) {
  68. console.error('复制失败: ', err);
  69. showNotification('复制失败', true);
  70. }
  71.  
  72. document.body.removeChild(textarea);
  73. }
  74.  
  75. function showNotification(message, isError = false) {
  76. const notificationDiv = document.createElement('div');
  77. notificationDiv.textContent = message;
  78. notificationDiv.style.position = 'fixed';
  79. notificationDiv.style.bottom = '20px';
  80. notificationDiv.style.right = '20px';
  81. notificationDiv.style.padding = '10px';
  82. notificationDiv.style.backgroundColor = isError ? 'rgba(220, 53, 69, 0.8)' : 'rgba(0, 0, 0, 0.7)';
  83. notificationDiv.style.color = 'white';
  84. notificationDiv.style.borderRadius = '5px';
  85. notificationDiv.style.zIndex = '9999';
  86. document.body.appendChild(notificationDiv);
  87.  
  88. setTimeout(() => {
  89. document.body.removeChild(notificationDiv);
  90. }, 2000);
  91. }
  92. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址