MCPcopy Create free account
hub / github.com/bugy/script-server / copyToClipboard

Function copyToClipboard

web-src/src/common/utils/common.js:573–627  ·  view source on GitHub ↗
(elem)

Source from the content-addressed store, hash-verified

571}
572
573export function copyToClipboard(elem) {
574 // if new Clipboard API is available, just use it
575 if (navigator.clipboard) {
576 const selectionString = readUserVisibleText(elem);
577 navigator.clipboard.writeText(selectionString);
578 return;
579 }
580
581 const targetId = '_hiddenCopyText_';
582 const isInput = elem.tagName === 'INPUT' || elem.tagName === 'TEXTAREA';
583
584 let origSelectionStart, origSelectionEnd;
585 let target;
586 if (isInput) {
587 target = elem;
588 origSelectionStart = elem.selectionStart;
589 origSelectionEnd = elem.selectionEnd;
590 } else {
591 target = document.getElementById(targetId);
592 if (!target) {
593 target = document.createElement('textarea');
594 target.style.position = 'absolute';
595 target.style.left = '9999px';
596 target.style.top = '0';
597 target.id = targetId;
598 document.body.appendChild(target);
599 }
600 target.value = readUserVisibleText(elem);
601 }
602
603 const currentFocus = document.activeElement;
604 target.focus();
605 target.setSelectionRange(0, target.value.length);
606
607 try {
608 document.execCommand('copy');
609 } catch (e) {
610 console.error(e);
611 }
612
613 if (currentFocus && typeof currentFocus.focus === 'function') {
614 currentFocus.focus();
615 }
616
617 if (isInput) {
618 elem.setSelectionRange(origSelectionStart, origSelectionEnd);
619 } else {
620 target.textContent = '';
621 }
622
623 document.body.removeChild(target);
624
625 // for mobiles, we need to scroll down to make URL bar disappear
626 elem.scrollIntoView();
627}
628
629export function uuidv4() {
630 return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>

Callers

nothing calls this directly

Calls 1

readUserVisibleTextFunction · 0.85

Tested by

no test coverage detected