Skip to content

Commit 41773b8

Browse files
Update code to fetch products.
1 parent 22ce7d4 commit 41773b8

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

cloudflare-workers/gumroad-products/worker.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,7 @@ export default {
158158

159159
const html = await upstream.text();
160160

161-
// Extract hrefs → /l/<slug>
162-
const hrefs = [];
163-
const reHref = /href\s*=\s*["']([^"']+)["']/gi;
164-
let m;
165-
while ((m = reHref.exec(html)) !== null) hrefs.push(m[1]);
166-
167-
const seen = new Set();
168-
const products = [];
169-
for (const href of hrefs) {
170-
let absolute;
171-
try { absolute = new URL(href, profileUrl).toString(); } catch { continue; }
172-
const mm = absolute.match(/\/l\/([A-Za-z0-9-_]+)\/?$/);
173-
if (!mm) continue;
174-
const slug = mm[1];
175-
if (seen.has(slug)) continue;
176-
seen.add(slug);
177-
const title = slug.replace(/[-_]+/g, " ").trim();
178-
products.push({ slug, url: absolute, title });
179-
}
180-
181-
products.sort((a, b) => a.slug.localeCompare(b.slug));
161+
const products = extractProducts(html);
182162

183163
const payload = {
184164
username: u,
@@ -214,3 +194,31 @@ export default {
214194
});
215195
},
216196
};
197+
198+
function extractProducts(html) {
199+
// Lightweight HTML parsing without DOM: heuristic regex over links.
200+
// For more robustness, you could use an HTML parser lib with Workers Bundler.
201+
const linkRe = /<a\b[^>]*href=["']([^"']*\/l\/[^"']*)["'][^>]*>([\s\S]*?)<\/a>/gi;
202+
const tagRe = /<\/?[^>]+>/g;
203+
const nbspRe = /&nbsp;/g;
204+
205+
const seen = new Set();
206+
const items = [];
207+
let m;
208+
while ((m = linkRe.exec(html)) !== null) {
209+
const href = m[1];
210+
let title = m[2].replace(tagRe, '').replace(nbspRe, ' ').trim();
211+
if (!title) continue;
212+
213+
// Normalize absolute vs relative
214+
const url = href.startsWith('http') ? href : new URL(href, 'https://example.com').href;
215+
const slug = url.split('/').filter(Boolean).pop();
216+
217+
const key = url + '|' + title;
218+
if (seen.has(key)) continue;
219+
seen.add(key);
220+
221+
items.push({ title, url, slug });
222+
}
223+
return items;
224+
}

0 commit comments

Comments
 (0)