This commit is contained in:
Timothy Jaeryang Baek committed 2026-07-10 14:35:27 -05:00
1 parent 5146bbe070
commit fdb9c456c4
5 files changed
+215 -44

No files matched your search

+1 -1
View File
@@ -30,7 +30,7 @@ export const updateBrowserSession = (sessionId: string, url: string, title: stri
export const browserFrameUrl = (sessionId: string, rawUrl: string) => {
const normalized = /^https?:\/\//i.test(rawUrl) ? rawUrl : `https://${rawUrl}`;
const url = new URL(normalized);
return `/api/browser/frame/${sessionId}?url=${encodeURIComponent(url.href)}`;
return `/api/browser/frame/${sessionId}/${url.protocol.slice(0, -1)}/${encodeURIComponent(url.host)}${url.pathname}${url.search}${url.hash}`;
};
export const browserBlankUrl = (sessionId: string) => `/api/browser/sessions/${sessionId}/blank`;
@@ -1,7 +1,12 @@
<script lang="ts">
import { onDestroy, onMount } from 'svelte';
import { tooltip } from '$lib/tooltip';
import { browserBlankUrl, browserFrameUrl, getBrowserSession, updateBrowserSession } from '$lib/apis/browser';
import {
browserBlankUrl,
browserFrameUrl,
getBrowserSession,
updateBrowserSession
} from '$lib/apis/browser';
import { openBrowserTab } from '$lib/stores';
import Icon from './Icon.svelte';
import { t } from '$lib/i18n';
@@ -22,19 +27,27 @@
function publicUrl(value: string) {
const proxyUrl = new URL(value, location.origin);
if (proxyUrl.pathname === `/api/browser/frame/${sessionId}`) return proxyUrl.searchParams.get('url') || value;
const legacyMarker = `/api/browser/frame/${sessionId}/`;
const index = value.lastIndexOf(legacyMarker);
if (proxyUrl.pathname === `/api/browser/frame/${sessionId}`)
return proxyUrl.searchParams.get('url') || value;
const marker = `/api/browser/frame/${sessionId}/`;
const index = proxyUrl.pathname.indexOf(marker);
if (index === -1) return value;
const [scheme, host, ...path] = value.slice(index + legacyMarker.length).split('/');
return scheme && host ? `${scheme}://${host}/${path.join('/')}` : value;
const [scheme, encodedHost, ...path] = proxyUrl.pathname
.slice(index + marker.length)
.split('/');
const host = decodeURIComponent(encodedHost || '');
return scheme && host
? `${scheme}://${host}/${path.join('/')}${proxyUrl.search}${proxyUrl.hash}`
: value;
}
function navigate(url = urlInput) {
const value = url.trim();
if (!value) return;
try {
urlInput = new URL(/^https?:\/\//i.test(publicUrl(value)) ? publicUrl(value) : `https://${publicUrl(value)}`).href;
urlInput = new URL(
/^https?:\/\//i.test(publicUrl(value)) ? publicUrl(value) : `https://${publicUrl(value)}`
).href;
frameSrc = browserFrameUrl(sessionId, urlInput);
loading = true;
error = '';
@@ -49,6 +62,25 @@
iframeEl?.contentWindow?.location.reload();
}
function frameLoaded() {
loading = false;
try {
const current = new URL(iframeEl?.contentWindow?.location.href || '');
const framePrefix = `/api/browser/frame/${sessionId}`;
const blankPath = `/api/browser/sessions/${sessionId}/blank`;
if (current.origin !== location.origin || current.pathname.startsWith(framePrefix) || current.pathname === blankPath)
return;
const upstream = new URL(`${current.pathname}${current.search}${current.hash}`, urlInput).href;
urlInput = upstream;
loading = true;
void updateBrowserSession(sessionId, upstream, title).catch(() => {});
iframeEl?.contentWindow?.location.replace(browserFrameUrl(sessionId, upstream));
} catch {
// Cross-origin frames cannot be inspected; the proxy frame is same-origin.
}
}
function receiveMessage(event: MessageEvent) {
if (event.origin !== location.origin || event.source !== iframeEl?.contentWindow) return;
const data = event.data;
@@ -76,10 +108,18 @@
<div class="preview-container">
<div class="preview-toolbar">
<button class="preview-btn" onclick={() => iframeEl?.contentWindow?.history.back()} use:tooltip={$t('settings.back')}>
<button
class="preview-btn"
onclick={() => iframeEl?.contentWindow?.history.back()}
use:tooltip={$t('settings.back')}
>
<Icon name="chevron-left" size={12} />
</button>
<button class="preview-btn" onclick={() => iframeEl?.contentWindow?.history.forward()} use:tooltip={$t('common.forward')}>
<button
class="preview-btn"
onclick={() => iframeEl?.contentWindow?.history.forward()}
use:tooltip={$t('common.forward')}
>
<Icon name="chevron-right" size={12} />
</button>
<button class="preview-btn" onclick={refresh} use:tooltip={$t('files.refresh')}>
@@ -94,19 +134,22 @@
spellcheck="false"
/>
</div>
{#if title}<span class="browser-title" title={title}>{title}</span>{/if}
{#if title}<span class="browser-title" {title}>{title}</span>{/if}
</div>
{#if loading}<div class="loading-track"><div class="loading-bar"></div></div>{/if}
<div class="preview-content">
{#if error}
<div class="preview-error"><p class="error-title">{error}</p><button class="error-retry" onclick={() => navigate()}>{$t('files.retry')}</button></div>
<div class="preview-error">
<p class="error-title">{error}</p>
<button class="error-retry" onclick={() => navigate()}>{$t('files.retry')}</button>
</div>
{:else}
<iframe
bind:this={iframeEl}
src={frameSrc}
title={title || $t('bar.newBrowser')}
class="preview-iframe"
onload={() => (loading = false)}
onload={frameLoaded}
onerror={() => {
error = $t('port.cannotConnect');
loading = false;
@@ -118,21 +161,109 @@
<style>
@reference "../../app.css";
.preview-container { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.preview-toolbar { display: flex; align-items: center; gap: 0.25rem; height: 2rem; padding: 0 0.375rem; border-bottom: 1px solid var(--color-gray-200); flex-shrink: 0; }
.preview-btn { display: flex; align-items: center; justify-content: center; width: 1.375rem; height: 1.375rem; border-radius: 0.25rem; color: var(--color-gray-500); flex-shrink: 0; }
.preview-btn:hover { background: var(--color-gray-100); color: var(--color-gray-700); }
.url-bar { flex: 1; min-width: 0; }
.url-input { width: 100%; border: 1px solid var(--color-gray-200); border-radius: 999px; padding: 0.1875rem 0.75rem; font-size: 0.6875rem; font-family: var(--font-mono); background: white; color: var(--color-gray-600); outline: none; }
.browser-title { max-width: 10rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.6875rem; color: var(--color-gray-500); }
.preview-content { flex: 1; min-height: 0; position: relative; }
.preview-iframe { width: 100%; height: 100%; border: 0; display: block; background: white; }
.loading-track { height: 2px; overflow: hidden; background: var(--color-gray-100); }
.loading-bar { height: 100%; width: 35%; background: var(--color-brand-500); animation: browser-loading 1s ease-in-out infinite; }
.preview-error { height: 100%; display: grid; place-content: center; gap: 0.5rem; text-align: center; }
.error-retry { color: var(--color-brand-600); }
@keyframes browser-loading { from { transform: translateX(-120%); } to { transform: translateX(320%); } }
:global(.dark) .preview-toolbar { border-color: rgba(255,255,255,.06); }
:global(.dark) .url-input { background: rgba(255,255,255,.04); border-color: rgba(255,255,255,.08); color: var(--color-gray-300); }
:global(.dark) .preview-btn:hover { background: rgba(255,255,255,.06); color: var(--color-gray-300); }
.preview-container {
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
}
.preview-toolbar {
display: flex;
align-items: center;
gap: 0.25rem;
height: 2rem;
padding: 0 0.375rem;
border-bottom: 1px solid var(--color-gray-200);
flex-shrink: 0;
}
.preview-btn {
display: flex;
align-items: center;
justify-content: center;
width: 1.375rem;
height: 1.375rem;
border-radius: 0.25rem;
color: var(--color-gray-500);
flex-shrink: 0;
}
.preview-btn:hover {
background: var(--color-gray-100);
color: var(--color-gray-700);
}
.url-bar {
flex: 1;
min-width: 0;
}
.url-input {
width: 100%;
border: 1px solid var(--color-gray-200);
border-radius: 999px;
padding: 0.1875rem 0.75rem;
font-size: 0.6875rem;
font-family: var(--font-mono);
background: white;
color: var(--color-gray-600);
outline: none;
}
.browser-title {
max-width: 10rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 0.6875rem;
color: var(--color-gray-500);
}
.preview-content {
flex: 1;
min-height: 0;
position: relative;
}
.preview-iframe {
width: 100%;
height: 100%;
border: 0;
display: block;
background: white;
}
.loading-track {
height: 2px;
overflow: hidden;
background: var(--color-gray-100);
}
.loading-bar {
height: 100%;
width: 35%;
background: var(--color-brand-500);
animation: browser-loading 1s ease-in-out infinite;
}
.preview-error {
height: 100%;
display: grid;
place-content: center;
gap: 0.5rem;
text-align: center;
}
.error-retry {
color: var(--color-brand-600);
}
@keyframes browser-loading {
from {
transform: translateX(-120%);
}
to {
transform: translateX(320%);
}
}
:global(.dark) .preview-toolbar {
border-color: rgba(255, 255, 255, 0.06);
}
:global(.dark) .url-input {
background: rgba(255, 255, 255, 0.04);
border-color: rgba(255, 255, 255, 0.08);
color: var(--color-gray-300);
}
:global(.dark) .preview-btn:hover {
background: rgba(255, 255, 255, 0.06);
color: var(--color-gray-300);
}
</style>