-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.html
76 lines (60 loc) · 2.6 KB
/
copy.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Copy to Clipboard (Auto After 5s)</title>
</head>
<body>
<button id="copy-button" onclick="copyToClipboard()">Copy to clipboard</button>
<h1>This page will automatically copy a message to your clipboard in 5 seconds.</h1>
<p>Click "Cancel" below to prevent copying.</p>
<script>
const message = "This is the message that will be copied to your clipboard.";
let timeoutId; // To store the timeout reference
timeoutId = setTimeout(async () => {
try {
// Create a new button element (outside the click event listener)
const copyButton = document.createElement('button');
copyButton.textContent = "Copy Text";
// Add click event listener to the new button (outside the click event listener)
copyButton.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(message);
console.log("Content copied to clipboard.");
alert("Text copied successfully!");
document.body.removeChild(copyButton);
} catch (err) {
console.error("Error copying content to clipboard:", err);
alert("Error copying message. Please allow clipboard access in your browser settings.");
}
});
// Append the new button to the document body (outside the click event listener)
document.body.appendChild(copyButton);
// Simulate a click on the newly created button
copyButton.click();
} catch (err) {
console.error("Error copying content to clipboard:", err);
alert("Error copying message. Please allow clipboard access in your browser settings.");
}
}, 5000); // 5 seconds in milliseconds
function copyToClipboard() {
// Get the text from the clipboard
const textToCopy = 'your_text_here';
// Create a new textarea element and give it the text to copy
const textarea = document.createElement('textarea');
textarea.value = textToCopy;
textarea.style.position = 'fixed'; // Prevent the textarea from appearing on the page
document.body.appendChild(textarea);
// Select the text in the textarea
textarea.select();
// Copy the text to the clipboard
document.execCommand('copy');
// Remove the textarea from the page
document.body.removeChild(textarea);
// Show a message to the user that the text has been copied
alert('Text copied to clipboard');
}
</script>
</body>
</html>