<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Copy Functions</title>
<script>
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// 防止元素显示在屏幕上
textArea.style.position = "fixed";
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.width = "2em";
textArea.style.height = "2em";
textArea.style.padding = "0";
textArea.style.border = "none";
textArea.style.outline = "none";
textArea.style.boxShadow = "none";
textArea.style.background = "transparent";
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copy text command was ' + msg);
} catch (err) {
console.error('Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyHTMLToClipboard(html) {
var tempDiv = document.createElement("div");
tempDiv.innerHTML = html;
var selection = window.getSelection();
var range = document.createRange();
window.getSelection().removeAllRanges();
range.selectNodeContents(tempDiv);
selection.addRange(range);
var successful = document.execCommand('copy');
selection.removeAllRanges();
tempDiv.parentNode.removeChild(tempDiv);
console.log('Copy HTML command was ' + (successful ? 'successful' : 'unsuccessful'));
}
function downloadImage(href) {
var image = new Image();
image.src = href;
image.crossOrigin = 'Anonymous';
image.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, image.width, image.height);
var dataURL = canvas.toD
评论已关闭