Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use frames for video capture & add iframes #346

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/clone-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Options } from './types'
import { getMimeType } from './mimes'
import { resourceToDataURL } from './dataurl'
import { clonePseudoElements } from './clone-pseudos'
import { createImage, toArray } from './util'

Expand All @@ -13,23 +11,46 @@ async function cloneCanvasElement(canvas: HTMLCanvasElement) {
return createImage(dataURL)
}

async function cloneVideoElement(video: HTMLVideoElement, options: Options) {
const poster = video.poster
const contentType = getMimeType(poster)
const dataURL = await resourceToDataURL(poster, contentType, options)
return createImage(dataURL)
async function cloneVideoElement(video: HTMLVideoElement) {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')

canvas.width = video.videoWidth
canvas.height = video.videoHeight
ctx?.drawImage(video, 0, 0, canvas.width, canvas.height)

return createImage(canvas.toDataURL())
}

async function cloneIFrameElement(iframe: HTMLIFrameElement) {
try {
if (iframe?.contentDocument?.body) {
return (await cloneNode(
iframe.contentDocument.body,
{},
true,
)) as HTMLBodyElement
}
} catch {
// Failed to clone iframe
}

return iframe.cloneNode(false) as HTMLIFrameElement
}

async function cloneSingleNode<T extends HTMLElement>(
node: T,
options: Options,
): Promise<HTMLElement> {
if (node instanceof HTMLCanvasElement) {
return cloneCanvasElement(node)
}

if (node instanceof HTMLVideoElement && node.poster) {
return cloneVideoElement(node, options)
if (node instanceof HTMLVideoElement) {
return cloneVideoElement(node)
}

if (node instanceof HTMLIFrameElement) {
return cloneIFrameElement(node)
}

return node.cloneNode(false) as T
Expand Down Expand Up @@ -185,7 +206,7 @@ export async function cloneNode<T extends HTMLElement>(
}

return Promise.resolve(node)
.then((clonedNode) => cloneSingleNode(clonedNode, options) as Promise<T>)
.then((clonedNode) => cloneSingleNode(clonedNode) as Promise<T>)
.then((clonedNode) => cloneChildren(node, clonedNode, options))
.then((clonedNode) => decorate(node, clonedNode))
.then((clonedNode) => ensureSVGSymbols(clonedNode, options))
Expand Down