-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
158 lines (136 loc) · 3.57 KB
/
index.html
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
<!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>Main App</title>
<style>
* {
font-family: sans-serif;
}
body {
text-align: center;
}
.frames {
display: flex;
width: 100%;
}
.col {
width: 50%;
}
iframe {
width: 100%;
height: 40vh;
}
button {
background-color: #666;
color: whitesmoke;
padding: 0.5rem 1rem;
border: 0;
border-radius: 0.2rem;
margin: 0 0.5rem 1rem;
cursor: pointer;
}
</style>
</head>
<body>
<h1>
postMessage Use Case
</h1>
<br>
<sections class="frames">
<div class="col" id="vueCol">
<button id="msgVue">Envia mensagem para Vue</button>
<p>Contagem Vue: <strong id="vueCount"></strong></p>
</div>
<div class="col" id="svelteCol">
<button id="msgSvelte">Envia mensagem para Svelte</button>
<p>Contagem Svelte: <strong id="svelteCount"></strong></p>
</div>
</sections>
<br>
<div>
<p>
<button id="openPopup">
Abrir popup
</button>
</p>
<p>
<button id="msgPopup">
Enviar mensagem para Popup
</button>
</p>
<p>
Contagem da popup: <strong id="popupCount"></strong>
</p>
</div>
<div>
<p>
<button id="msgAll">
Enviar mensagem para qualquer um
</button>
</p>
</div>
<script>
function createIframe(url, locationId) {
const location = document.getElementById(locationId);
const el = document.createElement('iframe');
el.setAttribute('src', url);
el.setAttribute('frameborder', 0);
location.appendChild(el);
return el;
}
const vueUrl = 'http://localhost:5173';
const svelteUrl = 'http://localhost:5174';
const popupUrl = 'http://localhost:5175';
const svelteIframe = createIframe(svelteUrl, 'svelteCol');
const vueIframe = createIframe(vueUrl, 'vueCol');
const vueCountEl = document.getElementById('vueCount');
const svelteCountEl = document.getElementById('svelteCount');
const popupCountEl = document.getElementById('popupCount');
vueCountEl.textContent = 0;
svelteCountEl.textContent = 0;
popupCountEl.textContent = 0;
document.getElementById('msgVue').addEventListener('click', (event) => {
vueIframe.contentWindow.postMessage(
'Enviado para o Vue',
vueUrl,
);
});
document.getElementById('msgSvelte').addEventListener('click', (event) => {
svelteIframe.contentWindow.postMessage(
'Enviado para o Svelte',
svelteUrl,
);
});
window.addEventListener('message', (event) => {
if (event.origin === vueUrl) {
vueCountEl.textContent = event.data;
}
if (event.origin === svelteUrl) {
svelteCountEl.textContent = event.data;
}
if (event.origin === popupUrl) {
popupCountEl.textContent = event.data;
}
return
}, false);
document.getElementById('openPopup').addEventListener('click', (event) => {
const popup = window.open(popupUrl);
document.getElementById('msgPopup').addEventListener('click', (event) => {
popup.postMessage(
'Enviado para o Popup',
popupUrl,
);
});
document.getElementById('msgAll').addEventListener('click', () => {
popup.postMessage(
'Enviado para qualquer um',
'*',
);
});
});
</script>
</body>
</html>