JSFiddle - React, Tailwind, and code Playground
by Sova
HTML
Some text for copy
<div>
<img src onerror="alert()">
SOme other text for copy
</div>
JavaScript
const toClipboard = (selection, text) => {
const el = document.createElement('div');
el.innerText = text;
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
selection.selectAllChildren(el);
setTimeout(() => document.body.removeChild(el), 0);
};
const injectText = 'Some text after copy!!';
window.addEventListener('copy', () => {
const selection = window.getSelection();
const result = `${selection}\n${injectText} Copied from ${location.href}`;
toClipboard(selection, result);
})