Generate Password Button

17.11.2020, 20:59:04

  1. // ==UserScript==
  2. // @name Generate Password Button
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*/*
  5. // @grant none
  6. // @version 1.1
  7. // @author Degreet Pro <degreetpro@gmail.com>
  8. // @description 17.11.2020, 20:59:04
  9. // ==/UserScript==
  10.  
  11. const chars = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!@$%^&*()_-+"
  12.  
  13. const styles = document.createElement("style")
  14. document.head.append(styles)
  15.  
  16. styles.innerHTML = `
  17. .generate-password-violentmonkey-script-button {
  18. position: fixed;
  19. right: 20px;
  20. bottom: 0;
  21. background: #fff;
  22. width: 60px;
  23. height: 60px;
  24. border-top-left-radius: 50%;
  25. border-top-right-radius: 50%;
  26. font-size: 30px;
  27. border: 1px solid #bfbfbf;
  28. opacity: .2;
  29. display: flex;
  30. justify-content: center;
  31. align-items: center;
  32. outline: none;
  33. z-index: 999;
  34. transform: translateY(50px);
  35. transition: .3s;
  36. }
  37.  
  38. .generate-password-violentmonkey-script-button:hover {
  39. opacity: 1;
  40. transform: translateY(20px);
  41. }
  42.  
  43. .generate-password-violentmonkey-script-button.success {
  44. border: 1px solid green;
  45. color: green;
  46. }
  47.  
  48. .generate-password-violentmonkey-script-button.warning {
  49. border: 1px solid orange;
  50. color: orange;
  51. }
  52. `
  53.  
  54. const genPwdBtn = document.createElement("button")
  55. genPwdBtn.className = "generate-password-violentmonkey-script-button"
  56. genPwdBtn.innerText = "+"
  57. document.body.append(genPwdBtn)
  58.  
  59. genPwdBtn.onclick = () => {
  60. const {hostname} = location
  61. if (localStorage[hostname]) {
  62. const password = localStorage[hostname]
  63. navigator.clipboard.writeText(password)
  64. genPwdBtn.classList.add("warning")
  65. setTimeout(() => genPwdBtn.classList.remove("warning"), 1000)
  66. } else {
  67. const password = generatePassword()
  68. navigator.clipboard.writeText(password)
  69. localStorage[hostname] = password
  70. genPwdBtn.classList.add("success")
  71. setTimeout(() => genPwdBtn.classList.remove("success"), 1000)
  72. }
  73. }
  74.  
  75. function generatePassword() {
  76. let pass = ""
  77. for (let i = 0; i < 16; i++) pass += chars[Math.floor(Math.random() * chars.length)]
  78. return pass
  79. }