How can a ui-template node reliably copy a simple text string to the clipboard?
The following JS code in the ui-template (Dashboard2) works with a button within the ui-template.
However, it is necessary for the ui-template node to receive the text string via a msg also. In the JS code, a msg is received via watch. This is confirmed by console.log.
But the text string is not copied to the clipboard. The alert function says: “undefined”.
The console outputs:
“document.execCommand('cut'/'copy') wurde abgelehnt, weil es nicht von innerhalb einer kurz dauernden benutzergenerierten Ereignisbehandlung aufgerufen wurde.”.
deepl says:
“document.execCommand(‘cut’/'copy') was rejected because it was not called from within a short-lived user-generated event handler.”
[{"id":"71258c04f9bf025c","type":"inject","z":"b25c3ffa5d02edb9","name":"copyThis2Clip","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"copyThis2Clip","payload":"","payloadType":"date","x":230,"y":1180,"wires":[["2377519e41010f8f"]]},{"id":"2377519e41010f8f","type":"ui-template","z":"b25c3ffa5d02edb9","group":"cf2110ce4840bb3c","page":"","ui":"","name":"copy2clip_test","order":1,"width":0,"height":0,"head":"","format":"<template>\n \n <span> copy2Clip_2026-01-05</span>\n <v-btn color=\"primary\" @click=\"this.cClip('hello ')\">\n hello cClip\n </v-btn>\n</template>\n\n<script>\n export default {\n data() {\n return {\n ctext: \"testpattern\"\n };\n },\n\n watch: {\n msg(newMsg) {\n if (!newMsg) return;\n\n if (newMsg.topic === 'copyThis2Clip') {\n const test = (`watch:: ${new Date(newMsg.payload)}`)\n this.ctext = test;\n\n console.log(test);\n // this.cClip(test);\n\n setTimeout(() => {\n this.cClip(this.ctest);\n }, 100);\n newMsg = null;\n }\n }\n },\n\n methods: {\n\n cClip(test) {\n //copy_text(input) {\n // Get the text field\n // copyText = document.getElementById(input).innerText;\n var dummy = document.createElement(\"textarea\");\n document.body.appendChild(dummy);\n dummy.value = test;\n dummy.select();\n document.execCommand(\"copy\");\n document.body.removeChild(dummy);\n alert(\"Copied Data to Clipboard\\n\\n\" + test)\n },\n }\n }\n</script>","storeOutMessages":true,"passthru":false,"resendOnRefresh":true,"templateScope":"local","className":"","x":440,"y":1180,"wires":[[]]},{"id":"cf2110ce4840bb3c","type":"ui-group","name":"Gruppe 1","page":"eb8696622e5576eb","width":"6","height":"1","order":1,"showTitle":false,"className":"","visible":"true","disabled":"false","groupType":"default"},{"id":"eb8696622e5576eb","type":"ui-page","name":"xPlorer_1","ui":"2f9f3adcdf082281","path":"/page6","icon":"home","layout":"grid","theme":"10d4154b82c88eef","breakpoints":[{"name":"Default","px":"0","cols":"3"},{"name":"Tablet","px":"576","cols":"6"},{"name":"Small Desktop","px":"768","cols":"9"},{"name":"Desktop","px":"1024","cols":"12"}],"order":4,"className":"","visible":true,"disabled":false},{"id":"2f9f3adcdf082281","type":"ui-base","name":"My Dashboard","path":"/dashboard","appIcon":"","includeClientData":true,"acceptsClientConfig":["ui-notification","ui-control"],"showPathInSidebar":false,"headerContent":"page","navigationStyle":"default","titleBarStyle":"default","showReconnectNotification":true,"notificationDisplayTime":"5","showDisconnectNotification":true,"allowInstall":true},{"id":"10d4154b82c88eef","type":"ui-theme","name":"Default Theme","colors":{"surface":"#ffffff","primary":"#0094CE","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"},"sizes":{"density":"default","pagePadding":"12px","groupGap":"12px","groupBorderRadius":"4px","widgetGap":"12px"}}]
Any advice?