-
Notifications
You must be signed in to change notification settings - Fork 11
Supplements: save product URL for future reference #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -358,15 +358,16 @@ function _suppFormHtml(editIdx, s) { | |||||
| const editing = !!s; | ||||||
| const ingredients = editing && s.ingredients ? s.ingredients : []; | ||||||
| const periods = editing ? getSupplementPeriods(s) : [{ start: new Date().toISOString().slice(0, 10), end: null }]; | ||||||
| const sourceUrl = editing && s.sourceUrl ? s.sourceUrl : ''; | ||||||
| return `<div class="supp-form" id="supp-form-panel"> | ||||||
| ${hasAIProvider() ? `<div class="supp-form-row supp-url-row"> | ||||||
| <div class="supp-form-field" style="flex:1"><label>Import from URL</label> | ||||||
| <div class="supp-form-row supp-url-row"> | ||||||
| <div class="supp-form-field" style="flex:1"><label>Product URL <span style="font-weight:normal;color:var(--text-muted)">(saved for reference${hasAIProvider() ? '; Fetch auto-fills' : ''})</span></label> | ||||||
| <div class="supp-url-input-row"> | ||||||
| <input type="url" id="supp-url" placeholder="Paste product page link to auto-fill" autocomplete="off"> | ||||||
| <button class="supp-url-fetch" onclick="fetchSupplementFromURL()">Fetch</button> | ||||||
| <input type="url" id="supp-url" placeholder="https://..." autocomplete="off" value="${escapeHTML(sourceUrl)}"> | ||||||
| ${hasAIProvider() ? `<button class="supp-url-fetch" onclick="fetchSupplementFromURL()">Fetch</button>` : ''} | ||||||
| </div> | ||||||
| </div> | ||||||
| </div>` : ''} | ||||||
| </div> | ||||||
| <div class="supp-form-row"> | ||||||
| <div class="supp-form-field"><label>Name</label> | ||||||
| <input type="text" id="supp-name" placeholder="e.g. Creatine, Metformin" value="${editing ? escapeHTML(s.name) : ''}"> | ||||||
|
|
@@ -461,11 +462,15 @@ export function openSupplementsEditor(editIdx) { | |||||
| const dateRange = pds.length === 1 | ||||||
| ? `${fmtDate(pds[0].start)} \u2192 ${pds[0].end ? fmtDate(pds[0].end) : 'ongoing'}` | ||||||
| : pds.map(p => `${fmtDate(p.start)}\u2192${p.end ? fmtDate(p.end) : 'now'}`).join(' \u00b7 '); | ||||||
| let sourceHost = ''; | ||||||
| if (s.sourceUrl) { | ||||||
| try { sourceHost = new URL(s.sourceUrl).hostname.replace(/^www\./, ''); } catch { sourceHost = ''; } | ||||||
| } | ||||||
| html += `<div class="supp-list-item${isEdit && editIdx === i ? ' supp-list-item-active' : ''}" data-idx="${i}" onclick="toggleSuppAccordion(${i})"> | ||||||
| <span class="supp-list-icon">${icon}</span> | ||||||
| <div class="supp-list-info"> | ||||||
| <div class="supp-list-name">${escapeHTML(s.name)}${s.dosage ? ` <span class="supp-list-meta">${escapeHTML(s.dosage)}</span>` : ''}</div> | ||||||
| <div class="supp-list-meta">${dateRange}</div> | ||||||
| <div class="supp-list-meta">${dateRange}${sourceHost ? ` · <a href="${escapeHTML(s.sourceUrl)}" target="_blank" rel="noopener noreferrer" onclick="event.stopPropagation()" class="supp-list-source">${escapeHTML(sourceHost)} ↗</a>` : ''}</div> | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ${s.ingredients?.length ? `<div class="supp-list-ingredients">${s.ingredients.map(ing => { | ||||||
| const total = ingredientDailyTotal(ing, s); | ||||||
| const times = effectiveTimesPerDay(ing, s); | ||||||
|
|
@@ -540,11 +545,21 @@ export function saveSupplement(idx) { | |||||
| const ingredients = _collectIngredients(); | ||||||
| const timesRaw = document.getElementById('supp-times')?.value.trim(); | ||||||
| const timesNum = timesRaw ? parseFloat(timesRaw) : NaN; | ||||||
| const sourceUrlRaw = document.getElementById('supp-url')?.value.trim() || ''; | ||||||
| let sourceUrl = ''; | ||||||
| if (sourceUrlRaw) { | ||||||
| try { | ||||||
| const parsed = new URL(sourceUrlRaw); | ||||||
| if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { showNotification('Product URL must be http or https', 'error'); return; } | ||||||
| sourceUrl = parsed.toString(); | ||||||
| } catch { showNotification('Invalid product URL', 'error'); return; } | ||||||
| } | ||||||
| if (!state.importedData.supplements) state.importedData.supplements = []; | ||||||
| const entry = { name, dosage, startDate, endDate, type, note }; | ||||||
| if (sorted.length > 1) entry.periods = sorted; | ||||||
| if (ingredients) entry.ingredients = ingredients; | ||||||
| if (isFinite(timesNum) && timesNum > 0) entry.timesPerDay = timesNum; | ||||||
| if (sourceUrl) entry.sourceUrl = sourceUrl; | ||||||
| if (idx >= 0) { | ||||||
| state.importedData.supplements[idx] = entry; | ||||||
| } else { | ||||||
|
|
||||||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hrefusess.sourceUrlwithout a scheme check at render time. WhilesaveSupplementvalidates the protocol,sourceUrlcan also arrive via CRDT sync, JSON import, or directimportedDatamanipulation. A URL of the formjavascript://hostname.com/...would have a non-empty hostname (passing thesourceHosttruthy check) and be rendered as a clickable XSS-capable link. The fix is to add the samehttp:/https:guard inside the existingtryblock that extractssourceHost, so both variables are derived from the same validated parse.