admin管理员组文章数量:1434966
I want to override console methods (e.g. log, warn, error) of an iframe.
I get the error: webrtc/offer: stream not found in the console from the iframe.
I am trying to override console methods (log, warn, error) so that they does their original job in addition to storing all the method call arguments in a React State variable - consoleOutput
.
But the data in consoleOutput
is always empty. Any idea why I am unable to override those methods?
My code:
const iframeRef = useRef<HTMLIFrameElement | null>(null)
const [consoleOutput, setConsoleOutput] = useState<string[]>([])
useEffect(() => {
if (iframeRef.current) {
const iframeWindow = iframeRef.current.contentWindow as any;
if (iframeWindow) {
const originalConsole = iframeWindow.console;
const newConsoleOutput: string[] = [];
const wrapConsoleMethod = (method: keyof Console, prefix: string) => {
return (...args: any[]) => {
newConsoleOutput.push(`[${prefix}] ` + args.map(String).join(" "));
setConsoleOutput([...newConsoleOutput]);
originalConsole[method](...args);
};
};
iframeWindow.console = {
...originalConsole,
log: wrapConsoleMethod("log", "Log"),
error: wrapConsoleMethod("error", "Error"),
warn: wrapConsoleMethod("warn", "Warn"),
};
}
}
}, []);
<div>
<h3>Console output from iframe::</h3>
<pre>{consoleOutput.join('\n')}</pre>
</div>
<iframe
ref={iframeRef}
src={iframeSrc}
onLoad={() => console.warn('iframe loaded')}
/>
本文标签: javascriptOverride console methods (logwarnerror) of an iframeStack Overflow
版权声明:本文标题:javascript - Override console methods (log, warn, error) of an iframe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737424023a1988955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论