Copy it

Hold Alt click on text, Copy as plain text. Alt + Shift click on text, Copy as kebab-case.

当前为 2023-05-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Copy it
  3. // @name:zh-CN 便捷复制
  4. // @namespace https://github.com/xianghongai/Tampermonkey-UserScript
  5. // @version 1.1.0
  6. // @description Hold Alt click on text, Copy as plain text. Alt + Shift click on text, Copy as kebab-case.
  7. // @description:zh-CN 按住 Alt 键点击文本,复制为纯文本。Alt + Shift 复制为 kebab-case 风格字符。
  8. // @author Nicholas Hsiang
  9. // @icon https://xinlu.ink/favicon.ico
  10. // @match http*://*/*
  11. // @grant GM_setClipboard
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. document.addEventListener('click', listener, false);
  19.  
  20. function listener(event) {
  21. if (event.altKey) {
  22. event.preventDefault();
  23. event.stopPropagation();
  24. const text = getText(event.target);
  25. if (event.shiftKey) {
  26. return copyTextToClipboard(toKebab(text));
  27. }
  28. return copyTextToClipboard(text);
  29. }
  30. }
  31.  
  32. const VALUE_CONTROLLER = ['INPUT', 'TEXTAREA'];
  33.  
  34. function getText(el) {
  35. let text = '';
  36. if (VALUE_CONTROLLER.includes(el.tagName)) {
  37. const value = `${el.value}`.trim();
  38. const placeholder = el.placeholder ?? '';
  39. text = value === '' ? placeholder : value;
  40. } else if (el.tagName === 'SELECT') {
  41. const selectedOption = el.options[el.selectedIndex];
  42. text = selectedOption.text;
  43. } else {
  44. text = el.innerText;
  45. }
  46. return text.trim();
  47. }
  48.  
  49. function toKebab(input) {
  50. if (typeof input === 'string') {
  51. return input
  52. .replace(/[\W\s]/gi, '-')
  53. .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
  54. .replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
  55. .toLowerCase();
  56. }
  57. }
  58.  
  59. function copyTextToClipboard(text) {
  60. GM_setClipboard(text, 'text');
  61. }
  62. })();