Files
webextensions-examples/context-menu-copy-link-with-types/clipboard-helper.js
Rob Wu 277ac935fa Add example with advanced clipboard manipulation (#207)
(tested with Firefox 52 and 54)
2017-04-24 14:09:19 -07:00

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");
}