Hold Ctrl + Alt or Alt click on text, copy it

Hold Ctrl + Alt or Alt click on text, Copy as plain text.

当前为 2022-07-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Hold Ctrl + Alt or Alt click on text, copy it
  3. // @name:zh-CN 按住 Ctrl + Alt 或 Alt 键点击文本,复制
  4. // @namespace https://github.com/xianghongai/Tampermonkey-UserScript
  5. // @version 1.0.1
  6. // @description Hold Ctrl + Alt or Alt click on text, Copy as plain text.
  7. // @description:zh-CN 按住 Ctrl + Alt 或 Alt 键点击文本,复制为纯文本。
  8. // @author Nicholas Hsiang
  9. // @icon https://xinlu.ink/favicon.ico
  10. // @match http*://*/*
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. function wrapperMsg(input) {
  19. const prefix = `__Tampermonkey® (Hold Ctrl + Alt or Alt click on text, copy it)__: `;
  20. return `${prefix}${input}`;
  21. }
  22.  
  23. function fallbackCopyTextToClipboard(text) {
  24. let textArea = document.createElement('textarea');
  25. textArea.value = text;
  26.  
  27. // Avoid scrolling to bottom
  28. textArea.style.top = '0';
  29. textArea.style.left = '0';
  30. textArea.style.position = 'fixed';
  31.  
  32. document.body.appendChild(textArea);
  33. textArea.focus();
  34. textArea.select();
  35.  
  36. try {
  37. var successful = document.execCommand('copy');
  38. var msg = successful ? 'successful' : 'unsuccessful';
  39. console.log(wrapperMsg('Copying text command was ' + msg));
  40. } catch (err) {
  41. console.error(wrapperMsg('Oops, unable to copy'), err);
  42. }
  43.  
  44. document.body.removeChild(textArea);
  45. }
  46.  
  47. function copyTextToClipboard(text) {
  48. if (!navigator.clipboard) {
  49. fallbackCopyTextToClipboard(text);
  50. return;
  51. }
  52. navigator.clipboard.writeText(text).then(
  53. function () {
  54. console.log(wrapperMsg('Copying to clipboard was successful!'));
  55. },
  56. function (err) {
  57. console.error(wrapperMsg('Could not copy text: '), err);
  58. }
  59. );
  60. }
  61.  
  62. function listener(event) {
  63. if ((event.ctrlKey && event.altKey) || event.altKey) {
  64. event.preventDefault();
  65. event.stopPropagation();
  66. const text = event.target.innerText;
  67. copyTextToClipboard(text);
  68. return false;
  69. }
  70. }
  71.  
  72. document.addEventListener('click', listener, true);
  73. })();