forked from DeanMarkTaylor/clipboard-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
114 lines (100 loc) · 3.86 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Clipboard Test</title>
</head>
<body>
<div style="display:block;">
<textarea class="js-useragent" rows="3" style="width: 100%"></textarea>
</div>
<div style="display:inline-block; vertical-align:top;">
<button class="js-copy-bob-btn">Set clipboard to BOB</button><br /><br />
<button class="js-copy-jane-btn">Set clipboard to JANE</button>
</div>
<div style="display:inline-block;">
<textarea class="js-test-textarea" cols="35" rows="4">Try pasting into here to see what you have on your clipboard:
</textarea>
<input type='text' id='camCodigo' name='camCodigo' value='' style='font-size: 16px;'>
</div>
<div style="display:block;">
<textarea class="js-console" rows="10" style="width: 100%"></textarea>
</div>
<script>
function fallbackCopyTextToClipboard(text) {
var 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('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: 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('Async: Copying to clipboard was successful!');
document.getElementById('camCodigo').value = text;
document.getElementById('camCodigo').select();
}, function(err) {
console.error('Async: Could not copy text: ', err);
document.getElementById('camCodigo').value = 'erro!';
document.getElementById('camCodigo').select();
});
}
document.querySelector('.js-useragent').value = navigator.userAgent;
var copyBobBtn = document.querySelector('.js-copy-bob-btn'),
copyJaneBtn = document.querySelector('.js-copy-jane-btn');
copyBobBtn.addEventListener('click', function(event) {
copyTextToClipboard('Bob');
});
copyJaneBtn.addEventListener('click', function(event) {
copyTextToClipboard('Jane');
});
// Displaying console output without opening dev tools.
// Please don't include this console override in any production code.
window.console = (function(oldConsole) {
var counter = 0,
localLog = function(logType, text) {
counter++;
var con = document.querySelector('.js-console');
con.value += "\n" + counter + ' ' + logType + ': ' + text;
con.scrollTop = con.scrollHeight;
};
return {
log: function(text) {
oldConsole.log.apply(null, arguments);
localLog('LOG', text);
},
info: function(text) {
oldConsole.info.apply(null, arguments);
localLog('INFO', text);
},
warn: function(text) {
oldConsole.warn.apply(null, arguments);
localLog('WARN', text);
},
error: function(text) {
oldConsole.error.apply(null, arguments);
localLog('ERROR', text);
localLog('ERROR - open developers console for more detail');
}
};
}(window.console));
</script>
</body>
</html>
<!--This is an exemple of hide and display: https://jsfiddle.net/82ewo05p/-->