@@ -2,8 +2,50 @@ $(function() {
22 var $textarea = $ ( "#contents" ) ,
33 $printarea = $ ( '#printable_contents' ) ,
44 $loading = $ ( "#loading" ) ,
5+ $copyButton = $ ( "#copy_button" ) ,
6+ $pasteButton = $ ( "#paste_button" ) ,
57 content = $textarea . val ( ) ;
68
9+ function selectAllContents ( ) {
10+ var textarea = $textarea . get ( 0 ) ;
11+
12+ $textarea . trigger ( "focus" ) ;
13+ textarea . setSelectionRange ( 0 , textarea . value . length ) ;
14+ }
15+
16+ async function copyContents ( ) {
17+ var text = $textarea . val ( ) ;
18+
19+ if ( navigator . clipboard && window . isSecureContext ) {
20+ await navigator . clipboard . writeText ( text ) ;
21+ return ;
22+ }
23+
24+ // fallback for older browsers
25+ selectAllContents ( ) ;
26+ document . execCommand ( "copy" ) ;
27+ }
28+
29+ async function pasteContents ( ) {
30+ if ( navigator . clipboard && window . isSecureContext ) {
31+ $textarea . val ( await navigator . clipboard . readText ( ) ) ;
32+ return ;
33+ }
34+
35+ // fallback for older browsers
36+ $textarea . focus ( ) ;
37+ $textarea . setSelectionRange ( 0 , $textarea . value . length ) ;
38+ document . execCommand ( "paste" ) ;
39+ }
40+
41+ $copyButton . on ( "click" , function ( ) {
42+ copyContents ( ) . catch ( function ( ) { } ) ;
43+ } ) ;
44+
45+ $pasteButton . on ( "click" , function ( ) {
46+ pasteContents ( ) . catch ( function ( ) { } ) ;
47+ } ) ;
48+
749 $printarea . text ( content ) ;
850
951 setInterval ( function ( ) {
0 commit comments