Generate Password Button

17.11.2020, 20:59:04

当前为 2020-11-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Generate Password Button
  3. // @namespace Violentmonkey Scripts
  4. // @match *://*/*
  5. // @grant none
  6. // @version 1.0
  7. // @author -
  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: 20px;
  21. background: #fff;
  22. width: 60px;
  23. height: 60px;
  24. border-radius: 50%;
  25. font-size: 30px;
  26. border: 1px solid #bfbfbf;
  27. opacity: .5;
  28. outline: none;
  29. z-index: 999;
  30. transition: .3s;
  31. }
  32.  
  33. .generate-password-violentmonkey-script-button:hover {
  34. opacity: 1;
  35. }
  36.  
  37. .generate-password-violentmonkey-script-button.success {
  38. border: 1px solid green;
  39. color: green;
  40. }
  41.  
  42. .generate-password-violentmonkey-script-button.warning {
  43. border: 1px solid orange;
  44. color: orange;
  45. }
  46. `
  47.  
  48. const genPwdBtn = document.createElement("button")
  49. genPwdBtn.className = "generate-password-violentmonkey-script-button"
  50. genPwdBtn.innerText = "+"
  51. document.body.append(genPwdBtn)
  52.  
  53. genPwdBtn.onclick = () => {
  54. const {hostname} = location
  55. if (localStorage[hostname]) {
  56. const password = localStorage[hostname]
  57. navigator.clipboard.writeText(password)
  58. genPwdBtn.classList.add("warning")
  59. setTimeout(() => genPwdBtn.classList.remove("warning"), 1000)
  60. } else {
  61. const password = generatePassword()
  62. navigator.clipboard.writeText(password)
  63. localStorage[hostname] = password
  64. genPwdBtn.classList.add("success")
  65. setTimeout(() => genPwdBtn.classList.remove("success"), 1000)
  66. }
  67. }
  68.  
  69. function generatePassword() {
  70. let pass = ""
  71. for (let i = 0; i < 16; i++) pass += chars[Math.floor(Math.random() * chars.length)]
  72. return pass
  73. }