Skip to content

Instantly share code, notes, and snippets.

@segdeha
Created October 6, 2022 16:40
Show Gist options
  • Save segdeha/45d8c4ccae9329f2d94893a57e45b80c to your computer and use it in GitHub Desktop.
Save segdeha/45d8c4ccae9329f2d94893a57e45b80c to your computer and use it in GitHub Desktop.
Bookmarklet: Create a random password and copy it to the pasteboard
javascript:(function()%7Bfunction Types()%7Bthis.characterTypes%3D%5B%27uppercase%27,%27lowercase%27,%27number%27,%27dash%27%5D%3Bthis.limits%3D%7Buppercase:1,lowercase:13,number:1,dash:1%7D%7DTypes.prototype.getValidType%3Dfunction()%7Bconst types%3D%5B%5D%3Bif(this.limits.uppercase>0)%7Btypes.push(%27uppercase%27)%7Dif(this.limits.lowercase>0)%7Btypes.push(%27lowercase%27)%7Dif(this.limits.number>0)%7Btypes.push(%27number%27)%7Dif(this.limits.dash>0)%7Btypes.push(%27dash%27)%7Dconst type%3Dtypes%5BgetRandomNumber(0,types.length-1)%5D%3Breturn type%7D%3BTypes.prototype.decrement%3Dfunction(type)%7Bthis.limits%5Btype%5D-%3D1%3Bif(this.limits%5Btype%5D<0)%7Bthis.limits%5Btype%5D%3D0%7D%7D%3Bfunction getRandomNumber(min,max)%7Breturn Math.floor(Math.random()*(max-min%2B1))%2Bmin%7Dfunction getValidType()%7Breturn(new Types()).getValidType()%7Dfunction getCharacterCode(types,type)%7Blet number%3Bswitch(type)%7Bcase %27uppercase%27:number%3DgetRandomNumber(65,90)%3Btypes.decrement(%27uppercase%27)%3Bbreak%3Bcase %27lowercase%27:number%3DgetRandomNumber(97,122)%3Btypes.decrement(%27lowercase%27)%3Bbreak%3Bcase %27number%27:number%3DgetRandomNumber(48,57)%3Btypes.decrement(%27number%27)%3Bbreak%3Bcase %27dash%27:number%3D45%3Btypes.decrement(%27dash%27)%3Bbreak%3Bdefault:throw %60Invalid type: %24%7B type %7D%60%7Dreturn number%7Dfunction createPassword()%7Bconst types%3Dnew Types()%3Bconst codes%3D%5B%5D%3Bfor(let i%3D0%3Bi<16%3Bi%2B%3D1)%7Blet type%3Dtypes.getValidType()%3Bcodes.push(getCharacterCode(types,type))%7Dconst password%3DString.fromCharCode(...codes)%3Breturn password%7Dfunction copyToPasteboard(str)%7Bnavigator.clipboard.writeText(str).then(()%3D>%7Balert(%27Password successfully copied to pasteboard!%27)%7D,err%3D>console.error)%7Dfunction getPassword()%7Bconst password%3DcreatePassword()%3BcopyToPasteboard(password)%7DgetPassword()%3B%7D)()%3B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment