🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/13 21:20:39
🔄 下次更新时间:2026/01/13 22:20:39
手动刷新缓存

copy_text.js

复制文字内容到剪切板

Acest script nu ar trebui instalat direct. Aceasta este o bibliotecă pentru alte scripturi care este inclusă prin directiva meta a // @require https://update.greasyfork.org/scripts/492927/1362733/copy_textjs.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         copy_text.js
// @namespace    https://github.com/iibeibei
// @version      0.0.1
// @description  复制文字内容到剪切板
// @author       Beibei
// @license      GPLv3
// @match        *://*/*
// ==/UserScript==

self.copyText = function (button, content, success) {
	if (!button) {
		return;
	}

	if (typeof content == 'function') {
		success = content;
		content = null;
	}

	success = success || function () {};

	// 是否降级使用
	var isFallback = !navigator.clipboard;

	if (typeof button == 'string' && !content) {
		if (content === false) {
			isFallback = true;
		}
		content = button;
		button = null;
	}

	var eleTextarea = document.querySelector('#tempTextarea');
	if (!eleTextarea && isFallback) {
		eleTextarea = document.createElement('textarea');
		eleTextarea.style.width = 0;
		eleTextarea.style.position = 'fixed';
		eleTextarea.style.left = '-999px';
		eleTextarea.style.top = '10px';
		eleTextarea.setAttribute('readonly', 'readonly');
		document.body.appendChild(eleTextarea);
	}

	var funCopy = function (text, callback) {
		callback = callback || function () {};

		if (!isFallback) {
			navigator.clipboard.writeText(text).then(
				function () {
					callback();
					// 成功回调
					success(text);
				},
				function () {
					// 禁止写入剪切板后使用兜底方法
					copyText(text, false);
					callback();
					// 成功回调
					success(text);
				}
			);

			return;
		}

		eleTextarea.value = text;
		eleTextarea.select();
		document.execCommand('copy', true);

		callback();
		// 成功回调
		success(text);
	};

	// 提示复制成功的方法
	// 对外可访问
	copyText.tips = function (event) {
		if (!event) {
			return;
		}
		// 复制成功提示
		var eleTips = document.createElement('span');
		eleTips.className = 'text-popup';
		eleTips.innerHTML = '复制成功';
		document.body.appendChild(eleTips);
		// 事件
		eleTips.addEventListener('animationend', function () {
			eleTips.parentNode.removeChild(eleTips);
		});
		// For IE9
		if (!history.pushState) {
			setTimeout(function () {
				eleTips.parentNode.removeChild(eleTips);
			}, 1000);
		}

		eleTips.style.left = event.pageX - eleTips.clientWidth / 2 + 'px';
		eleTips.style.top = event.pageY - eleTips.clientHeight + 'px';
	};

	var strStyle =
		'.text-popup { animation: textPopup 1s both; -ms-transform: translateY(-20px); color: #01cf97; user-select: none; white-space: nowrap; position: absolute; z-index: 99; }@keyframes textPopup {0%, 100% { opacity: 0; } 5% { opacity: 1; } 100% { transform: translateY(-50px); }}';

	var eleStyle = document.querySelector('#popupStyle');
	if (!eleStyle) {
		eleStyle = document.createElement('style');
		eleStyle.id = 'popupStyle';
		eleStyle.innerHTML = strStyle;
		document.head.appendChild(eleStyle);
	}

	if (!button) {
		funCopy(content);
		return;
	}

	// 事件绑定
	button.addEventListener('click', function (event) {
		var strCopy = content;
		if (content && content.tagName) {
			strCopy = content.textContent || content.value;
		}
		// 复制的文字内容
		if (!strCopy) {
			return;
		}

		funCopy(strCopy, function () {
			copyText.tips(event);
		});
	});
};