Copy Text To Clipboard When A Chrome Extension’s Browser Action Is Clicked
I want clicking my Chrome extension’s browser action button to copy some text, but the methods used on websites don’t work. (I realize there are some similar questions, however
Solution 1:
The textarea
element should be added to the live DOM first e.g. to document.body
and focused because execCommand
operates on document.activeElement
. You can hide the textarea so it doesn't flicker.
functioncopy(text) {
const ta = document.createElement('textarea');
ta.style.cssText = 'opacity:0; position:fixed; width:1px; height:1px; top:0; left:0;';
ta.value = text;
document.body.appendChild(ta);
ta.focus();
ta.select();
document.execCommand('copy');
ta.remove();
}
Might wanna set a few more CSS props to none
or 0
like border/padding/margin just in case.
Post a Comment for "Copy Text To Clipboard When A Chrome Extension’s Browser Action Is Clicked"