Skip to content

Commit 032e04d

Browse files
committed
asset handling is now website-agnostic
1 parent 8f5c55b commit 032e04d

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

browser-extension/tests/har-view.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,40 @@ app.get('/har/:key/:mode(clean|gitcasso)', async (req, res) => {
146146

147147
// Find the main HTML response
148148
const harData = await loadHar(key)
149+
const originalUrl = PAGES[key]
149150
const mainEntry = harData.log.entries.find(
150151
(entry) =>
151-
entry.request.url.includes('github.com') &&
152+
entry.request.url === originalUrl &&
153+
entry.response.content.mimeType?.includes('text/html') &&
154+
entry.response.content.text,
155+
) || harData.log.entries.find(
156+
(entry) =>
157+
entry.response.status === 200 &&
152158
entry.response.content.mimeType?.includes('text/html') &&
153159
entry.response.content.text,
154160
)
155161
if (!mainEntry) {
156162
return res.status(404).send('No HTML content found in HAR file')
157163
}
158164

165+
// Extract all domains from HAR entries for dynamic replacement
166+
const domains = new Set<string>()
167+
harData.log.entries.forEach(entry => {
168+
try {
169+
const url = new URL(entry.request.url)
170+
domains.add(url.hostname)
171+
} catch {
172+
// Skip invalid URLs
173+
}
174+
})
175+
159176
// Replace external URLs with local asset URLs
160177
let html = mainEntry.response.content.text!
161-
html = html.replace(
162-
/https:\/\/(github\.com|assets\.github\.com|avatars\.githubusercontent\.com|user-images\.githubusercontent\.com)/g,
163-
`/asset/${key}`,
164-
)
178+
domains.forEach(domain => {
179+
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
180+
const regex = new RegExp(`https?://${escapedDomain}`, 'g')
181+
html = html.replace(regex, `/asset/${key}`)
182+
})
165183
if (mode === 'gitcasso') {
166184
html = injectGitcassoScript(key, html)
167185
}
@@ -183,10 +201,27 @@ app.get('/asset/:key/*', async (req, res) => {
183201

184202
const harData = await loadHar(key)
185203

186-
// Find matching asset in HAR
204+
// Find matching asset in HAR by full URL comparison
187205
const assetEntry = harData.log.entries.find((entry) => {
188-
const url = new URL(entry.request.url)
189-
return url.pathname === `/${assetPath}` || url.pathname.endsWith(`/${assetPath}`)
206+
try {
207+
const url = new URL(entry.request.url)
208+
// First try exact path match
209+
if (url.pathname === `/${assetPath}`) {
210+
return true
211+
}
212+
// Then try path ending match (for nested paths)
213+
if (url.pathname.endsWith(`/${assetPath}`)) {
214+
return true
215+
}
216+
// Handle query parameters - check if path without query matches
217+
const pathWithoutQuery = url.pathname + url.search
218+
if (pathWithoutQuery === `/${assetPath}` || pathWithoutQuery.endsWith(`/${assetPath}`)) {
219+
return true
220+
}
221+
return false
222+
} catch {
223+
return false
224+
}
190225
})
191226

192227
if (!assetEntry) {

0 commit comments

Comments
 (0)