Copy <td> text from table in Ui Template on click

I am trying to get my code to copy a value from text and am using Dashboard V1. When I run the flow I get the following error in the console:

'Uncaught TypeError: Cannot read properties of undefined (reading 'writeText')

Since V1 dashboard uses Angular I am using scope watch to get the data. The function works and displays 'copied' as it should in the ui but the console message indicates I am not getting the value passed to the function correctly.

I don't think I am correctly passing the data to the textToCopy var

  scope.$watch('msg', function(msg) {
    $(document).ready(function() {
      $('td').click(function() {
        if(this.id.startsWith("id")){
          var textToCopy = $(this).text();
          console.log(textToCopy);
        $(this).css("background", "lightblue").text("COPIED");
          setTimeout(()=> {
        $(this).css("background", "white").text(textToCopy);
          },500); navigator.clipboard.writeText(textToCopy);
        }
      });
    });
  });
 })(scope);

I created a quick flow with only 1 Table TD for testing:

[{"id":"9127372381ecb1e8","type":"debug","z":"7432ea42210264ed","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":300,"y":660,"wires":[]},{"id":"51d33c22732a0c54","type":"ui_template","z":"7432ea42210264ed","group":"fa136d15de3a3532","name":"Test Table","order":5,"width":"22","height":"10","format":"<style>\n    .text-font {\n        font-family: sans-serif;\n       // text-align: center;        \n    }\n\n    .styled-table {\n        border-collapse: collapse;\n        margin: .5em;\n        font-size: 0.9em;\n        font-family: sans-serif;\n        box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);\n    }\n\n    .styled-table thead tr {\n        background-color: #009879;\n        color: #ffffff;\n        text-align: left;\n    }\n\n    .styled-table th,\n    .styled-table td {\n        padding: 5px 10px;\n        max-width: .5em;\n        text-align: center;\n        text-wrap: pretty;\n        word-break: break-word;\n    }\n\n    .styled-table tbody tr {\n        border-bottom: 1px solid #dddddd;\n    }\n\n    .styled-table tbody tr:nth-of-type(even) {\n        background-color: #f3f3f3;\n    }\n\n    .styled-table tbody tr:last-of-type {\n        border-bottom: 2px solid #009879;\n    }\n</style>\n\n<script>\n  \n(function(scope) {  \n  scope.$watch('msg.payload', function(obj) {\n    var globalCounter = 0;\n    var tbody = document.getElementById('tbody');\n    $(\"tbody#tbody tr\").remove()\n    for (var i = 0; i < Object.keys(obj).length; i++) {  \n      var tr=\"<tr>\" ;\n      tr +=\"<td id='id-row-\"+ obj[i].id +\"'\" + \">\" + obj[i].id + \"</td>\" + \"</tr>\" ;            \n      tbody.innerHTML +=tr; }        \n    });    \n  })(scope);  \n\n  (function(scope) {\n\n  scope.$watch('msg', function(msg) {\n    $(document).ready(function() {\n      $('td').click(function() {\n        if(this.id.startsWith(\"id\")){\n          var textToCopy = $(this).text();\n          console.log(textToCopy);\n        $(this).css(\"background\", \"lightblue\").text(\"COPIED\");\n          setTimeout(()=> {\n        $(this).css(\"background\", \"white\").text(textToCopy);\n          },500); navigator.clipboard.writeText(textToCopy);\n        }\n      });\n    });\n  });\n })(scope);\n\n  </script>\n\n<table class=\"styled-table\">\n    <thead>\n        <tr>\n            <th>Id </th>\n        </tr>\n    </thead>\n    <tbody id=\"tbody\"></tbody>\n</table>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","className":"","x":460,"y":620,"wires":[[]]},{"id":"2f5bc6921a72af80","type":"template","z":"7432ea42210264ed","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n    {\n        \"id\": 1\n    },\n    {\n        \"id\": 2\n    },\n    {\n        \"id\": 3\n    },\n    {\n        \"id\": 4\n    },\n    {\n        \"id\": 5        \n    }\n]","output":"json","x":300,"y":620,"wires":[["51d33c22732a0c54","9127372381ecb1e8"]]},{"id":"9c57e6716794f5d0","type":"inject","z":"7432ea42210264ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":620,"wires":[["2f5bc6921a72af80"]]},{"id":"fa136d15de3a3532","type":"ui_group","name":"Password","tab":"7c74ed0670b283cd","order":1,"disp":false,"width":"22","collapse":false,"className":""},{"id":"7c74ed0670b283cd","type":"ui_tab","name":"Password List","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Thank you for any advice...

and

implies that navigator.clipboard is undefined

navigator.clipboard requires a secure origin

I.e. you need to either be using HTTPS or localhost.

2 Likes