library to copy the string and html parameters to the clipboard
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/491896/1355860/Copy%20Text%20and%20HTML%20to%20Clipboard.js
// ==UserScript==
// @name Copy Text and HTML to Clipboard
// @namespace https://greasyfork.org/en/users/906106-escctrl
// @description library to copy the string and html parameters to the clipboard
// @author escctrl
// @version 1.0
// @grant none
// @license MIT
// ==/UserScript==
// solution for setting richtext clipboard content found at https://jsfiddle.net/jdhenckel/km7prgv4/3/
// and https://stackoverflow.com/questions/34191780/javascript-copy-string-to-clipboard-as-text-html/74216984#74216984
function copy2Clipboard(e, str, html=null) {
if (html === null) html = str;
// trying first with the new Clipboard API
try {
const clipboardItem = new ClipboardItem({'text/html': new Blob([html], {type: 'text/html'}),
'text/plain': new Blob([str], {type: 'text/plain'})});
navigator.clipboard.write([clipboardItem]);
}
// fallback method in case clipboard.write is not enabled - especially in Firefox it's disabled by default
// to enable, go to about:config and turn dom.events.asyncClipboard.clipboardItem to true
catch(err) {
function listener(e) {
e.clipboardData.setData("text/html", html);
e.clipboardData.setData("text/plain", str);
e.preventDefault();
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
}
}