Repository navigation
Expand file tree
/
Copy pathiframe-b.html
More file actions
54 lines (45 loc) · 1.59 KB
/
Copy pathiframe-b.html
File metadata and controls
54 lines (45 loc) · 1.59 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
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>Iframe B</title>
</head>
<body>
<h2>Iframe B</h2>
<button id="sendToA">Emitir para iframe A (via host)</button>
<button id="sendHiToHost">Mandar oi para o host</button>
<div id="output"></div>
<script type="module">
import { IframeBus } from './iframe-bus.js';
const bus = new IframeBus('iframe-b', { debug: true });
const p = (text) => {
const p = document.createElement('p');
p.textContent = text;
return p;
}
// Recebe mensagem do host
bus.subscribe('from:host', (data) => {
document.getElementById('output').appendChild(p(`[Iframe B] Host disse: ${data.text}`));
});
bus.subscribe('from:host-to-b', (data) => {
document.getElementById('output').appendChild(p(`[Iframe B] Host disse para B: ${data.text}`));
});
// Recebe mensagem de A
bus.subscribe('from:iframe-a', (data) => {
document.getElementById('output').appendChild(p(`[Iframe B] A disse: ${data.text}`));
});
// Envia para A via tópico
document.getElementById('sendToA').onclick = () => {
bus.publish('from:iframe-b', { text: 'Oi A, aqui é o B!' });
};
// Também pode emitir algo para host, se quiser
document.getElementById('sendHiToHost').onclick = () => {
bus.publish('from:iframe', { text: 'Oi host, aqui é o B!' });
}
// Responder a requisição (opcional — só para mostrar que todos podem responder)
bus.reply('get:iframeBStatus', () => {
return { status: 'Iframe B está pronto' };
});
</script>
</body>
</html>