Password Generator

Generate random passwords with Context Menu

  1. // ==UserScript==
  2. // @name Password Generator
  3. // @name:tr Şifre Oluşturucu
  4. // @namespace https://aktolu.com/
  5. // @version 0.6
  6. // @description Generate random passwords with Context Menu
  7. // @description:tr Sağ tık menüsünü kullanarak rastgele şifreler oluşturun
  8. // @author Muhammed Aktolu
  9. // @match *://*/*
  10. // @icon https://static-00.iconduck.com/assets.00/security-password-icon-1959x2048-sq0rdvok.png
  11. // @grant GM_addElement
  12. // @grant GM_addStyle
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_setClipboard
  17. // ==/UserScript==
  18.  
  19. /* jshint esversion: 11 */
  20.  
  21. (function() {
  22. 'use strict';
  23. // Start Global
  24. const toast = function(text, type = 'success') {
  25. document.getElementById('myToast')?.remove();
  26. document.getElementById('myToastStyle')?.remove();
  27. // language=css
  28. let styleString = `
  29. #myToast {
  30. display: inline-block !important;
  31. padding: 20px !important;
  32. opacity: .6 !important;
  33. color: white !important;
  34. background-color: black !important;
  35. position: fixed !important;
  36. top: 20px !important;
  37. right: 20px !important;
  38. z-index: 9999999999999999999999 !important;
  39. }
  40. #myToast.success {
  41. background-color: green !important;
  42. }
  43. #myToast.warning {
  44. background-color: orange !important;
  45. }
  46. #myToast.danger {
  47. background-color: darkred !important;
  48. }
  49. `;
  50. let style = multilineCss(styleString, 'myToastStyle');
  51. let block = GM_addElement(document.body, 'div', {
  52. id: 'myToast',
  53. class: type,
  54. });
  55. block.innerText = text;
  56. setTimeout(() => {
  57. block.remove();
  58. style.remove();
  59. }, 5000);
  60. }
  61. const multilineCss = function(style, id = '') {
  62. let el = GM_addStyle(style);
  63. if (id !== '') {
  64. el.id = id;
  65. }
  66. return el;
  67. }
  68. // End Global
  69. // Start Password Generator
  70. const generatePassword = function(length = 16, minPerType = 2, uppercase = true, lowercase = true, number = true, symbol = false) {
  71. let string = '';
  72. let pass = {
  73. uppercase: true,
  74. lowercase: true,
  75. number: true,
  76. symbol: true,
  77. }
  78. let match;
  79. if (uppercase) {
  80. string += 'ABCDEFGHJKMNPQRSTUVWXYZ';
  81. }
  82. if (lowercase) {
  83. string += 'abcdefghjkmnpqrstuvwxyz';
  84. }
  85. if (number) {
  86. string += '23456789';
  87. }
  88. if (symbol) {
  89. string += '!";#$%&\'()*+,-./:;<=>?@[]^_`{|}~';
  90. }
  91. let password = Array.from(crypto.getRandomValues(new Uint32Array(length)))
  92. .map((x) => string[x % string.length])
  93. .join('');
  94. if (uppercase) {
  95. match = password.match(/[A-Z]/g);
  96. if (!match || match.length < minPerType) {
  97. pass.uppercase = false;
  98. }
  99. }
  100. if (lowercase) {
  101. match = password.match(/[a-z]/g);
  102. if (!match || match.length < minPerType) {
  103. pass.lowercase = false;
  104. }
  105. }
  106. if (number) {
  107. match = password.match(/\d/g);
  108. if (!match || match.length < minPerType) {
  109. pass.number = false;
  110. }
  111. }
  112. if (symbol) {
  113. match = password.match(/[^A-Za-z0-9]/g);
  114. if (!match || match.length < minPerType) {
  115. pass.symbol = false;
  116. }
  117. }
  118. if (password.match(/(.)\1/) || !pass.uppercase || !pass.lowercase || !pass.number || !pass.symbol) {
  119. return generatePassword(length, minPerType, uppercase, lowercase, number, symbol);
  120. } else {
  121. return password;
  122. }
  123. }
  124. // End Password Generator
  125. GM_registerMenuCommand('Generate Password', function() {
  126. if (document.getElementById('pgBlock')) {
  127. return false;
  128. }
  129. //let length = GM_getValue('passwordGeneratorLength', '16');
  130. let password = generatePassword(GM_getValue('pgLength', 16), GM_getValue('pgMinPerType', 2), GM_getValue('pgUppercase', true), GM_getValue('pgLowercase', true), GM_getValue('pgNumber', true), GM_getValue('pgSymbol', false));
  131. // language=css
  132. let styleString = `
  133. #pgBlock {
  134. position: fixed !important;
  135. width: 100vw !important;
  136. height: 100vh !important;
  137. z-index: 999999999999999999999 !important;
  138. display: flex !important;
  139. align-items: center !important;
  140. justify-content: center !important;
  141. background-color: rgba(0, 0, 0, .9) !important;
  142. left: 0 !important;
  143. top: 0 !important;
  144. }
  145. #pgBlock * {
  146. font-family: Helvetica, Arial, sans-serif !important;
  147. font-size: 15px !important;
  148. font-weight: 300 !important;
  149. letter-spacing: 0 !important;
  150. line-height: 100% !important;
  151. color: whitesmoke !important;
  152. float: none !important;
  153. border-radius: 0 !important;
  154. }
  155. #pgBlock div {
  156. display: block !important;
  157. }
  158. #pgBlock button {
  159. background-color: red !important;
  160. padding: 10px 30px !important;
  161. border: none !important;
  162. cursor: pointer !important;
  163. }
  164. #pgBlock input {
  165. margin-inline: 0 !important;
  166. font-size: 13px !important;
  167. display: inline !important;
  168. }
  169. #pgBlock input[type="number"] {
  170. background-color: black !important;
  171. border: 1px solid whitesmoke !important;
  172. width: 60px !important;
  173. padding: 1px 0 1px 2px !important;
  174. appearance: auto !important;
  175. }
  176. #pgBlock label {
  177. margin-right: 15px !important;
  178. cursor: pointer !important;
  179. -webkit-user-select: none !important;
  180. user-select: none !important;
  181. }
  182.  
  183. .pgBox {
  184. text-align: center !important;
  185. }
  186.  
  187. #pgPassword {
  188. font-size: 3rem !important;
  189. margin-bottom: 15px !important;
  190. }
  191. `;
  192. let style = multilineCss(styleString);
  193. let block = GM_addElement(document.body, 'div', {
  194. id: 'pgBlock',
  195. });
  196. // language=html
  197. block.innerHTML = `<div class="pgBox">
  198. <div id="pgPassword"></div>
  199. <div>
  200. <button id="pgCopyPassword">Copy</button>
  201. <button id="pgCancelPassword">Cancel</button>
  202. </div>
  203. <div style="margin-top: 100px">
  204. <div>
  205. Length: <input id="pgPasswordLength" type="number" value="`+GM_getValue('pgLength', 16)+`" style="margin-right: 60px" min="8" max="64">
  206. Min Chars Per Type: <input id="pgPasswordMinPerType" type="number" value="`+GM_getValue('pgMinPerType', 2)+`" min="0">
  207. </div>
  208. <div style="margin-top: 10px">
  209. <label>Uppercase: <input id="pgUppercase" type="checkbox"`+(GM_getValue('pgUppercase', true) ? ' checked' : '')+`></label>
  210. <label>Lowercase: <input id="pgLowercase" type="checkbox"`+(GM_getValue('pgLowercase', true) ? ' checked' : '')+`></label>
  211. <label>Number: <input id="pgNumber" type="checkbox"`+(GM_getValue('pgNumber', true) ? ' checked' : '')+`></label>
  212. <label>Symbol: <input id="pgSymbol" type="checkbox"`+(GM_getValue('pgSymbol', false) ? ' checked' : '')+`></label>
  213. </div>
  214. <button id="pgReGeneratePassword" style="margin-top: 25px">Regenerate Password</button>
  215. </div>
  216. </div>`;
  217. let passwordDiv = document.getElementById('pgPassword');
  218. let copyButton = document.getElementById('pgCopyPassword');
  219. let cancelButton = document.getElementById('pgCancelPassword');
  220. let passwordLengthInput = document.getElementById('pgPasswordLength');
  221. let minPerTypeInput = document.getElementById('pgPasswordMinPerType');
  222. let uppercaseCheckBox = document.getElementById('pgUppercase');
  223. let lowercaseCheckBox = document.getElementById('pgLowercase');
  224. let numberCheckBox = document.getElementById('pgNumber');
  225. let symbolCheckBox = document.getElementById('pgSymbol');
  226. let reGenerateButton = document.getElementById('pgReGeneratePassword');
  227. passwordDiv.innerText = password;
  228. copyButton.addEventListener('click', () => {
  229. navigator.clipboard.writeText(password)
  230. .then(r => toast('Password has been copied successfully.'));
  231. block.remove();
  232. style.remove();
  233. });
  234. cancelButton.addEventListener('click', () => {
  235. block.remove();
  236. style.remove();
  237. });
  238. [passwordLengthInput, minPerTypeInput].forEach(item => {
  239. item.addEventListener('click', () => {
  240. item.select();
  241. });
  242. });
  243. reGenerateButton.addEventListener('click', function() {
  244. GM_setValue('pgLength', parseInt(passwordLengthInput.value));
  245. GM_setValue('pgMinPerType', parseInt(minPerTypeInput.value));
  246. GM_setValue('pgUppercase', uppercaseCheckBox.checked);
  247. GM_setValue('pgLowercase', lowercaseCheckBox.checked);
  248. GM_setValue('pgNumber', numberCheckBox.checked);
  249. GM_setValue('pgSymbol', symbolCheckBox.checked);
  250. password = generatePassword(parseInt(passwordLengthInput.value), parseInt(minPerTypeInput.value), uppercaseCheckBox.checked, lowercaseCheckBox.checked, numberCheckBox.checked, symbolCheckBox.checked);
  251. passwordDiv.innerText = password;
  252. });
  253. });
  254. })();