Created
July 24, 2026 14:00
-
-
Save nicjansma/5267fc99483065dceb1e7564a52cbc0b to your computer and use it in GitHub Desktop.
Adds loose Minifigs from a BrickLink order to your Brickset Collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 1. Add to your Chrome Snippets list | |
| // 2. Log into BrickLink and open your orderDetail page (https://www.bricklink.com/orderDetail.asp?ID=nnnn) | |
| // 3. Run from Chrome Snippets | |
| // 4. Copy the resulting JavaScript/JSON object in the Console to the next script | |
| // | |
| (() => { | |
| // Match "Minifigure No: XXXXXX" anywhere in the page text of each row | |
| const rows = Array.from(document.querySelectorAll('.mainPrintList tr')); | |
| const figs = []; | |
| const seen = new Map(); | |
| const re = /Minifigure\s*No:\s*([A-Za-z0-9]+)/i; | |
| rows.forEach(tr => { | |
| const text = tr.innerText || tr.textContent || ''; | |
| const m = text.match(re); | |
| if (!m) return; | |
| const no = m[1]; | |
| // Try to find a quantity in the same row. | |
| // BrickLink order rows usually have a "Qty" cell. | |
| let qty = 1; | |
| const cells = Array.from(tr.querySelectorAll('td')).map(td => (td.innerText || '').trim()); | |
| // Heuristic: a standalone small integer cell is likely the quantity | |
| const qtyCandidate = cells.find(c => /^\d{1,4}$/.test(c)); | |
| if (qtyCandidate) qty = parseInt(qtyCandidate, 10); | |
| if (seen.has(no)) { | |
| seen.set(no, seen.get(no) + qty); | |
| } else { | |
| seen.set(no, qty); | |
| } | |
| }); | |
| seen.forEach((qty, no) => figs.push({ minifigNo: no, qty })); | |
| console.log(`Found ${figs.length} distinct minifigures:`); | |
| console.table(figs); | |
| // Copy JSON to clipboard for easy reuse | |
| const json = JSON.stringify(figs, null, 2); | |
| copy(json); // Chrome DevTools console helper | |
| console.log('JSON copied to clipboard:\n' + json); | |
| return figs; | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 1. Add to your Chrome Snippets list | |
| // 2. Modify the API_KEY, USERNAME, PASSWORD and FIGS (from the bricklink-order-get-minifigs.js run) | |
| // 3. Log into BrickSet and open any page | |
| // 4. Run from Chrome Snippets and verify it works (without changes) | |
| // 5. Change DRY_RUN to false and run again | |
| // | |
| (async () => { | |
| // ========= CONFIG ========= | |
| const API_KEY = 'TODO'; | |
| const USERNAME = 'TODO'; | |
| const PASSWORD = 'TODO'; | |
| // Paste the JSON array your BrickLink scraper produced: | |
| const FIGS = [ | |
| { | |
| "minifigNo": "abc001", | |
| "qty": 1 | |
| } | |
| ]; | |
| const DRY_RUN = true; // <-- set to false to actually write changes | |
| // ========================== | |
| const BASE = 'https://brickset.com/api/v3.asmx'; | |
| const post = async (method, fields) => { | |
| const body = new URLSearchParams(fields); | |
| const res = await fetch(`${BASE}/${method}`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
| body | |
| }); | |
| if (!res.ok) throw new Error(`${method} HTTP ${res.status}`); | |
| const data = await res.json(); | |
| if (data.status && data.status !== 'success') { | |
| throw new Error(`${method} failed: ${data.message || JSON.stringify(data)}`); | |
| } | |
| return data; | |
| }; | |
| // 1) Log in -> userHash | |
| console.log('Logging in…'); | |
| const login = await post('login', { | |
| apiKey: API_KEY, | |
| username: USERNAME, | |
| password: PASSWORD | |
| }); | |
| const userHash = login.hash; | |
| if (!userHash) throw new Error('No userHash returned from login: ' + JSON.stringify(login)); | |
| console.log('Logged in.'); | |
| const results = []; | |
| for (const { minifigNo, qty } of FIGS) { | |
| try { | |
| // 2) Read current owned qty | |
| const cur = await post('getMinifigCollection', { | |
| apiKey: API_KEY, | |
| userHash, | |
| params: JSON.stringify({ owned: true }) // returns owned figs; we filter below | |
| }); | |
| // Find this specific minifig in the returned collection | |
| const existing = (cur.minifigs || []).find( | |
| m => (m.minifigNumber || '').toLowerCase() === minifigNo.toLowerCase() | |
| ); | |
| const currentQty = existing ? (existing.ownedInSets + existing.ownedLoose || existing.qtyOwned || 0) : 0; | |
| const newQty = currentQty + qty; | |
| if (DRY_RUN) { | |
| results.push({ minifigNo, currentQty, add: qty, newQty, action: 'DRY_RUN' }); | |
| continue; | |
| } | |
| // 3) Write new total (set-to) | |
| await post('setMinifigCollection', { | |
| apiKey: API_KEY, | |
| userHash, | |
| minifigNumber: minifigNo, | |
| params: JSON.stringify({ qtyOwned: newQty, wanted: false }) | |
| }); | |
| results.push({ minifigNo, currentQty, add: qty, newQty, action: 'UPDATED' }); | |
| } catch (e) { | |
| results.push({ minifigNo, error: e.message, action: 'ERROR' }); | |
| } | |
| // Be polite to the API | |
| await new Promise(r => setTimeout(r, 300)); | |
| } | |
| console.table(results); | |
| console.log(DRY_RUN | |
| ? '\nDRY_RUN is ON — nothing was written. Review the table, then set DRY_RUN = false.' | |
| : '\nDone. Changes written to Brickset.'); | |
| return results; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment