forked from liriliri/chii
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iframe.js
74 lines (66 loc) · 2.18 KB
/
iframe.js
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
const targetIframe = document.getElementById('target');
const devtoolsIframe = document.getElementById('devtools');
function resetHeight() {
const targetHeight = Math.floor(window.innerHeight * 0.4);
targetIframe.style.height = targetHeight + 'px';
devtoolsIframe.style.height = window.innerHeight - targetHeight + 'px';
}
resetHeight();
window.addEventListener('resize', resetHeight);
const targetSrc =
location.protocol + '//' + location.host + location.pathname.replace('test/iframe.html', '') + 'target.js';
targetIframe.onload = function () {
targetIframe.contentWindow.ChiiDevtoolsIframe = devtoolsIframe;
targetIframe.contentWindow.injectTarget(targetSrc);
};
window.addEventListener('message', event => {
targetIframe.contentWindow.postMessage(event.data, event.origin);
});
function load() {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Target</title>
<style>
.motto {
display: inline-block;
}
</style>
</head>
<body>
<div class="motto">Hello Chii!</div>
<button onclick="reload()">Reload</button>
<script>
console.log('Page loaded!');
setTimeout(function () {
console.log('Hello Chii');
fetch(location.href);
}, 1000);
window.reload = function () {
location.reload();
}
window.injectTarget = function (targetSrc) {
var script = document.createElement('script');
script.src = targetSrc;
script.setAttribute('embedded', 'true');
script.setAttribute('cdn', 'https://cdn.jsdelivr.net/npm/chii/public');
script.onload = function () {
console.log('console right after target injected');
throw Error('exception right after target injected');
};
document.head.appendChild(script);
}
</script>
</body>
</html>`;
const blob = new Blob([html], {
type: 'text/html',
});
const src = URL.createObjectURL(blob);
targetIframe.src = src;
}
load();