function copy_cmd_ind_clear(caller) {
        caller.setAttribute("icon-title", "Copy Commands");
}

function copy_cmd_ind(caller) {
        caller.setAttribute("icon-title", "Copied!");

        setTimeout(copy_cmd_ind_clear, 1000, caller);
}

function cli_example_copy_cmd(element_id, caller) {
        const regexp = /^(?:[\/a-zA-Z0-9_\-:\. ,:]*[#\$]&gt;)\s*(?:<[^\>]*\>)*([\/a-zA-Z0-9_\-\\. ,:\*!]*)(?:<[^\>]*\>)*$/gm;
        var element = document.querySelector(element_id);
        text = element.innerHTML;
        var clipboard_str = ""
        var i;

        const array = [...text.matchAll(regexp)];
        for (const match of array) {

                clipboard_str += match[1] + "\n";
        }

        navigator.clipboard.writeText(clipboard_str);

        copy_cmd_ind(caller);
}


function cli_example_header_insert() {
        var param_list = document.querySelectorAll('pre.cli-example');
        var i;

        for (i = 0; i < param_list.length; i++) {
                const pre_header = document.createElement("pre");
                var cli_example_pre = param_list[i];
                var pre_id = "cli-exmaple-" + i;

                pre_header.classList.add("cli-example-header");
                pre_header.innerHTML = '<i class="fa-solid fa-terminal fa-lg"></i>' +
                '<i class="fa-solid fa-copy fa-lg copy_content" ' +
                'icon-title=\"Copy Commands\" ' +
                'onclick=\"cli_example_copy_cmd(\'#' + pre_id + '\', this)\"></i>';

                cli_example_pre.insertAdjacentElement("beforebegin", pre_header);
                cli_example_pre.id = pre_id;
        }
}


cli_example_header_insert();
