Copy it

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

目前為 2023-05-30 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Copy it
// @name:zh-CN   便捷复制
// @namespace    https://github.com/xianghongai/Tampermonkey-UserScript
// @version      1.1.0
// @description  Hold Alt click on text, Copy as plain text. Alt + Shift click on text, Copy as kebab-case.
// @description:zh-CN   按住 Alt 键点击文本,复制为纯文本。Alt + Shift 复制为 kebab-case 风格字符。
// @author       Nicholas Hsiang
// @icon         https://xinlu.ink/favicon.ico
// @match        http*://*/*
// @grant        GM_setClipboard
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  document.addEventListener('click', listener, false);

  function listener(event) {
    if (event.altKey) {
      event.preventDefault();
      event.stopPropagation();
      const text = getText(event.target);
      if (event.shiftKey) {
        return copyTextToClipboard(toKebab(text));
      }
      return copyTextToClipboard(text);
    }
  }

  const VALUE_CONTROLLER = ['INPUT', 'TEXTAREA'];

  function getText(el) {
    let text = '';
    if (VALUE_CONTROLLER.includes(el.tagName)) {
      const value = `${el.value}`.trim();
      const placeholder = el.placeholder ?? '';
      text = value === '' ? placeholder : value;
    } else if (el.tagName === 'SELECT') {
      const selectedOption = el.options[el.selectedIndex];
      text = selectedOption.text;
    } else {
      text = el.innerText;
    }
    return text.trim();
  }

  function toKebab(input) {
    if (typeof input === 'string') {
      return input
        .replace(/[\W\s]/gi, '-')
        .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
        .replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
        .toLowerCase();
    }
  }

  function copyTextToClipboard(text) {
    GM_setClipboard(text, 'text');
  }
})();