# copy-the-code/3.4.0/classes/blocks/assets/js/core.js

Copy Anything to Clipboard for WordPress – Copy Button, Copy Text &amp; Copy Code, version 3.4.0. 83 lines.

- Page: https://pluginprobe.com/plugins/copy-the-code/3.4.0/code/classes/blocks/assets/js/core.js
- Raw: https://pluginprobe.com/plugins/copy-the-code/3.4.0/raw/classes/blocks/assets/js/core.js
- Modified: 2023-10-28T18:56:20+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/copy-the-code/3.4.0/code/classes/blocks/assets/js/core.js#L10-L20`.

```javascript
(function ($) {
    const CTCCore = {

        /**
         * Init
         */
        init: function () {
            this._bind();
        },

        /**
         * Binds events
         */
        _bind: function () {
            $( document ).on( 'click', '.ctc-block-copy', this.doCopy );
        },

        /**
         * Do Copy to Clipboard
         */
        doCopy: function (event) {
            event.preventDefault();

            let btn = $(this),
                btnText = btn.find( '.ctc-button-text' ),
                oldText = btnText.text(),
                copiedText = btn.attr( 'data-copied' ) || 'Copied',
                copyAsRaw = btn.attr( 'copy-as-raw' ) || '',
                block = btn.parents('.ctc-block'),
                content = block.find('.ctc-copy-content').val();

                if ( ! copyAsRaw ) {
                    // Convert the <br/> tags into new line.
                    content = content.replace(/<br\s*[\/]?>/gi, "\n");

                    // Convert the <div> tags into new line.
                    content = content.replace(/<div\s*[\/]?>/gi, "\n");

                    // Convert the <p> tags into new line.
                    content = content.replace(/<p\s*[\/]?>/gi, "\n\n");

                    // Convert the <li> tags into new line.
                    content = content.replace(/<li\s*[\/]?>/gi, "\n");

                    // Remove all tags.
                    content = content.replace(/(<([^>]+)>)/ig, '');
                
                    // Remove white spaces.
                    content = content.replace(new RegExp("/^\s+$/"), "");
                }

                // Remove first and last new line.
                content = $.trim(content);

                // Support for IOS devices too.
                CTC.copy(content);

            if ( btn.hasClass('ctc-block-copy-icon') ) {
                // Copied!
                btn.addClass('copied');
                setTimeout(function () {
                    btn.removeClass('copied')
                }, 1000);
            } else {
                // Copied!
                btnText.text( copiedText );
                block.addClass('copied')
                setTimeout(function () {
                    btnText.text(oldText)
                    block.removeClass('copied')
                }, 1000);
            }
        }
    };

    /**
     * Initialization
     */
    $(function () {
        CTCCore.init();
    });

})(jQuery);
```
