Spaces:
Build error
Build error
File size: 2,141 Bytes
9b63060 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import React, { useRef, forwardRef, useImperativeHandle } from 'react';
export interface IframeManagerRef {
loadUrl: (url: string) => void;
refresh: () => void;
injectScript: (script: string) => void;
requestSummary: () => void;
}
interface IframeManagerProps {
onTitleChange: (title: string) => void;
}
const IframeManager = forwardRef<IframeManagerRef, IframeManagerProps>(
({ onTitleChange }, ref) => {
const iframeRef = useRef<HTMLIFrameElement>(null);
const loadUrl = async (url: string) => {
try {
console.log('Loading into iframe:', url);
const proxiedUrl = url.startsWith('http')
? `https://embed-proxy-prod.gamma-app.workers.dev/?alt_url=${encodeURIComponent(url)}`
: url;
if (iframeRef.current) {
iframeRef.current.src = proxiedUrl;
}
} catch (error) {
console.error('Error loading page:', error);
alert(`Failed to load page: ${error}`);
}
};
const refresh = () => {
if (iframeRef.current && iframeRef.current.src) {
const currentSrc = iframeRef.current.src;
iframeRef.current.src = '';
setTimeout(() => {
if (iframeRef.current) {
iframeRef.current.src = currentSrc;
}
}, 100);
}
};
const injectScript = (script: string) => {
if (iframeRef.current?.contentWindow) {
iframeRef.current.contentWindow.postMessage({
type: 'user-script',
script
}, '*');
}
};
const requestSummary = () => {
if (iframeRef.current?.contentWindow) {
iframeRef.current.contentWindow.postMessage('extract-summary', '*');
}
};
useImperativeHandle(ref, () => ({
loadUrl,
refresh,
injectScript,
requestSummary
}));
return (
<iframe
ref={iframeRef}
className="w-full h-full border-none"
sandbox="allow-scripts allow-same-origin allow-popups allow-forms"
title="Embedded Browser"
/>
);
}
);
IframeManager.displayName = 'IframeManager';
export default IframeManager;
|