refac
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
const i18n = getContext('i18n');
|
const i18n = getContext('i18n');
|
||||||
|
|
||||||
|
|
||||||
export let baseUrl: string;
|
export let baseUrl: string;
|
||||||
export let port: number;
|
export let port: number;
|
||||||
export let path: string = '';
|
export let path: string = '';
|
||||||
@@ -23,7 +24,6 @@
|
|||||||
$: canGoForward = historyIndex < history.length - 1;
|
$: canGoForward = historyIndex < history.length - 1;
|
||||||
|
|
||||||
const pushHistory = (newPath: string) => {
|
const pushHistory = (newPath: string) => {
|
||||||
// Trim forward history when navigating from a non-tip position
|
|
||||||
if (historyIndex < history.length - 1) {
|
if (historyIndex < history.length - 1) {
|
||||||
history = history.slice(0, historyIndex + 1);
|
history = history.slice(0, historyIndex + 1);
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,7 @@
|
|||||||
if (!canGoBack) return;
|
if (!canGoBack) return;
|
||||||
historyIndex -= 1;
|
historyIndex -= 1;
|
||||||
path = history[historyIndex];
|
path = history[historyIndex];
|
||||||
|
syncUrlBar();
|
||||||
iframeKey += 1;
|
iframeKey += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -42,28 +43,26 @@
|
|||||||
if (!canGoForward) return;
|
if (!canGoForward) return;
|
||||||
historyIndex += 1;
|
historyIndex += 1;
|
||||||
path = history[historyIndex];
|
path = history[historyIndex];
|
||||||
|
syncUrlBar();
|
||||||
iframeKey += 1;
|
iframeKey += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ── URLs ─────────────────────────────────────────────────────────────
|
// ── URLs ─────────────────────────────────────────────────────────────
|
||||||
$: proxyUrl = getPortProxyUrl(baseUrl, port, path);
|
$: proxyUrl = getPortProxyUrl(baseUrl, port, path);
|
||||||
|
|
||||||
// The proxy path prefix, e.g. "/proxy/8080/"
|
|
||||||
$: proxyPathPrefix = (() => {
|
$: proxyPathPrefix = (() => {
|
||||||
try {
|
try {
|
||||||
return new URL(getPortProxyUrl(baseUrl, port, '')).pathname;
|
return new URL(getPortProxyUrl(baseUrl, port, ''), window.location.origin).pathname;
|
||||||
} catch {
|
} catch {
|
||||||
return `/proxy/${port}/`;
|
return `/proxy/${port}/`;
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Display-friendly localhost URL for the address bar
|
const makeDisplayUrl = (p: string) => `localhost:${port}${p ? '/' + p : ''}`;
|
||||||
$: displayUrl = `localhost:${port}${path ? '/' + path : ''}`;
|
const syncUrlBar = () => { urlInput = makeDisplayUrl(path); };
|
||||||
$: urlInput = displayUrl;
|
urlInput = makeDisplayUrl(path);
|
||||||
|
|
||||||
const refresh = () => {
|
const refresh = () => { iframeKey += 1; };
|
||||||
iframeKey += 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
const openExternal = () => {
|
const openExternal = () => {
|
||||||
window.open(proxyUrl, '_blank', 'noopener,noreferrer');
|
window.open(proxyUrl, '_blank', 'noopener,noreferrer');
|
||||||
@@ -84,113 +83,36 @@
|
|||||||
path = newPath;
|
path = newPath;
|
||||||
pushHistory(path);
|
pushHistory(path);
|
||||||
}
|
}
|
||||||
|
syncUrlBar();
|
||||||
iframeKey += 1;
|
iframeKey += 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* After each iframe load:
|
* Read the iframe's current location and sync the URL bar.
|
||||||
* 1. Sync the address bar with the current iframe path
|
* If the iframe escaped the proxy prefix, redirect it back.
|
||||||
* 2. Inject a click interceptor to rewrite links BEFORE navigation
|
|
||||||
* 3. Redirect if the page already escaped the proxy (fallback)
|
|
||||||
*/
|
*/
|
||||||
const onIframeLoad = () => {
|
const onIframeLoad = () => {
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
if (!iframeEl) return;
|
if (!iframeEl) return;
|
||||||
try {
|
try {
|
||||||
const doc = iframeEl.contentDocument;
|
const loc = iframeEl.contentWindow?.location;
|
||||||
const win = iframeEl.contentWindow;
|
if (!loc) return;
|
||||||
if (!doc || !win) return;
|
const iframePath = loc.pathname ?? '';
|
||||||
|
const iframeSearch = loc.search ?? '';
|
||||||
const loc = win.location;
|
const iframeHash = loc.hash ?? '';
|
||||||
const iframePath = loc?.pathname ?? '';
|
|
||||||
const iframeSearch = loc?.search ?? '';
|
|
||||||
const iframeHash = loc?.hash ?? '';
|
|
||||||
|
|
||||||
if (iframePath.startsWith(proxyPathPrefix)) {
|
if (iframePath.startsWith(proxyPathPrefix)) {
|
||||||
// Still inside the proxy — sync address bar
|
|
||||||
const relativePath = iframePath.slice(proxyPathPrefix.length) + iframeSearch + iframeHash;
|
const relativePath = iframePath.slice(proxyPathPrefix.length) + iframeSearch + iframeHash;
|
||||||
if (relativePath !== path) {
|
if (relativePath !== path) {
|
||||||
path = relativePath;
|
path = relativePath;
|
||||||
pushHistory(path);
|
pushHistory(path);
|
||||||
urlInput = displayUrl;
|
syncUrlBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inject click interceptor to rewrite links before navigation
|
|
||||||
injectLinkInterceptor(doc, win);
|
|
||||||
} else if (iframePath && iframePath !== 'about:blank') {
|
|
||||||
// Escaped the proxy — redirect back through it
|
|
||||||
const escapedPath = iframePath.replace(/^\//, '') + iframeSearch + iframeHash;
|
|
||||||
const correctedUrl = getPortProxyUrl(baseUrl, port, escapedPath);
|
|
||||||
loc?.replace(correctedUrl);
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Cross-origin — can't access
|
// Cross-origin — can't access
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Inject a click interceptor into the iframe document that rewrites
|
|
||||||
* anchor hrefs to stay within the proxy path.
|
|
||||||
*/
|
|
||||||
const injectLinkInterceptor = (doc: Document, win: Window) => {
|
|
||||||
// Skip if we already injected
|
|
||||||
if ((win as any).__portPreviewIntercepted) return;
|
|
||||||
(win as any).__portPreviewIntercepted = true;
|
|
||||||
|
|
||||||
const prefix = proxyPathPrefix;
|
|
||||||
|
|
||||||
// Intercept all click events on anchors
|
|
||||||
doc.addEventListener('click', (e: MouseEvent) => {
|
|
||||||
const anchor = (e.target as HTMLElement)?.closest?.('a');
|
|
||||||
if (!anchor || !anchor.href) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const url = new URL(anchor.href, win.location.href);
|
|
||||||
|
|
||||||
// Only intercept same-origin links that escaped the proxy
|
|
||||||
if (url.origin === win.location.origin && !url.pathname.startsWith(prefix)) {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
// Rewrite to go through proxy
|
|
||||||
const rewrittenPath = prefix + url.pathname.replace(/^\//, '') + url.search + url.hash;
|
|
||||||
win.location.href = url.origin + rewrittenPath;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Invalid URL — let browser handle it
|
|
||||||
}
|
|
||||||
}, true); // Use capture phase to intercept before SPA routers
|
|
||||||
|
|
||||||
// Also intercept form submissions
|
|
||||||
doc.addEventListener('submit', (e: Event) => {
|
|
||||||
const form = e.target as HTMLFormElement;
|
|
||||||
if (!form?.action) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const url = new URL(form.action, win.location.href);
|
|
||||||
if (url.origin === win.location.origin && !url.pathname.startsWith(prefix)) {
|
|
||||||
form.action = url.origin + prefix + url.pathname.replace(/^\//, '') + url.search;
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
}, true);
|
|
||||||
|
|
||||||
// Intercept window.open and programmatic navigation
|
|
||||||
const origOpen = win.open.bind(win);
|
|
||||||
win.open = function(url?: string | URL, ...args: any[]) {
|
|
||||||
if (url && typeof url === 'string') {
|
|
||||||
try {
|
|
||||||
const parsed = new URL(url, win.location.href);
|
|
||||||
if (parsed.origin === win.location.origin && !parsed.pathname.startsWith(prefix)) {
|
|
||||||
url = parsed.origin + prefix + parsed.pathname.replace(/^\//, '') + parsed.search + parsed.hash;
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
return origOpen(url, ...args);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const onIframeLoadStart = () => {
|
|
||||||
isLoading = true;
|
|
||||||
};
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col h-full min-h-0">
|
<div class="flex flex-col h-full min-h-0">
|
||||||
@@ -230,22 +152,16 @@
|
|||||||
</button>
|
</button>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
|
||||||
<!-- Refresh / Stop -->
|
<!-- Refresh -->
|
||||||
<Tooltip content={$i18n.t('Refresh')}>
|
<Tooltip content={$i18n.t('Refresh')}>
|
||||||
<button
|
<button
|
||||||
class="p-1 rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-800 hover:text-gray-700 dark:hover:text-gray-300 transition"
|
class="p-1 rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-800 hover:text-gray-700 dark:hover:text-gray-300 transition"
|
||||||
on:click={refresh}
|
on:click={refresh}
|
||||||
aria-label={$i18n.t('Refresh')}
|
aria-label={$i18n.t('Refresh')}
|
||||||
>
|
>
|
||||||
{#if isLoading}
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-3.5" class:animate-spin={isLoading}>
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-3.5 animate-spin">
|
<path fill-rule="evenodd" d="M15.312 11.424a5.5 5.5 0 0 1-9.201 2.466l-.312-.311h2.451a.75.75 0 0 0 0-1.5H4.5a.75.75 0 0 0-.75.75v3.75a.75.75 0 0 0 1.5 0v-2.127l.13.13a7 7 0 0 0 11.712-3.138.75.75 0 0 0-1.449-.39Zm-10.624-2.85a5.5 5.5 0 0 1 9.201-2.465l.312.31H11.75a.75.75 0 0 0 0 1.5h3.75a.75.75 0 0 0 .75-.75V3.42a.75.75 0 0 0-1.5 0v2.126l-.13-.129A7 7 0 0 0 3.239 8.555a.75.75 0 0 0 1.449.39Z" clip-rule="evenodd" />
|
||||||
<path fill-rule="evenodd" d="M15.312 11.424a5.5 5.5 0 0 1-9.201 2.466l-.312-.311h2.451a.75.75 0 0 0 0-1.5H4.5a.75.75 0 0 0-.75.75v3.75a.75.75 0 0 0 1.5 0v-2.127l.13.13a7 7 0 0 0 11.712-3.138.75.75 0 0 0-1.449-.39Zm-10.624-2.85a5.5 5.5 0 0 1 9.201-2.465l.312.31H11.75a.75.75 0 0 0 0 1.5h3.75a.75.75 0 0 0 .75-.75V3.42a.75.75 0 0 0-1.5 0v2.126l-.13-.129A7 7 0 0 0 3.239 8.555a.75.75 0 0 0 1.449.39Z" clip-rule="evenodd" />
|
</svg>
|
||||||
</svg>
|
|
||||||
{:else}
|
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-3.5">
|
|
||||||
<path fill-rule="evenodd" d="M15.312 11.424a5.5 5.5 0 0 1-9.201 2.466l-.312-.311h2.451a.75.75 0 0 0 0-1.5H4.5a.75.75 0 0 0-.75.75v3.75a.75.75 0 0 0 1.5 0v-2.127l.13.13a7 7 0 0 0 11.712-3.138.75.75 0 0 0-1.449-.39Zm-10.624-2.85a5.5 5.5 0 0 1 9.201-2.465l.312.31H11.75a.75.75 0 0 0 0 1.5h3.75a.75.75 0 0 0 .75-.75V3.42a.75.75 0 0 0-1.5 0v2.126l-.13-.129A7 7 0 0 0 3.239 8.555a.75.75 0 0 0 1.449.39Z" clip-rule="evenodd" />
|
|
||||||
</svg>
|
|
||||||
{/if}
|
|
||||||
</button>
|
</button>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
|
||||||
@@ -275,7 +191,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
|
|
||||||
<!-- Close preview -->
|
<!-- Close -->
|
||||||
<Tooltip content={$i18n.t('Close')}>
|
<Tooltip content={$i18n.t('Close')}>
|
||||||
<button
|
<button
|
||||||
class="p-1 rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-800 hover:text-gray-700 dark:hover:text-gray-300 transition"
|
class="p-1 rounded text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-800 hover:text-gray-700 dark:hover:text-gray-300 transition"
|
||||||
@@ -296,7 +212,7 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<!-- Iframe content -->
|
<!-- Iframe -->
|
||||||
{#key iframeKey}
|
{#key iframeKey}
|
||||||
<iframe
|
<iframe
|
||||||
bind:this={iframeEl}
|
bind:this={iframeEl}
|
||||||
|
|||||||
Reference in New Issue
Block a user