LP_Bot/src/utils/iframeUtils.ts

47 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-11-23 16:33:21 +08:00
import { CSSProperties } from "react";
import { sleep } from ".";
/**
* iframe
* @param parent
* @param url
* @param style CSSProperties
*/
export function createIframe(parent: string, url: string, style?: CSSProperties): HTMLIFrameElement {
const iframe: HTMLIFrameElement = document.createElement("iframe");
iframe.src = url;
iframe.frameBorder = "0";
iframe.scrolling = "0";
for (let i: number = 0, keys: string[] = Object.keys(style); i < keys.length; i++) {
const key: string = keys[i];
iframe.style[key] = style[key];
}
document.getElementById(parent).appendChild(iframe);
return iframe;
}
/**
* iframe
* @param parent
*/
export async function destroyIframe(parent: string) {
const iframeContent: HTMLElement = document.getElementById(parent);
for (let i = 0; i < iframeContent.children.length; i++) {
const iframeNode: HTMLIFrameElement = iframeContent.children[i] as HTMLIFrameElement;
try {
iframeNode.src = "about:blank";
await sleep(10);
const iframeWindow = iframeNode.contentWindow;
iframeWindow.document.open();
iframeWindow.document.write("");
iframeWindow.document.clear && iframeWindow.document.clear();
iframeWindow.document.close();
} catch (error) {
console.warn(error);
}
iframeNode.remove();
iframeNode.parentNode && iframeContent.removeChild(iframeNode);
// Cocos.CocosEventListener.RemoveAllCallbacks();
// await sleep(2000);
}
}