Skip to content

Instantly share code, notes, and snippets.

@neotam
Last active March 2, 2019 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neotam/7a7b2809515aeae244bb6e27be872fc3 to your computer and use it in GitHub Desktop.
Save neotam/7a7b2809515aeae244bb6e27be872fc3 to your computer and use it in GitHub Desktop.
function writeToClipboardOnPermission(text){
navigator.permissions.query({name:'clipboard-write'})
.then(
result => {
if (result.state == 'granted' || result.state == 'prompt'){
writeToClipboard(text);
}
else {
console.log("Don't have permissions to use clipboard", result.state);
}
}
)
.catch(
err => {
console.log("Error! Reqeusting permission", err)
}
)
}
function writeToClipboard(text) {
navigator.clipboard.writeText(text).then(
result => {
console.log("Successfully copied to clipboard", result)
}
)
.catch(
err => {
console.log("Error! could not copy text", err)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment