1var titlebartext = "PasswordGenerator";
2
3
4var removeSelection = JSINFO.plugin_passwordgenerator_removeSelection,
5    generator1Length = JSINFO.plugin_passwordgenerator_generator1Length,
6    selectioncache = "";
7
8if (removeSelection === 1) {
9		titlebartext = "Passwordgenerator (remove selection enabled): Generate a new password. If any text is selected a validation will be made and if necessary a new password will be generated.";
10} else {
11	titlebartext = "Passwordgenerator (remove selection disabled): Generate a new password. If any text is selected a validation will be made and if necessary a new password will be generated. When you click again and the selection still has the same value (generated or validated) you will get a new password.";
12}
13
14
15function addBtnActionPasswordgenerator($btn, props, edid) {
16    // base code for passwordgenerator was from https://jpvalappil.wordpress.com/2010/07/02/javascript-password-generator/
17
18
19    function generatePassword(type, plen){
20    // content of spl again in regex some lines below!
21    var lwrAlph = JSINFO.plugin_passwordgenerator_charset1,
22        uprAlph = JSINFO.plugin_passwordgenerator_charset2,
23        nums = JSINFO.plugin_passwordgenerator_charset3,
24        spl = JSINFO.plugin_passwordgenerator_charset4,
25        passwd = [],
26        maxLen = 2048,
27        defLen = 10,
28        minLen = 1;
29
30
31
32    /*Parameter Manipulations */
33    type = type || "all";
34    type = isNaN(type)?type.toLowerCase():"all";
35    plen = plen || defLen;
36    plen = (plen < 0?defLen:(plen <= maxLen? (plen < minLen?defLen:plen): maxLen));
37
38    /*Choosing the password source characters*/
39    src = type === "alpha"? [lwrAlph, uprAlph]:type === "alphanum"?[lwrAlph, uprAlph, nums]:[lwrAlph, uprAlph, nums, spl];
40    /*Password construction*/
41    for (var i = 0; i < plen; i++) {
42        var rnd = Math.floor(Math.random() * src.length),
43            charBuild = src[rnd].split("");
44        /*force only one special char*/
45        if (rnd === 3) {src = [lwrAlph, uprAlph, nums]};
46            rnd = Math.floor(Math.random() * charBuild.length);
47        passwd.push(charBuild[rnd]);
48    }
49    return passwd.join("");
50    }
51
52
53
54    $btn.click(function() {
55        // your click handler
56    var opts;
57    var selection = DWgetSelection(jQuery('#'+edid)[0]);
58
59
60    // is something selected?
61        sample = selection.getText();
62
63
64        if (removeSelection===1) {
65            opts = {nosel: true};
66        } else {
67            opts = {nosel: false};
68            if (sample === selectioncache) {sample = "";}
69        };
70
71    // Generate password with atleast one of this chars and replace selection only not matching this expression
72    while (sample.search(/[!@#$%*()_=+,.:]/) < 0) {
73        sample = generatePassword('all',generator1Length);
74}
75    pasteText(selection,sample,opts);
76    selectioncache = sample;
77        return false;
78    });
79
80    return 'click';
81}
82
83// add a new toolbar button, but first check if there is a toolbar
84if (typeof window.toolbar !== 'undefined') {
85    window.toolbar[window.toolbar.length] = {
86        type: "passwordgenerator",
87        title: titlebartext,
88        key: "g",
89        icon: "../../plugins/passwordgenerator/toolbaricon_password.png"
90    };
91}
92