From 688528440bf0fa9b118a81828f6f283b13ec2727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Soria?= Date: Sun, 18 Sep 2022 14:20:32 -0500 Subject: [PATCH] Remove deprecated execComand function Document.execCommand() has been deprecated and altthough some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes, my alternative uses instead the Clipboard API to copy the generated password to the clipboard --- password-generator/script.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/password-generator/script.js b/password-generator/script.js index ee4fe2b..4f2f9d2 100644 --- a/password-generator/script.js +++ b/password-generator/script.js @@ -15,16 +15,11 @@ const randomFunc = { } clipboardEl.addEventListener('click', () => { - const textarea = document.createElement('textarea') - const password = resultEl.innerText - - if(!password) { return } - - textarea.value = password - document.body.appendChild(textarea) - textarea.select() - document.execCommand('copy') - textarea.remove() + const password = resultEl.innerText; + if (!password) { + return; + } + navigator.clipboard.writeText(password); alert('Password copied to clipboard!') }) @@ -74,4 +69,4 @@ function getRandomNumber() { function getRandomSymbol() { const symbols = '!@#$%^&*(){}[]=<>/,.' return symbols[Math.floor(Math.random() * symbols.length)] -} \ No newline at end of file +}