0

I want to make the method or only copy to clipboard the "syntax" part of a paragraph. I've done the logic to get the specific part of content I want and stored it in variable "syntaxClean". Now I just need to copy it somehow.

document.execCommand("copy"); would be awesome, but I just can't seem to make it work.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test</title>
</head>

<body>
    <pre id="test"></pre>

    <script>
        const message = "====== Executor details =======\nathena@21.109.21.25 (tunneled:39516)\n====== Request details ========\nClass....................: com.ericsson.athena.taf.os.linux.commands.common.SimpleCmdRequest\nThread : ................: main\nExpected prompt..........: ^((?![&lt;?]|\\\\.{3}|(\\\\S+\\\\s){6,}).)*[&gt;#$%]+(\\\\\\\\u001B\\\\[(\\\\d;?)*[m|n|K])*\\\\s(\\\\\\\\u001B\\\\[(\\\\d;?)*[m|n|K])*$|#\\\\s\\\\u001B\\\\[6n\nPrompt forced............: false\nTimeout..................: 20000ms\nSyntax...................: lsb_release -i\n"
        document.getElementById("test").append(message);

        var res = message.split("\n");
        for (var i in res) {
            if (res[i].indexOf("Syntax") != -1) {
                var syntax = res[i].split(':');
                var syntaxClean = syntax[1].slice(1);
                console.log(syntaxClean);
            }
        }
    </script>
</body>

</html>

In this example I would like to copy to clipboard "lsb_release -i" and I have it stored in variable syntaxClean as I've already said above.

Any help is appreciated!

  • Possible duplicate of [Click button copy to clipboard using jQuery](https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery) – Devsi Odedra Sep 09 '19 at 11:56

1 Answers1

0

You can achieve this by creating a dummy textarea like this:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Test</title>
</head>

<body>
    <pre id="test"></pre>
    <button onclick="copy()">Copy</button>

    <script>
        const message = "====== Executor details =======\nathena@21.109.21.25 (tunneled:39516)\n====== Request details ========\nClass....................: com.ericsson.athena.taf.os.linux.commands.common.SimpleCmdRequest\nThread : ................: main\nExpected prompt..........: ^((?![&lt;?]|\\\\.{3}|(\\\\S+\\\\s){6,}).)*[&gt;#$%]+(\\\\\\\\u001B\\\\[(\\\\d;?)*[m|n|K])*\\\\s(\\\\\\\\u001B\\\\[(\\\\d;?)*[m|n|K])*$|#\\\\s\\\\u001B\\\\[6n\nPrompt forced............: false\nTimeout..................: 20000ms\nSyntax...................: lsb_release -i\n"
     
           document.getElementById("test").append(message);
        
        function copy() {
        var res = message.split("\n");
        for (var i in res) {
            if (res[i].indexOf("Syntax") != -1) {
                var syntax = res[i].split(':');
                var syntaxClean = syntax[1].slice(1);
                console.log(syntaxClean);
                copyToClipboard(syntaxClean);
            }
        }
        }
        
        function copyToClipboard(text) {
    var dummyElm = document.createElement("textarea");
    document.body.appendChild(dummyElm);
    dummyElm.value = text;
    dummyElm.select();
    document.execCommand("copy");
    document.body.removeChild(dummyElm);
}
    </script>
</body>

</html>
Tom Marienfeld
  • 686
  • 3
  • 13