@@ -2,28 +2,57 @@ $(document).ready(function () {
2
2
3
3
var UUIDPosition = document . getElementById ( 'uuid_p' ) ;
4
4
var btnGenerateNewUUID = document . getElementById ( 'btnGenerateNewUUID' ) ;
5
+ var btnCopyUUID = document . getElementById ( 'btnCopyUUID' ) ;
5
6
7
+ //initial presentation
6
8
UUIDPosition . innerHTML = uuid ( ) ;
7
9
10
+ //button generate new uuid
8
11
btnGenerateNewUUID . addEventListener ( "click" , function ( ) {
9
12
UUIDPosition . innerHTML = uuid ( ) ;
10
13
} ) ;
11
14
15
+ //button copy uuid
16
+ btnCopyUUID . addEventListener ( "click" , function ( ) {
17
+ copyToClipboard ( ) ;
18
+ } ) ;
19
+
12
20
//generate new uuid
13
21
function uuid ( ) {
14
22
var seed = Date . now ( ) ;
15
23
if ( window . performance && typeof window . performance . now === "function" ) {
16
24
seed += performance . now ( ) ;
17
25
}
18
26
19
- var uuid = 'xxxxxxxx-xxxx-4xxx -yxxx-xxxxxxxxxxxx ' . replace ( / [ x y ] / g, function ( c ) {
27
+ var uuid = 'xxxxxxxx-xxxx-xxxx -yxxx-wxxexxdxxoxx ' . replace ( / [ x y ] / g, function ( c ) {
20
28
var r = ( seed + Math . random ( ) * 16 ) % 16 | 0 ;
21
29
seed = Math . floor ( seed / 16 ) ;
22
30
23
31
return ( c === 'x' ? r : r & ( 0x3 | 0x8 ) ) . toString ( 16 ) ;
24
32
} ) ;
25
33
26
34
return uuid ;
27
- }
35
+ } ;
36
+
37
+ //copy uuid
38
+ function copyToClipboard ( ) {
39
+ var el = document . createElement ( 'textarea' ) ; // Create a <textarea> element
40
+ el . value = $ ( document . getElementById ( 'uuid_p' ) ) . text ( ) ; // Set its value to the string that you want copied
41
+ el . setAttribute ( 'readonly' , '' ) ; // Make it readonly to be tamper-proof
42
+ el . style . position = 'absolute' ;
43
+ el . style . left = '-9999px' ; // Move outside the screen to make it invisible
44
+ document . body . appendChild ( el ) ; // Append the <textarea> element to the HTML document
45
+ var selected =
46
+ document . getSelection ( ) . rangeCount > 0 // Check if there is any content selected previously
47
+ ? document . getSelection ( ) . getRangeAt ( 0 ) // Store selection if found
48
+ : false ; // Mark as false to know no selection existed before
49
+ el . select ( ) ; // Select the <textarea> content
50
+ document . execCommand ( 'copy' ) ; // Copy - only works as a result of a user action (e.g. click events)
51
+ document . body . removeChild ( el ) ; // Remove the <textarea> element
52
+ if ( selected ) { // If a selection existed before copying
53
+ document . getSelection ( ) . removeAllRanges ( ) ; // Unselect everything on the HTML document
54
+ document . getSelection ( ) . addRange ( selected ) ; // Restore the original selection
55
+ }
56
+ } ;
28
57
29
58
} ) ;
0 commit comments