1const el = document.createElement('textarea');
2el.value = str; //str is your string to copy
3document.body.appendChild(el);
4el.select();
5document.execCommand('copy'); // Copy command
6document.body.removeChild(el);
1// Copy to clipboard in node.js
2const child_process = require('child_process')
3
4// This uses an external application for clipboard access, so fill it in here
5// Some options: pbcopy (macOS), xclip (Linux or anywhere with Xlib)
6const COPY_APP = 'xclip'
7function copy(data, encoding='utf8') {
8 const proc = child_process.spawn(COPY_APP)
9 proc.stdin.write(data, {encoding})
10 proc.stdin.end()
11}
12
1const copyToClipboard = () => {
2
3 navigator.permissions.query({name: "clipboard-write"}).then(result => {
4 if (result.state == "granted" || result.state == "prompt") {
5 // write to the clipboard now
6 updateClipboard('I copy this string');
7 }
8 });
9};
10
11const updateClipboard = (newClip) => {
12
13 navigator.clipboard.writeText(newClip).then(() => {
14 // clipboard successfully set
15 console.log('success');
16 }, () => {
17 // clipboard write failed
18 console.log('Failed to copy');
19 });
20};
21
22const btn = document.getElementById('copy-button');
23
24btn.addEventListener('click', copyHashtagToClipboard);
25
26