Hold Alt click on text, Copy as plain text. Alt + Shift click on text, Copy as kebab-case.
目前為
// ==UserScript==
// @name Copy it
// @name:zh-CN 便捷复制
// @namespace https://github.com/xianghongai/Tampermonkey-UserScript
// @version 1.0.2
// @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 none
// @license MIT
// ==/UserScript==
(function () {
'use strict';
document.addEventListener('click', listener, false);
function listener(event) {
if (event.altKey) {
event.preventDefault();
event.stopPropagation();
const text = event.target.innerText;
if (event.shiftKey) {
copyTextToClipboard(toKebab(text));
return false;
}
copyTextToClipboard(text);
return false;
}
}
function wrapperMsg(input) {
const prefix = `__Tampermonkey® (Hold Ctrl + Alt or Alt click on text, copy it)__: `;
return `${prefix}${input}`;
}
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 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);
}
);
}
})();