Random Password Generator

Generate secure random password

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Random Password Generator
// @namespace    https://openuserjs.org/users/burn
// @version      20260207
// @description  Generate secure random password
// @copyright    2026, burn (https://openuserjs.org//users/burn)
// @license      MIT
// @match        *://*/*
// @include      *
// @grant        GM_registerMenuCommand
// @grant        GM.registerMenuCommand
// @grant        GM_setClipboard
// @grant        GM.setClipboard
// ==/UserScript==

// ==OpenUserJS==
// @author burn
// ==/OpenUserJS==

/*jshint esversion: 8 */ 
(function() {
    'use strict';
    const DBG = false;
    const SCRIPT = GM_info.script.name;
    let log = (str) => {
        DBG && console.log(GM_info.script.name + " | " + str);
    };

    function secureRandomIndex(max) {
        const range = 0xFFFFFFFF;
        const limit = range - (range % max);
        const buf = new Uint32Array(1);

        do {
            crypto.getRandomValues(buf);
        } while (buf[0] >= limit);

        return buf[0] % max;
    }

    function pick(str) {
        return str[secureRandomIndex(str.length)];
    }

    Object.defineProperty(Array.prototype, "shuffle", {
        value: function() {
            for (let i = this.length - 1; i > 0; i--) {
                const j = secureRandomIndex(i + 1);
                [this[i], this[j]] = [this[j], this[i]];
            }
            return this;
        },
        enumerable: false
    });

    function generatePassword() {
        const LEN = 19;
        const LOWER = "abcdefghijkmnopqrstuvwxyz";
        const UPPER = "ABCDEFGHJKLMNPQRSTUVWXYZ";
        const NUM = "23456789";
        const SYM = "!@#$%^&*()-_=+[]{};:,.<>?";

        const ALL = LOWER + UPPER + NUM + SYM;

        const result = new Array(
            pick(LOWER),
            pick(UPPER),
            pick(NUM),
            pick(SYM)
        );

        for (let i = result.length; i < LEN; i++) {
            result.push(pick(ALL));
            result.shuffle();
        }

        if (typeof GM_setClipboard !== "undefined") {
            GM_setClipboard(result.join(""), "text");
        } else if (typeof GM !== "undefined" && GM.setClipboard) {
            GM.setClipboard(result.join(""), "text");
        }
    }

    if (typeof GM_registerMenuCommand !== "undefined") {
        GM_registerMenuCommand("Generate Secure Password", generatePassword);
    } else if (typeof GM !== "undefined" && GM.registerMenuCommand) {
        GM.registerMenuCommand("Generate Secure Password", generatePassword);
    }
})();