Files

36 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2025-01-04 00:34:03 +01:00
/**
* A helper class to manage requesting clipboard permissions and provide common functionality for working with the
* clipboard.
*/
class ClipboardHelper {
constructor() {
if ( game.clipboard instanceof this.constructor ) {
throw new Error("You may not re-initialize the singleton ClipboardHelper. Use game.clipboard instead.");
}
}
/* -------------------------------------------- */
/**
* Copies plain text to the clipboard in a cross-browser compatible way.
* @param {string} text The text to copy.
* @returns {Promise<void>}
*/
async copyPlainText(text) {
// The clipboard-write permission name is not supported in Firefox.
try {
const result = await navigator.permissions.query({name: "clipboard-write"});
if ( ["granted", "prompt"].includes(result.state) ) {
return navigator.clipboard.writeText(text);
}
} catch(err) {}
// Fallback to deprecated execCommand here if writeText is not supported in this browser or security context.
document.addEventListener("copy", event => {
event.clipboardData.setData("text/plain", text);
event.preventDefault();
}, {once: true});
document.execCommand("copy");
}
}