feat: get sandbox window using ad iframes

This commit is contained in:
Xmader 2020-11-27 12:11:34 -05:00
parent 1f62f47dc8
commit 01514f651b
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
1 changed files with 22 additions and 2 deletions

View File

@ -47,9 +47,29 @@ export const useTimeout = async <T> (promise: T | Promise<T>, ms: number): Promi
})
}
export const getSandboxWindowAsync = async (targetEl = document.documentElement): Promise<Window> => {
export const getSandboxWindowAsync = async (targetEl: Element | undefined = undefined): Promise<Window> => {
if (typeof document === 'undefined') return {} as any as Window
if (!targetEl) {
return new Promise((resolve) => {
// You need ads in your pages, right?
const observer = new MutationObserver(() => {
for (let i = 0; i < window.frames.length; i++) {
// find iframe windows created by ads
const frame = frames[i]
try {
const href = frame.location.href
if (href === location.href || href === 'about:blank') {
resolve(frame)
return
}
} catch { }
}
})
observer.observe(document.body, { subtree: true, childList: true })
})
}
return new Promise((resolve) => {
const eventName = 'onmousemove'
const id = Math.random().toString()
@ -75,7 +95,7 @@ export const getUnsafeWindow = (): Window => {
export const console: Console = (window || global).console // Object.is(window.console, unsafeWindow.console) == false
export const windowOpenAsync = (targetEl = document.documentElement, ...args: Parameters<Window['open']>): Promise<Window | null> => {
export const windowOpenAsync = (targetEl: Element | undefined, ...args: Parameters<Window['open']>): Promise<Window | null> => {
return getSandboxWindowAsync(targetEl).then(w => w.open(...args))
}