mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
19 lines
736 B
JavaScript
19 lines
736 B
JavaScript
// This function must be called in a visible page, such as a browserAction popup
|
|
// or a content script. Calling it in a background page has no effect!
|
|
function copyToClipboard(text, html) {
|
|
function oncopy(event) {
|
|
document.removeEventListener("copy", oncopy, true);
|
|
// Hide the event from the page to prevent tampering.
|
|
event.stopImmediatePropagation();
|
|
|
|
// Overwrite the clipboard content.
|
|
event.preventDefault();
|
|
event.clipboardData.setData("text/plain", text);
|
|
event.clipboardData.setData("text/html", html);
|
|
}
|
|
document.addEventListener("copy", oncopy, true);
|
|
|
|
// Requires the clipboardWrite permission, or a user gesture:
|
|
document.execCommand("copy");
|
|
}
|