Skip to content

Commit 083e0f4

Browse files
committed
chore: add detect iframe
1 parent 521ebe8 commit 083e0f4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export function detectIframe(target: Window | typeof globalThis, inIframe = false) {
2+
console.log({
3+
inIframe,
4+
})
5+
function injectIframeHook(iframe) {
6+
if ((iframe as any).__vdevtools__injected) {
7+
return
8+
}
9+
try {
10+
(iframe as any).__vdevtools__injected = true
11+
const inject = () => {
12+
try {
13+
/**
14+
* Install the hook on window, which is an event emitter.
15+
* Note because Chrome content scripts cannot directly modify the window object,
16+
* we are evaluating this function by inserting a script tag. That's why we have
17+
* to inline the whole event emitter implementation here.
18+
*/
19+
(iframe.contentWindow as any).__VUE_DEVTOOLS_IFRAME__ = iframe
20+
const script = iframe.contentDocument.createElement('script')
21+
script.textContent = `;(${detectIframe.toString()})(window, true)`
22+
iframe.contentDocument.documentElement.appendChild(script)
23+
script.parentNode.removeChild(script)
24+
}
25+
catch (e) {
26+
// Ignore
27+
}
28+
}
29+
inject()
30+
iframe.addEventListener('load', () => inject())
31+
}
32+
catch (e) {
33+
// Ignore
34+
}
35+
}
36+
37+
let iframeChecks = 0
38+
function injectToIframes() {
39+
if (typeof window === 'undefined') {
40+
return
41+
}
42+
43+
const iframes = Array.from(document.querySelectorAll<HTMLIFrameElement>('iframe:not([data-vue-devtools-ignore])'))
44+
for (const iframe of iframes) {
45+
injectIframeHook(iframe)
46+
}
47+
}
48+
injectToIframes()
49+
const iframeTimer = setInterval(() => {
50+
injectToIframes()
51+
iframeChecks++
52+
if (iframeChecks >= 5) {
53+
clearInterval(iframeTimer)
54+
}
55+
}, 1000)
56+
}

packages/devtools-kit/src/core/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import {
1818
import { createDevToolsHook, hook, subscribeDevToolsHook } from '../hook'
1919
import { DevToolsHooks } from '../types'
2020
import { createAppRecord, removeAppRecordId } from './app'
21+
import { detectIframe } from './iframe'
2122
import { callDevToolsPluginSetupFn, createComponentsDevToolsPlugin, registerDevToolsPlugin, removeRegisteredPluginApp, setupDevToolsPlugin } from './plugin'
2223
import { initPluginSettings } from './plugin/plugin-settings'
2324
import { normalizeRouterInfo } from './router'
2425

2526
export function initDevTools() {
27+
detectIframe()
28+
2629
updateDevToolsState({
2730
vitePluginDetected: getDevToolsEnv().vitePluginDetected,
2831
})

0 commit comments

Comments
 (0)