forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgumroad-links.html
More file actions
696 lines (611 loc) · 24.9 KB
/
gumroad-links.html
File metadata and controls
696 lines (611 loc) · 24.9 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gumroad Link Builder</title>
<link rel="stylesheet" href="styles.css">
<style>
:root {
color-scheme: light dark;
}
body {
max-width: 960px;
margin: 0 auto;
padding: clamp(1.5rem, 3vw, 2.75rem) clamp(1rem, 3vw, 2.75rem) clamp(3rem, 4vw, 4rem);
}
header {
margin-bottom: clamp(1.75rem, 3vw, 2.5rem);
}
h1 {
font-size: clamp(2rem, 3.4vw, 2.75rem);
margin: 0 0 0.45rem;
color: var(--tx);
}
p.lead {
margin: 0;
color: var(--tx-2);
font-size: clamp(1rem, 2.2vw, 1.2rem);
}
.layout {
display: grid;
gap: clamp(1.5rem, 3vw, 2.5rem);
}
form.surface {
display: grid;
gap: clamp(1.25rem, 2.5vw, 1.75rem);
padding: clamp(1.25rem, 2.6vw, 2rem);
}
.form-grid {
display: grid;
gap: clamp(1rem, 2vw, 1.25rem);
}
.field-group {
display: grid;
gap: 0.5rem;
}
.field-row {
display: grid;
gap: 0.75rem;
}
.field-row.split {
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
align-items: start;
}
label {
font-weight: 600;
}
input[type="text"],
input[type="url"],
input[type="email"],
input[type="number"],
select,
textarea {
width: 100%;
}
.helper-text {
margin: 0;
color: var(--tx-3);
font-size: 0.92rem;
}
.checkbox-stack {
display: grid;
gap: 0.75rem;
}
.checkbox-stack label {
align-items: flex-start;
gap: 0.6rem;
font-weight: 500;
}
.checkbox-stack label span {
flex: 1;
min-width: 0;
}
.checkbox-stack input[type="checkbox"] {
margin-top: 0.25rem;
flex-shrink: 0;
width: auto;
}
.custom-fields {
display: grid;
gap: 0.75rem;
}
.custom-field-row {
display: grid;
gap: 0.75rem;
}
.custom-field-row .pair {
display: grid;
gap: 0.5rem;
}
@media (min-width: 680px) {
.custom-field-row {
grid-template-columns: minmax(180px, 1fr) minmax(200px, 1fr) auto;
align-items: end;
}
.custom-field-row .pair {
gap: 0.35rem;
}
}
.custom-field-row button {
align-self: center;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
}
.results.surface {
padding: clamp(1.25rem, 2.6vw, 2rem);
display: grid;
gap: 1rem;
}
#generated-link {
font-family: var(--mono-font, "SFMono-Regular", "Menlo", "Consolas", "Liberation Mono", "Courier New", monospace);
min-height: 3.2rem;
}
.table-container {
overflow-x: auto;
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
text-align: left;
padding: 0.65rem 0.5rem;
border-bottom: 1px solid var(--ui-2);
}
th {
font-weight: 600;
color: var(--tx-2);
}
.empty-state {
margin: 0;
color: var(--tx-3);
}
.sr-only {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
@media (min-width: 920px) {
.layout {
grid-template-columns: 1fr minmax(260px, 320px);
}
.results.surface {
height: fit-content;
position: sticky;
top: clamp(1.5rem, 4vw, 3rem);
}
}
</style>
</head>
<body>
<header class="page-header">
<a class="site-link" href="https://tools.mathspp.com/" aria-label="Back to tools.mathspp.com">← tools.mathspp.com</a>
<h1>Gumroad Link Builder</h1>
<p class="lead">Compose Gumroad product URLs with optional checkout parameters to prefill your customer’s
experience.</p>
</header>
<div class="layout">
<form id="gumroad-form" class="surface" autocomplete="off">
<div class="form-grid">
<div class="field-row split">
<div class="field-group">
<label for="username">Gumroad username</label>
<input type="text" id="username" name="username" placeholder="username">
</div>
<div class="field-group">
<label for="product-slug">Product ID</label>
<input type="text" id="product-slug" name="product-slug" placeholder="product">
</div>
<p class="helper-text" style="grid-column: 1 / 3;">
Updating the username and product ID updates the product link.
</p>
</div>
<div class="field-group">
<label for="product-url">Product URL</label>
<input type="url" id="product-url" name="product-url"
placeholder="https://username.gumroad.com/l/product" required>
<p class="helper-text">You can also paste a Gumroad product link to sync the username and product ID
fields automatically.</p>
</div>
<div class="field-group">
<label for="discount-code">Discount code (optional)</label>
<input type="text" id="discount-code" name="discount-code" placeholder="SUMMER25">
</div>
<fieldset class="checkbox-stack">
<legend>Checkout behaviour</legend>
<label for="wanted">
<input type="checkbox" id="wanted" name="wanted">
<span>Take the customer straight to checkout</span>
</label>
<label for="pay_in_installments">
<input type="checkbox" id="pay_in_installments" name="pay_in_installments">
<span>Prefer the installments option when available</span>
</label>
</fieldset>
<div class="field-group">
<label for="frequency">Membership payment frequency</label>
<select id="frequency" name="frequency">
<option value="">No preference</option>
<option value="monthly">Monthly</option>
<option value="quarterly">Quarterly</option>
<option value="biannually">Biannually</option>
<option value="yearly">Yearly</option>
</select>
</div>
<div class="field-group">
<label for="variant">Variant / version / tier</label>
<input type="text" id="variant" name="variant" placeholder="Professional">
<p class="helper-text">Links to a specific variant. The value will be URL encoded for
you.</p>
</div>
<div class="field-row split">
<div class="field-group">
<label for="price">Pay-what-you-want price</label>
<input type="number" id="price" name="price" min="0" step="0.01" placeholder="20">
</div>
<div class="field-group">
<label for="quantity">Quantity</label>
<input type="number" id="quantity" name="quantity" min="1" step="1" placeholder="1">
</div>
</div>
<div class="field-row split">
<div class="field-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="customer@example.com">
</div>
<div class="field-group">
<label for="referrer">Referrer</label>
<input type="url" id="referrer" name="referrer" placeholder="https://twitter.com">
</div>
</div>
<section class="field-group">
<div class="field-row">
<div>
<h2 style="margin: 0; font-size: 1.1rem;">Custom checkout fields</h2>
<p class="helper-text">Provide the exact label from your Gumroad custom field to prefill it.
Only rows with both a label and value are added.</p>
</div>
<div class="actions">
<button type="button" id="add-custom-field">Add custom field</button>
</div>
</div>
<div id="custom-fields" class="custom-fields" aria-live="polite"></div>
</section>
</div>
<div class="actions">
<button type="reset">Reset</button>
</div>
</form>
<section class="surface results" aria-live="polite">
<div>
<h2>Generated link</h2>
<textarea id="generated-link" readonly placeholder="Your URL will appear here"></textarea>
</div>
<div class="actions">
<button type="button" id="copy-button" disabled>Copy link</button>
</div>
<div>
<h3 style="margin: 0; font-size: 1.1rem;">Included parameters</h3>
<div class="table-container">
<table aria-describedby="parameters-description">
<thead>
<tr>
<th scope="col">Parameter</th>
<th scope="col">Value</th>
</tr>
</thead>
<tbody id="parameters-body">
<tr>
<td colspan="2">
<p class="empty-state" id="parameters-description">No query parameters yet. Fill in
the
form to generate them.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>
<template id="custom-field-template">
<div class="custom-field-row">
<div class="pair">
<label class="sr-only" data-role="name-label">Custom field label</label>
<input type="text" data-role="custom-name" placeholder="Custom field label">
</div>
<div class="pair">
<label class="sr-only" data-role="value-label">Custom field value</label>
<input type="text" data-role="custom-value" placeholder="Prefilled value">
</div>
<button type="button" class="remove-field">Remove</button>
</div>
</template>
<script>
(function () {
const form = document.getElementById('gumroad-form');
const output = document.getElementById('generated-link');
const copyButton = document.getElementById('copy-button');
const paramsTableBody = document.getElementById('parameters-body');
const customFieldsContainer = document.getElementById('custom-fields');
const addCustomFieldButton = document.getElementById('add-custom-field');
const template = document.getElementById('custom-field-template');
const productUrlInput = document.getElementById('product-url');
const usernameInput = document.getElementById('username');
const productSlugInput = document.getElementById('product-slug');
let customFieldCount = 0;
let syncingFromParts = false;
let syncingFromUrl = false;
function normaliseBaseUrl(url) {
return url.replace(/\s+/g, '').replace(/\/+$/, '');
}
function sanitiseSlug(value) {
let slug = value.trim();
if (/https?:\/\//i.test(slug)) {
const parsed = parseProductUrl(slug);
if (parsed) {
slug = parsed.slug;
}
}
slug = slug.replace(/^\/+/, '');
if (slug.startsWith('l/')) {
slug = slug.slice(2);
}
const slashIndex = slug.indexOf('/');
if (slashIndex !== -1) {
slug = slug.slice(0, slashIndex);
}
const queryIndex = slug.indexOf('?');
if (queryIndex !== -1) {
slug = slug.slice(0, queryIndex);
}
const hashIndex = slug.indexOf('#');
if (hashIndex !== -1) {
slug = slug.slice(0, hashIndex);
}
return slug;
}
function composeProductUrl(username, slug) {
const cleanUsername = username.trim();
const cleanSlug = sanitiseSlug(slug);
if (!cleanUsername || !cleanSlug) {
return '';
}
return `https://${cleanUsername}.gumroad.com/l/${cleanSlug}`;
}
function parseProductUrl(value) {
if (!value) {
return null;
}
let parsedValue = value.trim();
if (!/^https?:\/\//i.test(parsedValue)) {
parsedValue = `https://${parsedValue}`;
}
try {
const url = new URL(parsedValue);
const hostParts = url.hostname.split('.');
if (hostParts.length < 2 || hostParts.slice(-2).join('.') !== 'gumroad.com') {
return null;
}
const username = hostParts.length > 2 ? hostParts[0] : '';
const path = url.pathname.replace(/^\/+/, '');
const segments = path.split('/');
if (segments[0] !== 'l' || !segments[1]) {
return null;
}
const canonicalBase = normaliseBaseUrl(`${url.origin}${url.pathname}`);
return {
username,
slug: segments[1],
canonicalBase,
};
} catch (error) {
return null;
}
}
function updateUrlFromParts() {
if (syncingFromUrl) {
return;
}
const username = usernameInput.value.trim();
const slug = sanitiseSlug(productSlugInput.value);
if (productSlugInput.value !== slug) {
productSlugInput.value = slug;
}
const composedUrl = composeProductUrl(username, slug);
syncingFromParts = true;
productUrlInput.value = composedUrl;
syncingFromParts = false;
buildLink();
}
function updatePartsFromUrl() {
if (syncingFromParts) {
return;
}
const rawUrl = productUrlInput.value.trim();
const parts = parseProductUrl(rawUrl);
syncingFromUrl = true;
if (parts) {
usernameInput.value = parts.username;
productSlugInput.value = parts.slug;
if (parts.canonicalBase && productUrlInput.value.trim() !== parts.canonicalBase) {
productUrlInput.value = parts.canonicalBase;
}
} else if (!rawUrl) {
usernameInput.value = '';
productSlugInput.value = '';
}
syncingFromUrl = false;
buildLink();
}
function buildLink() {
const formData = new FormData(form);
const rawUrl = (formData.get('product-url') || '').trim();
if (!rawUrl) {
output.value = '';
copyButton.disabled = true;
paramsTableBody.innerHTML = `
<tr>
<td colspan="2">
<p class="empty-state">No query parameters yet. Fill in the form to generate them.</p>
</td>
</tr>
`;
return;
}
let baseUrl = normaliseBaseUrl(rawUrl);
const discountCode = (formData.get('discount-code') || '').trim();
if (discountCode) {
baseUrl = `${baseUrl}/${encodeURIComponent(discountCode)}`;
}
const params = new URLSearchParams();
if (formData.get('wanted')) {
params.set('wanted', 'true');
}
if (formData.get('pay_in_installments')) {
params.set('pay_in_installments', 'true');
}
const frequency = formData.get('frequency');
if (frequency) {
params.set(frequency, 'true');
}
const variant = (formData.get('variant') || '').trim();
if (variant) {
params.set('variant', variant);
}
const email = (formData.get('email') || '').trim();
if (email) {
params.set('email', email);
}
const price = (formData.get('price') || '').trim();
if (price) {
params.set('price', price);
}
const quantity = (formData.get('quantity') || '').trim();
if (quantity) {
params.set('quantity', quantity);
}
const referrer = (formData.get('referrer') || '').trim();
if (referrer) {
params.set('referrer', referrer);
}
const customFieldRows = customFieldsContainer.querySelectorAll('.custom-field-row');
customFieldRows.forEach((row) => {
const nameInput = row.querySelector('[data-role="custom-name"]');
const valueInput = row.querySelector('[data-role="custom-value"]');
const name = nameInput.value.trim();
const value = valueInput.value.trim();
if (name && value) {
params.set(name, value);
}
});
const queryString = params.toString();
let finalUrl = baseUrl;
if (queryString) {
if (/[?&]$/.test(baseUrl)) {
finalUrl = `${baseUrl}${queryString}`;
} else {
const separator = baseUrl.includes('?') ? '&' : '?';
finalUrl = `${baseUrl}${separator}${queryString}`;
}
}
output.value = finalUrl;
copyButton.disabled = !finalUrl;
if (copyButton.disabled) {
copyButton.textContent = 'Copy link';
}
if (queryString) {
const rows = Array.from(params.entries()).map(([key, value]) => `
<tr>
<td><code>${key}</code></td>
<td><code>${value}</code></td>
</tr>
`);
paramsTableBody.innerHTML = rows.join('');
} else {
paramsTableBody.innerHTML = `
<tr>
<td colspan="2">
<p class="empty-state">No query parameters included.</p>
</td>
</tr>
`;
}
}
function addCustomField() {
const clone = template.content.firstElementChild.cloneNode(true);
customFieldCount += 1;
const nameInput = clone.querySelector('[data-role="custom-name"]');
const valueInput = clone.querySelector('[data-role="custom-value"]');
const nameLabel = clone.querySelector('[data-role="name-label"]');
const valueLabel = clone.querySelector('[data-role="value-label"]');
const nameId = `custom-name-${customFieldCount}`;
const valueId = `custom-value-${customFieldCount}`;
nameInput.id = nameId;
valueInput.id = valueId;
nameLabel.setAttribute('for', nameId);
valueLabel.setAttribute('for', valueId);
customFieldsContainer.appendChild(clone);
buildLink();
nameInput.focus();
}
function handleCustomFieldInteraction(event) {
if (event.target.classList.contains('remove-field')) {
event.preventDefault();
const row = event.target.closest('.custom-field-row');
if (row) {
row.remove();
buildLink();
}
}
}
form.addEventListener('input', buildLink);
form.addEventListener('change', buildLink);
form.addEventListener('reset', () => {
window.setTimeout(() => {
customFieldsContainer.innerHTML = '';
usernameInput.value = '';
productSlugInput.value = '';
productUrlInput.value = '';
buildLink();
}, 0);
});
addCustomFieldButton.addEventListener('click', (event) => {
event.preventDefault();
addCustomField();
});
customFieldsContainer.addEventListener('click', handleCustomFieldInteraction);
customFieldsContainer.addEventListener('input', () => {
buildLink();
});
usernameInput.addEventListener('input', updateUrlFromParts);
productSlugInput.addEventListener('input', updateUrlFromParts);
productUrlInput.addEventListener('input', updatePartsFromUrl);
productUrlInput.addEventListener('change', updatePartsFromUrl);
copyButton.addEventListener('click', async () => {
const text = output.value.trim();
if (!text) {
return;
}
try {
await navigator.clipboard.writeText(text);
copyButton.textContent = 'Copied!';
window.setTimeout(() => {
copyButton.textContent = 'Copy link';
}, 1500);
} catch (error) {
output.focus();
output.select();
try {
const successful = document.execCommand('copy');
if (successful) {
copyButton.textContent = 'Copied!';
window.setTimeout(() => {
copyButton.textContent = 'Copy link';
}, 1500);
}
} catch (fallbackError) {
console.error('Copy failed', fallbackError);
}
}
});
// Initialise state on load
updatePartsFromUrl();
buildLink();
})();
</script>
<footer class="page-footer">
<p>Built with ❤️, 🤖, and 🐍, by <a href="https://mathspp.com/">Rodrigo Girão Serrão</a></p>
</footer>
</body>
</html>