-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (106 loc) · 4.04 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var run = function () {
console.log('loading');
document.body.innerHTML = '';
var plainDictionary = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
symbolDictionary = plainDictionary + '!"%C2%A3$%^&*()_+-={}[]:@~;#?,./|\\',
body = document.createElement('div'),
textBox = document.createElement('input'),
createTextNode = 'createTextNode',
createElement = 'createElement',
addButton = function (dictionary, passwordLength) {
var goButton = document.createElement('button');
goButton.appendChild(
document.createTextNode('x' + passwordLength)
);
goButton.onclick = function () {
var randomData = new Uint8Array(passwordLength),
i = randomData.length - 1,
passwordString = ''
;
window.crypto.getRandomValues(randomData);
while (i) {
passwordString += dictionary[randomData[i--] % dictionary.length];
}
textBox.value = passwordString;
textBox.select();
};
body.appendChild(goButton);
},
newline = function () {
appendObj(createElement, 'br');
},
appendObj = function (R, t) {
body.appendChild(document[R](t));
};
var h1 = document.createElement('h1');
h1.appendChild(document.createTextNode('PwGen'));
textBox.setAttribute('readonly', 'readonly');
textBox.setAttribute('aria-label', 'password output field');
var container = document.createElement('div');
container.appendChild(body);
document.body.appendChild(container);
body.onclick = function () {
textBox.select();
};
var goButton = document.createElement('button');
goButton.appendChild(document.createTextNode('reveal'));
goButton.onclick = function () {
textBox.style.width = '300px';
goButton.style.display = 'none';
};
var copied = document.createElement('span');
copied.style.visibility = 'hidden';
copied.appendChild(document.createTextNode('copied!'));
var p = document.createElement('p');
p.innerHTML =
'A <a target="_blank" rel="noopener" href="https://developers.google.com/web/progressive-web-apps">progressive web-app</a> ' +
'<br>' +
'created by ' +
'<a target="_blank" rel="noopener" href="https://stampy.me/2017/09/password-generator-responsive-web-app/">@stampycode</a>.'
;
body.appendChild(h1);
body.appendChild(textBox);
body.appendChild(goButton);
body.appendChild(copied);
newline();
addButton(plainDictionary, 16);
addButton(plainDictionary, 32);
addButton(plainDictionary, 64);
appendObj(createTextNode, ' a-zA-Z0-9');
newline();
addButton(symbolDictionary, 16);
addButton(symbolDictionary, 32);
addButton(symbolDictionary, 64);
appendObj(createTextNode, ' +symbols');
newline();
body.appendChild(p);
// Auto-copy-to-clipboard
textBox.onclick = function() {
textBox.select();
if(!textBox.value) {
return;
}
try {
document.execCommand('copy');
copied.style.visibility = 'visible';
setTimeout(function(){
copied.style.visibility = 'hidden';
}, 1000);
} catch (err) {
}
};
textBox.onfocus = textBox.onclick;
// Service worker for Progressive Web App
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js?v3', {
scope: '.' // THIS IS REQUIRED FOR RUNNING A PROGRESSIVE WEB APP FROM A NON_ROOT PATH
}).then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
};
window.addEventListener('load', run);