forked from vuejs/devtools-v6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevIframe.vue
More file actions
199 lines (179 loc) · 5.58 KB
/
DevIframe.vue
File metadata and controls
199 lines (179 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<script lang="ts">
import { defineComponent, ref, watch } from '@vue/composition-api'
import { Bridge } from '@vue-devtools/shared-utils'
import { initDevTools } from '@front'
import { installHook } from '@back/hook'
const STORAGE_URL = 'vue-devtools.dev-iframe.url'
export default defineComponent({
setup () {
const src = ref(localStorage.getItem(STORAGE_URL) ?? 'target.html')
const url = ref(src.value)
const iframe = ref(null)
const loading = ref(true)
const error = ref(false)
watch(src, (value) => {
localStorage.setItem(STORAGE_URL, value)
})
function inject (src, done) {
if (!src || src === 'false') {
return done()
}
const script = iframe.value.contentDocument.createElement('script')
script.src = src.replace(/\$ORIGIN/g, location.origin)
script.onload = done
iframe.value.contentDocument.body.appendChild(script)
}
function onLoad () {
loading.value = false
try {
console.log('%cInstalling hook...', 'color:#42B983;')
installHook(iframe.value.contentWindow)
let loadListener
// 2. init devtools
console.log('%cInit devtools...', 'color:#42B983;')
initDevTools({
connect (cb) {
// 3. called by devtools: inject backend
console.log('%cInjecting backend...', 'color:#42B983;')
inject('$ORIGIN/target/backend.js', () => {
// 4. send back bridge
console.log('%cInit bridge...', 'color:#42B983;')
cb(new Bridge({
listen (fn) {
iframe.value.contentWindow.parent.addEventListener('message', evt => fn(evt.data))
},
send (data) {
if (process.env.NODE_ENV !== 'production') {
console.log('%cdevtools -> backend', 'color:#888;', data)
}
iframe.value.contentWindow.postMessage(data, '*')
},
}))
})
},
onReload (reloadFn) {
loadListener = reloadFn
},
})
iframe.value.contentWindow.addEventListener('unload', () => {
if (loadListener) loadListener()
loading.value = true
})
} catch (e) {
console.error(e)
error.value = true
}
}
function openUrl () {
let value = url.value
if (value === src.value) {
reload()
} else {
if (!value.startsWith('http')) {
value = `http://${value}`
}
src.value = value
url.value = value
}
}
function reload () {
iframe.value.contentWindow.location.reload()
}
function reset () {
src.value = 'target.html'
}
const includeCode = `<script src="${location.origin}/target/hook.js"></script>`
return {
src,
url,
loading,
openUrl,
iframe,
onLoad,
reset,
error,
includeCode,
}
},
})
</script>
<template>
<div class="dev-iframe flex-1 flex flex-col">
<iframe
id="target"
ref="iframe"
name="target"
class="flex-1 border-none"
data-vue-devtools-ignore
:src="src"
@load="onLoad"
/>
<div class="border-t border-gray-300 flex relative">
<VueLoadingBar
v-if="loading"
class="primary ghost absolute left-0 top-0 w-full"
unknown
/>
<VueInput
v-model="url"
name="url"
placeholder="Enter target URL..."
class="min-w-0 flex-1 flat"
@keyup.enter="openUrl()"
/>
<VueButton
v-tooltip="'Refresh page'"
icon-left="refresh"
class="icon-button flat"
@click="openUrl()"
/>
<VueButton
v-tooltip="'Reset URL'"
icon-left="backspace"
:disabled="src === 'target.html'"
class="icon-button flat"
@click="reset()"
/>
<VueDropdown
:offset="[0, 0]"
>
<template #trigger>
<VueButton
v-tooltip="'Help'"
icon-left="help"
class="icon-button flat"
/>
</template>
<div class="p-8">
<p>
You need to <a
href="https://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome"
target="_blank"
class="text-green-500"
>disable Chrome web security</a> to allow manipulating an iframe on a different origin.
</p>
<pre class="my-2 p-2 rounded bg-gray-100 dark:bg-gray-900 text-sm">google-chrome --disable-web-security --disable-site-isolation-trials --user-data-dir="temp-chrome-data"</pre>
<p>If the devtools have trouble connecting to the webpage, please put this snippet in the target app HTML before the main scripts:</p>
<pre
class="my-2 p-2 rounded bg-gray-100 dark:bg-gray-900 text-sm"
v-html="includeCode"
/>
</div>
</VueDropdown>
</div>
<VueModal
v-if="error"
title="Error"
@close="error = false"
>
<div class="p-6">
Please <a
href="https://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome"
target="_blank"
class="text-green-500"
>disable Chrome web security</a> to allow manipulating an iframe on a different origin.
<pre class="my-2 p-2 rounded bg-gray-100 dark:bg-gray-900 text-sm">google-chrome --disable-web-security --disable-site-isolation-trials --user-data-dir="temp-chrome-data"</pre>
</div>
</VueModal>
</div>
</template>