forked from arcanis/secretsanta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·435 lines (281 loc) · 12.2 KB
/
index.html
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Secret Santa Generator</title>
<script src="vendors/Lodash-3.10.1.js"></script>
<script src="vendors/Cryptojs.aes-3.1.2.js"></script>
<script src="./SecretSanta.js"></script>
<style>
* {
box-sizing: border-box;
}
html, body, .main {
margin: 0;
width: 100%;
height: 100%;
padding: 0;
}
body {
background-image: url(assets/santa.png);
background-position: bottom right;
background-repeat: no-repeat;
font-family: 'Comic Sans MS';
}
.main {
display: flex;
}
.part {
display: flex;
flex-direction: column;
padding: 20px;
flex: 1;
}
.instructions {
margin: auto 10%;
}
.instructions a {
text-decoration: none;
color: #0CB50C;
}
.input {
display: block;
align-self: stretch;
width: 100%;
flex: auto;
border: 10px solid #0CB50C;
border-radius: 10px;
padding: 15px;
background-color: rgba(255, 255, 255, .7);
outline: none;
}
.generate {
display: block;
margin-top: 20px;
flex: none;
height: 100px;
border: 10px solid #0CB50C;
border-radius: 10px;
font-family: inherit;
font-size: 40px;
background-color: #EA1212;
color: #FFFFFF;
}
.generate:focus {
border-color: #E6E020;
outline: none;
}
.result {
margin-top: 20px;
flex: none;
border: 10px solid #E6E020;
border-radius: 10px;
padding: 15px;
background: #FFFFFF;
}
.result a {
color: blue;
}
.result.none {
display: none;
}
.result.error {
border-color: #EA1212;
color: #EA1212;
}
.result-table {
table-layout: fixed;
width: 100%;
}
.result-name {
width: 30%;
padding: 5px 8px;
}
.result-link {
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
</style>
</head>
<body>
<div class="main">
<div class="part" style="overflow-y: auto">
<div class="instructions">
<h1><img src="assets/mistletoe.png" style="vertical-align: middle" /> Secret Santa Generator</h1>
<p>No signup, no email, no bullshit. Just a straightforward <a href="https://github.com/jacobwoffenden/secretsanta">open-source</a> tool to help you generate your secret santa pairings. One static page, and that's it.</p>
<p>In the most common case (no exclusion rules, pair each guest with another at random), enter the name of your guests one line at a time. Once done, press "generate" and you're all set: send the generated links to your guests (by mail, chat, whatever floats your boat) and their pairing will be revealed to them (and only them) once they open the link.</p>
<h2>Where does this tool come from?</h2>
<p>I wanted to make a Secret Santa over Facebook without having to reveal to anyone my guests email addresses (so nothing that would require a backend). I also wanted not to know who was paired with me, so I had to find a way to somehow obfuscate the information. And being a developer, well, my first thought was "Let's AES it, for fun and profits!". Classic.</p>
</div>
</div>
<form id="form" class="part">
<textarea id="input" class="input" autofocus></textarea>
<button type="submit" class="generate">Generate your pairings</button>
<div id="result" class="result none"></div>
</form>
</div>
<script id="input-placeholder" type="text/placeholder">
# You can add a user by adding a line
Santa
# You can add some details if you want to, using parentheses after the name
Nicholas (the elf)
# You can prevent someone from being paired with someone else
Maël !Aurélie
Aurélie !Maël
# You can also exclude someone from being paired with multiple people
# Careful: too many exclusion rules can make your secret santa less interesting!
Rudolph !Santa !Nicholas (the elf)
# You can also cheat a bit and force someone to be paired with another
Nicholas (the saint) =Nicholas (the elf)
...
</script>
</body>
<script>
function isAmazonEnabled() {
const checkBox = document.getElementById('amazon');
return checkBox ? checkBox.checked : false;
}
function persist() {
if ( ! window.localStorage )
return ;
var amazon = isAmazonEnabled();
var content = document.getElementById( 'input' ).value;
window.localStorage.setItem( 'amazon', amazon );
window.localStorage.setItem( 'input', content );
}
function restore() {
if ( ! window.localStorage )
return ;
var amazon = window.localStorage.getItem( 'amazon' );
var content = window.localStorage.getItem( 'input' );
if (typeof amazon === 'undefined')
amazon = true;
if (typeof content === 'undefined')
content = '';
if ( document.getElementById( 'amazon' ) )
document.getElementById( 'amazon' ).checked = amazon;
document.getElementById( 'input' ).value = content;
}
function reset() {
var result = document.getElementById( 'result' );
result.classList.add( 'none' );
result.classList.remove( 'error' );
}
function error(text) {
var result = document.getElementById( 'result' );
result.classList.remove( 'none' );
result.classList.add( 'error' );
result.innerText = text;
}
function success( pairings ) {
var result = document.getElementById( 'result' );
result.classList.remove( 'none' );
result.classList.remove( 'error' );
result.innerHTML = '';
var table = document.createElement( 'table' );
table.className = 'result-table';
result.appendChild( table );
var names = Object.keys( pairings ).sort();
for ( var t = 0, T = names.length; t < T; ++ t ) {
var name = names[ t ];
var prettyName = names[ t ].replace( /\([^)]+\)/g, ' ' ).replace( / +/g, ' ' ).trim();
var tr = document.createElement( 'tr' );
tr.className = 'result-row';
table.appendChild( tr );
var tdName = document.createElement( 'td' );
tdName.className = 'result-name';
tr.appendChild( tdName );
var tdLink = document.createElement( 'td' );
tdLink.className = 'result-link';
tr.appendChild( tdLink );
var link = document.createElement( 'a' );
tdLink.appendChild( link );
var key = String( _.random( 0x0000, 0xFFFF ) );
var encryptedPairing = CryptoJS.AES.encrypt( pairings[ name ], key );
var pairingPath = window.location.pathname.replace( /[^/]+$/, '' ) + 'pairing.html';
var pairingQueryString = '?name=' + encodeURIComponent( prettyName ) + '&key=' + encodeURIComponent( key ) + '&pairing=' + encodeURIComponent( encryptedPairing );
if ( isAmazonEnabled() )
pairingQueryString += '&extra=1';
tdName.innerText = name;
link.addEventListener( 'click', protect );
link.setAttribute( 'data-name', name );
link.href = window.location.protocol + '//' + window.location.host + pairingPath + pairingQueryString;
link.target = '_blank';
link.innerText = link.href;
}
}
function updateAmazon() {
var result = document.getElementById( 'result' );
var links = result.getElementsByTagName( 'a' );
for ( var t = 0; t < links.length; ++t ) {
var link = links[t];
if ( isAmazonEnabled() )
link.href += '&extra=1';
else
link.href = link.href.replace( /&extra=[01]/, '' );
link.innerText = link.href;
}
}
function generate( e ) {
e.preventDefault();
var content = document.getElementById( 'input' ).value;
// Convert carriage returns into line feeds
content = content.replace( /(\r\n|\r)/g, '\n' );
// Merge adjacent blank characters into a single space
content = content.replace( /[ \t]+/g, ' ' );
// Trim lines
content = content.replace( /^ | $/gm, '' );
// Strip comments
content = content.replace( /^#.*$/gm, '' );
// Strip empty lines
content = content.replace( /\n+/g, '\n' );
// Remove leading/trailing newlines
content = content.replace( /^\n|\n$/g, '' );
var lines = content.split( /\n/g );
if ( lines.length === 0 || lines.length === 1 && lines[ 0 ].length === 0 )
return reset();
var santa = new SecretSanta();
for ( var t = 0, T = lines.length; t < T; ++ t ) {
var match = lines[ t ].match( /^((?:(?![!=]).)+)((?: [!=](?:(?! [!=]).)+)*)$/ );
if ( ! match )
return error( 'Syntax error: "' + lines[ t ] + '" isn\'t valid' );
var name = match[ 1 ];
var rules = match[ 2 ] ? match[ 2 ].match(/[!=][^!=]+/g) : null;
var person = santa.add( name );
if (rules) {
for ( var u = 0, U = rules.length; u < U; ++ u ) {
var fnName = {
'=': 'enforce',
'!': 'blacklist'
}[ rules[ u ].charAt( 0 ) ];
person[ fnName ]( rules[ u ].slice( 1 ).trim() );
}
}
}
try {
return success( santa.generate() );
} catch ( err ) {
console.error(err.stack)
return error( err.message );
}
}
function protect( e ) {
var name = e.currentTarget.getAttribute( 'data-name' );
if ( ! confirm( 'If you click this link, you will be revealed ' + name + '\'s pairing! Are you sure you want to do this? Only do this if you\'re actually ' + name + '.\n\nUse right-click to copy the link target instead.' ) ) {
e.preventDefault();
}
}
</script>
<script>
document.getElementById( 'input' ).placeholder = document.getElementById( 'input-placeholder' ).innerHTML.trim().replace( /^[ \t]+/gm, '' );
document.getElementById( 'input' ).addEventListener( 'change', persist );
if ( document.getElementById( 'amazon' ) ) {
document.getElementById( 'amazon' ).addEventListener( 'change', updateAmazon );
document.getElementById( 'amazon' ).addEventListener( 'change', persist );
}
document.getElementById( 'form' ).addEventListener( 'submit', generate );
restore();
</script>
</html>