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 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hold Ctrl + Alt or Alt click on text, copy it
// @name:zh-CN   按住 Ctrl + Alt 或 Alt 键点击文本,复制
// @namespace    https://github.com/xianghongai/Tampermonkey-UserScript
// @version      1.0.1
// @description  Hold Ctrl + Alt or Alt click on text, Copy as plain text.
// @description:zh-CN   按住 Ctrl + Alt 或 Alt 键点击文本,复制为纯文本。
// @author       Nicholas Hsiang
// @icon         https://xinlu.ink/favicon.ico
// @match        http*://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  function wrapperMsg(input) {
    const prefix = `__Tampermonkey® (Hold Ctrl + Alt or Alt click on text, copy it)__: `;
    return `${prefix}${input}`;
  }

  function fallbackCopyTextToClipboard(text) {
    let textArea = document.createElement('textarea');
    textArea.value = text;

    // Avoid scrolling to bottom
    textArea.style.top = '0';
    textArea.style.left = '0';
    textArea.style.position = 'fixed';

    document.body.appendChild(textArea);
    textArea.focus();
    textArea.select();

    try {
      var successful = document.execCommand('copy');
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log(wrapperMsg('Copying text command was ' + msg));
    } catch (err) {
      console.error(wrapperMsg('Oops, unable to copy'), err);
    }

    document.body.removeChild(textArea);
  }

  function copyTextToClipboard(text) {
    if (!navigator.clipboard) {
      fallbackCopyTextToClipboard(text);
      return;
    }
    navigator.clipboard.writeText(text).then(
      function () {
        console.log(wrapperMsg('Copying to clipboard was successful!'));
      },
      function (err) {
        console.error(wrapperMsg('Could not copy text: '), err);
      }
    );
  }

  function listener(event) {
    if ((event.ctrlKey && event.altKey) || event.altKey) {
      event.preventDefault();
      event.stopPropagation();
      const text = event.target.innerText;
      copyTextToClipboard(text);
      return false;
    }
  }

  document.addEventListener('click', listener, true);
})();