Skip to content

Commit 751816e

Browse files
committed
har:view now fixes the URL. domain is still weird though...
1 parent f52c484 commit 751816e

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

browser-extension/tests/har-view.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express'
22
import fs from 'node:fs/promises'
33
import path from 'node:path'
44
import { fileURLToPath } from 'node:url'
5+
import { PAGES } from './har-index'
56

67
const __dirname = path.dirname(fileURLToPath(import.meta.url))
78
const app = express()
@@ -10,6 +11,31 @@ const PORT = 3001
1011
// Store HAR data
1112
const harCache = new Map<string, any>()
1213

14+
// Create mapping from HAR filename to original URL
15+
const harToUrlMap = Object.fromEntries(
16+
PAGES.map(([key, url]) => [`${key}.har`, url])
17+
)
18+
19+
// Extract URL parts for location patching
20+
function getUrlParts(filename: string) {
21+
const originalUrl = harToUrlMap[filename]
22+
if (!originalUrl) {
23+
return null
24+
}
25+
26+
try {
27+
const url = new URL(originalUrl)
28+
return {
29+
hostname: url.hostname,
30+
pathname: url.pathname,
31+
href: originalUrl,
32+
host: url.host
33+
}
34+
} catch {
35+
return null
36+
}
37+
}
38+
1339
// Check if WXT dev server is running
1440
async function checkDevServer(): Promise<boolean> {
1541
try {
@@ -152,6 +178,12 @@ app.get('/page/:filename/gitcasso', async (req, res) => {
152178
return res.status(400).send('Invalid file type')
153179
}
154180

181+
// Get original URL parts for location patching
182+
const urlParts = getUrlParts(filename)
183+
if (!urlParts) {
184+
return res.status(400).send('Unknown HAR file - not found in har-index.ts')
185+
}
186+
155187
const harData = await loadHar(filename)
156188

157189
// Find the main HTML response
@@ -173,9 +205,22 @@ app.get('/page/:filename/gitcasso', async (req, res) => {
173205
`/asset/${filename.replace('.har', '')}`
174206
)
175207

176-
// Inject patched content script that bypasses webextension-polyfill
208+
// Inject patched content script with location patching
177209
const contentScriptTag = `
178210
<script>
211+
// Patch window.location before loading content script
212+
console.log('Patching window.location to simulate original URL...');
213+
214+
// Use history.pushState to change the pathname
215+
window.history.pushState({}, '', '${urlParts.pathname}');
216+
217+
console.log('Location patched:', {
218+
hostname: window.location.hostname,
219+
pathname: window.location.pathname,
220+
href: window.location.href,
221+
host: window.location.host
222+
});
223+
179224
// Fetch and patch the content script to remove webextension-polyfill issues
180225
fetch('http://localhost:3000/.output/chrome-mv3-dev/content-scripts/content.js')
181226
.then(response => response.text())
@@ -204,7 +249,7 @@ app.get('/page/:filename/gitcasso', async (req, res) => {
204249
script.textContent = patchedCode;
205250
document.head.appendChild(script);
206251
207-
console.log('Gitcasso content script loaded and patched for HAR testing');
252+
console.log('Gitcasso content script loaded with location patching for:', '${urlParts.href}');
208253
})
209254
.catch(error => {
210255
console.error('Failed to load and patch content script:', error);

0 commit comments

Comments
 (0)