PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 5.5.4
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v5.5.4
5.5.4 5.5.3 3.1.0 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 4.0.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 All 79 releases
copy-the-code / assets / js / clipboard.js

clipboard.js in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 5.5.4, at assets/js/clipboard.js

105 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 window.CTCWP = (function (window, document, navigator) {
2 var textArea,
3 copy;
4
5 function isOS() {
6 return navigator.userAgent.match( /ipad|iphone/i );
7 }
8
9 function createTextArea(text) {
10 textArea = document.createElement( 'textArea' );
11 textArea.value = text;
12 document.body.appendChild( textArea );
13 }
14
15 function selectText() {
16 var range,
17 selection;
18
19 if (isOS()) {
20 range = document.createRange();
21 range.selectNodeContents( textArea );
22 selection = window.getSelection();
23 selection.removeAllRanges();
24 selection.addRange( range );
25 textArea.setSelectionRange( 0, 999999 );
26 } else {
27 textArea.select();
28 }
29 }
30
31 function copyToClipboard() {
32 // Use modern Clipboard API (iOS 26+ compatible)
33 if ( navigator.clipboard && navigator.clipboard.writeText ) {
34 navigator.clipboard.writeText( textArea.value ).then(
35 function() {
36 document.body.removeChild( textArea );
37 // Redirect to page.
38 if ( typeof copyTheCode !== 'undefined' && copyTheCode.redirect_url ) {
39 window.location.href = copyTheCode.redirect_url;
40 }
41 }
42 ).catch(
43 function() {
44 // Fallback to legacy method if Clipboard API fails
45 document.execCommand( 'copy' );
46 document.body.removeChild( textArea );
47 // Redirect to page.
48 if ( typeof copyTheCode !== 'undefined' && copyTheCode.redirect_url ) {
49 window.location.href = copyTheCode.redirect_url;
50 }
51 }
52 );
53 } else {
54 // Fallback for older browsers
55 document.execCommand( 'copy' );
56 document.body.removeChild( textArea );
57 // Redirect to page.
58 if ( typeof copyTheCode !== 'undefined' && copyTheCode.redirect_url ) {
59 window.location.href = copyTheCode.redirect_url;
60 }
61 }
62 }
63
64 copy = function (text) {
65 createTextArea( text );
66 selectText();
67 copyToClipboard();
68 }
69
70 copySelection = function ( source ) {
71 const selection = window.getSelection()
72 const range = document.createRange()
73 range.selectNodeContents( source[0] )
74 selection.removeAllRanges()
75 selection.addRange( range )
76
77 // Get selected text
78 const selectedText = selection.toString();
79
80 // Use modern Clipboard API (iOS 26+ compatible)
81 if ( navigator.clipboard && navigator.clipboard.writeText ) {
82 navigator.clipboard.writeText( selectedText ).then(
83 function() {
84 selection.removeAllRanges();
85 }
86 ).catch(
87 function() {
88 // Fallback to legacy method if Clipboard API fails
89 document.execCommand( 'copy' );
90 selection.removeAllRanges();
91 }
92 );
93 } else {
94 // Fallback for older browsers
95 document.execCommand( 'copy' );
96 selection.removeAllRanges();
97 }
98 }
99
100 return {
101 copy: copy,
102 copySelection: copySelection,
103 };
104 })( window, document, navigator );
105