PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 3.3.0
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v3.3.0
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 4.1.1 All 78 releases
copy-the-code / assets / js / copy-the-code.js

copy-the-code.js in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 3.3.0, at assets/js/copy-the-code.js

451 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 window.CopyTheCodeToClipboard = (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 document.execCommand('copy');
33 document.body.removeChild(textArea);
34
35 // Redirect to page.
36 if( copyTheCode.redirect_url ) {
37 window.location.href = copyTheCode.redirect_url;
38 }
39 }
40
41 copy = function (text) {
42 createTextArea(text);
43 selectText();
44 copyToClipboard();
45 };
46
47 return {
48 copy: copy
49 };
50 })(window, document, navigator);
51
52 (function ($) {
53
54 CopyTheCode = {
55
56 selector: copyTheCode.settings.selector || copyTheCode.selector || 'pre',
57 button_position: copyTheCode.settings['button-position'] || 'inside',
58
59 /**
60 * Init
61 */
62 init: function () {
63 this._bind();
64 this._initialize();
65 },
66
67 /**
68 * Binds events
69 */
70 _bind: function () {
71 $(document).on('click', '.copy-the-code-button', CopyTheCode.copyCode);
72 $(document).on('click', '.copy-the-code-shortcode', CopyTheCode.copyShortcode);
73 },
74
75 /**
76 * Initialize the Button
77 */
78 _initialize: function () {
79 if (!$(copyTheCode.selectors).length) {
80 return;
81 }
82
83 $(copyTheCode.selectors).each(function (index, el) {
84 var button_copy_text = el['button_copy_text'] || '';
85 var button_position = el['button_position'] || '';
86 var button_text = el['button_text'] || '';
87 var button_title = el['button_title'] || '';
88 var selector = el['selector'] || '';
89 var style = el['style'] || '';
90 var copy_format = el['copy_format'] || '';
91
92 $(selector).each(function (index, single_selector) {
93
94 var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style);
95
96 $(single_selector).addClass('copy-the-code-target');
97
98 if ('cover' !== style && 'outside' === button_position) {
99 $(single_selector).wrap('<span data-copy-format="' + copy_format + '" data-button-text="' + button_text + '" data-button-position="' + button_position + '" data-button-copy-text="' + button_copy_text + '" data-style="' + style + '" data-button-title="' + button_title + '" data-selector="' + selector + '" class="copy-the-code-wrap copy-the-code-style-' + style + ' copy-the-code-outside-wrap"></span>');
100 $(single_selector).parent().prepend('<div class="copy-the-code-outside">' + buttonMarkup + '</div>');
101 } else {
102 $(single_selector).wrap('<span data-copy-format="' + copy_format + '" data-button-text="' + button_text + '" data-button-position="' + button_position + '" data-button-copy-text="' + button_copy_text + '" data-style="' + style + '" data-button-title="' + button_title + '" data-selector="' + selector + '" class="copy-the-code-wrap copy-the-code-style-' + style + ' copy-the-code-inside-wrap"></span>');
103 $(single_selector).append(buttonMarkup);
104 }
105
106 switch (style) {
107 case 'svg-icon':
108 $(single_selector).find('.copy-the-code-button').html(copyTheCode.buttonSvg);
109 break;
110 case 'cover':
111 case 'button':
112 default:
113 $(single_selector).find('.copy-the-code-button').html(button_text);
114 break;
115 }
116
117
118 });
119 });
120
121 },
122
123 /**
124 * Get Copy Button Markup
125 */
126 _getButtonMarkup: function (button_title, button_text, style) {
127 if ('svg-icon' === style) {
128 button_text = copyTheCode.buttonSvg;
129 }
130
131 return '<button class="copy-the-code-button" data-style="' + style + '" title="' + button_title + '">' + button_text + '</button>';
132 },
133
134 format: function (html) {
135 var tab = '\t';
136 var result = '';
137 var indent = '';
138
139 html.split(/>\s*</).forEach(function (element) {
140 if (element.match(/^\/\w/)) {
141 indent = indent.substring(tab.length);
142 }
143
144 result += indent + '<' + element + '>\r\n';
145
146 if (element.match(/^<?\w[^>]*[^\/]$/) && !element.startsWith("input")) {
147 indent += tab;
148 }
149 });
150
151 return result.substring(1, result.length - 3);
152 },
153
154 /**
155 * Copy to Clipboard
156 */
157 copyShortcode: function (event) {
158 event.preventDefault();
159
160 var btn = $(this),
161 oldText = btn.html(),
162 target = btn.attr('data-target') || '',
163 copy_content_as = btn.attr('data-copy-as') || copyTheCode.copy_content_as,
164 button_copy_text = btn.attr('data-button-copy-text') || '',
165 content = btn.attr('data-content') || '',
166 link = btn.attr( 'data-link' ) || '';
167
168 // Copy the secrate content.
169 if (content) {
170 CopyTheCodeToClipboard.copy(content);
171 // Copied!
172 btn.text(button_copy_text);
173 setTimeout(function () {
174 btn.html(oldText);
175
176 if( link ) {
177 window.open(link, '_blank').focus()
178 }
179 }, 1000);
180 return;
181 }
182
183 var source = $(target);
184
185 if (!source.length) {
186 btn.text('Not found!');
187 setTimeout(function () {
188 btn.text(oldText);
189 }, 1000);
190 return;
191 }
192
193 var html = source.html();
194
195 html = CopyTheCode.format(html);
196
197 if ('html' !== copy_content_as) {
198
199 // Convert the <br/> tags into new line.
200 var brRegex = /<br\s*[\/]?>/gi;
201 html = html.replace(brRegex, "\n");
202
203 // Convert the <div> tags into new line.
204 var divRegex = /<div\s*[\/]?>/gi;
205 html = html.replace(divRegex, "\n");
206
207 // Convert the <p> tags into new line.
208 var pRegex = /<p\s*[\/]?>/gi;
209 html = html.replace(pRegex, "\n");
210
211 // Convert the <li> tags into new line.
212 var pRegex = /<li\s*[\/]?>/gi;
213 html = html.replace(pRegex, "\n");
214
215 // Remove all tags.
216 html = html.replace(/(<([^>]+)>)/ig, '');
217 }
218
219 if ('html' !== copy_content_as) {
220 html = html.replace(/[\t\n]+/gm, ' ').trim();
221 } else {
222 var reWhiteSpace = new RegExp("/^\s+$/");
223 html = html.replace(reWhiteSpace, "");
224 }
225
226 var tempElement = $("<div id='temp-element'></div>");
227 $("body").append(tempElement);
228 html = $.trim(html);
229 $('#temp-element').html(html);
230 var html = $('#temp-element').html();
231 $('#temp-element').remove();
232
233 var tempHTML = html;
234
235 // Copy the Code.
236 var tempPre = $("<textarea id='temp-pre'>"),
237 temp = $("<textarea>");
238
239 // Append temporary elements to DOM.
240 $("body").append(temp);
241 $("body").append(tempPre);
242
243 // Set temporary HTML markup.
244 tempPre.html(tempHTML);
245
246 var content = tempPre.text();
247
248 content = content.trim();
249
250 if( copyTheCode.trim_lines ) {
251 content = content.replace(/^(?=\n)$|\s*$|\n\n+/gm,"");
252 if( content ) {
253 content = content.split('\n');
254 content = content.map( item => {
255 return item.trim();
256 } );
257 content = content.join('\n');
258 }
259 }
260
261 // Format the HTML markup.
262 temp.val(content).select();
263
264 // Support for IOS devices too.
265 CopyTheCodeToClipboard.copy(content.trim());
266
267 // Remove temporary elements.
268 temp.remove();
269 tempPre.remove();
270
271 // Copied!
272 btn.text(button_copy_text);
273 setTimeout(function () {
274 btn.text(oldText);
275 }, 1000);
276 },
277
278 /**
279 * Copy to Clipboard
280 */
281 copyCode: function (event) {
282 event.preventDefault();
283
284 var btn = $(this),
285 oldText = btn.text(),
286 parent = btn.parents('.copy-the-code-wrap'),
287 selector = parent.attr('data-selector') || '',
288 button_text = parent.attr('data-button-text') || '',
289 button_position = parent.attr('data-button-position') || '',
290 button_copy_text = parent.attr('data-button-copy-text') || '',
291 button_title = parent.attr('data-button-title') || '',
292 style = parent.attr('data-style') || ''
293 copy_format = parent.attr('data-copy-format') || '';
294
295 // Fix: nested selectors e.g. `.entry-content pre`
296 if (selector.indexOf(' ') >= 0) {
297 var source = btn.parents('.copy-the-code-wrap');
298 } else {
299 var source = btn.parents('.copy-the-code-wrap').find(selector);
300 }
301
302 if ( 'google-docs' === copy_format ) {
303 // Hide button temporary.
304 if ( 'inside' === button_position ) {
305 $( '.copy-the-code-button' ).hide();
306 }
307
308 var range = document.createRange();
309 range.selectNode(source[0]);
310 window.getSelection().addRange(range);
311 document.execCommand('copy');
312
313 // Clear selection.
314 if (window.getSelection) {
315 if (window.getSelection().empty) { // Chrome
316 window.getSelection().empty();
317 } else if (window.getSelection().removeAllRanges) { // Firefox
318 window.getSelection().removeAllRanges();
319 }
320 } else if (document.selection) { // IE?
321 document.selection.empty();
322 }
323
324 // Show button again.
325 if ( 'inside' === button_position ) {
326 $( '.copy-the-code-button' ).show();
327 }
328
329 } else {
330
331 var html = source.html();
332
333 var buttonMarkup = CopyTheCode._getButtonMarkup(button_title, button_text, style);
334
335 // Remove the <copy> button.
336 html = html.replace(buttonMarkup, '');
337
338 // Emojis.
339 let allEmojis = CopyTheCode._getImages( html );
340
341 if ( allEmojis ) {
342 allEmojis.map( function(image) {
343 let alt = CopyTheCode._getAlt( image );
344 if ( alt ) {
345 let cleanAlt = alt[0];
346 cleanAlt = cleanAlt.replaceAll( '"', '' );
347 cleanAlt = cleanAlt.replaceAll( "'", '' );
348 cleanAlt = cleanAlt.replaceAll( 'alt=', '' );
349 html = html.replaceAll( image, cleanAlt );
350 }
351 });
352
353 }
354
355 // Remove all duplicate empty lines.
356 if( copyTheCode.remove_spaces ) {
357 html = html.replace(/^(?=\n)$|\s*$|\n\n+/gm,"");
358 }
359
360 if ('html' !== copyTheCode.copy_content_as) {
361 // Convert the <br/> tags into new line.
362 var brRegex = /<br\s*[\/]?>/gi;
363 html = html.replace(brRegex, "\n");
364
365 // Convert the <div> tags into new line.
366 var divRegex = /<div\s*[\/]?>/gi;
367 html = html.replace(divRegex, "\n");
368
369 // Convert the <p> tags into new line.
370 var pRegex = /<p\s*[\/]?>/gi;
371 html = html.replace(pRegex, "\n");
372
373 // Convert the <li> tags into new line.
374 var pRegex = /<li\s*[\/]?>/gi;
375 html = html.replace(pRegex, "\n");
376
377 // Remove all tags.
378 html = html.replace(/(<([^>]+)>)/ig, '');
379 }
380
381 // Remove white spaces.
382 var reWhiteSpace = new RegExp("/^\s+$/");
383 html = html.replace(reWhiteSpace, "");
384
385 var tempElement = $("<div id='temp-element'></div>");
386 $("body").append(tempElement);
387 html = $.trim(html);
388 $('#temp-element').html(html);
389 var html = $('#temp-element').html();
390 $('#temp-element').remove();
391
392 var tempHTML = html;
393 var reWhiteSpace = new RegExp("/^\s+$/");
394 tempHTML = tempHTML.replace(reWhiteSpace, "");
395
396 // Remove button text.
397 tempHTML = tempHTML.replace(button_text, '');
398
399 // Copy the Code.
400 var tempPre = $("<textarea id='temp-pre'>"),
401 temp = $("<textarea>");
402
403 // Append temporary elements to DOM.
404 $("body").append(temp);
405 $("body").append(tempPre);
406
407 // Set temporary HTML markup.
408 var content = tempPre.html(tempHTML).text();
409
410 content = $.trim(content);
411
412 // Format the HTML markup.
413 temp.val(content).select();
414
415 // Support for IOS devices too.
416 CopyTheCodeToClipboard.copy(content);
417
418 // Remove temporary elements.
419 temp.remove();
420 tempPre.remove();
421 }
422
423 // Copied!
424 btn.text(button_copy_text);
425 setTimeout(function () {
426 if ('svg-icon' === style) {
427 btn.html(copyTheCode.buttonSvg);
428 } else {
429 btn.text(oldText);
430 }
431 }, 1000);
432 },
433
434 _getImages( content ) {
435 return content.match( /<img\s+[^>]*?src=("|')([^"']+)">/gi );
436 },
437
438 _getAlt( img ) {
439 return img.match( /alt=("|')([^"']+)"|'/gi );
440 }
441
442 };
443
444 /**
445 * Initialization
446 */
447 $(function () {
448 CopyTheCode.init();
449 });
450
451 })(jQuery);