• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<body></body>
2<script>
3  const BC0_FIRST_MSG = 'from BC0 - first';
4  const BC1_FIRST_MSG = 'from BC1 - first';
5  const BC2_FIRST_MSG = 'from BC2 - first';
6  const BC3_FIRST_MSG = 'from BC3 - first';
7  const BC0_SECOND_MSG = 'from BC0 - second';
8  const BC1_SECOND_MSG = 'from BC1 - second';
9  const BC2_SECOND_MSG = 'from BC2 - second';
10  const BC3_SECOND_MSG = 'done';
11  const BC0_TARGET_NAME = 'BC1';
12  const BC1_TARGET_NAME = 'BC1';
13  const BC2_TARGET_NAME = 'BC2';
14  const BC3_TARGET_NAME = 'BC3';
15  const MULTI_FRAME_ORDERING_TEST_CHANNEL_NAME = 'multi-frame-order';
16
17  var bc1, bc2, bc3;
18  var sentMessageCountForBc1 = 0;
19  var sentMessageCountForBc2 = 0;
20  var sentMessageCountForBc3 = 0;
21
22  var bc1_handler = e => {
23    window.top.logReceivedMessage(BC1_TARGET_NAME, e);
24    switch(sentMessageCountForBc1) {
25      case 0:
26        bc3 = new BroadcastChannel(MULTI_FRAME_ORDERING_TEST_CHANNEL_NAME);
27        bc3.onmessage = bc3_handler;
28        bc1.postMessage(BC1_FIRST_MSG);
29        break;
30      case 1:
31        bc1.postMessage(BC1_SECOND_MSG);
32        break;
33      case 2:
34        bc1.close();
35        return;
36    }
37    sentMessageCountForBc1 += 1;
38  }
39  var bc2_handler = e => {
40    window.top.logReceivedMessage(BC2_TARGET_NAME, e);
41    switch(sentMessageCountForBc2) {
42      case 0:
43        bc2.postMessage(BC2_FIRST_MSG);
44        bc2.postMessage(BC2_SECOND_MSG);
45        sentMessageCountForBc2 += 2;
46        break;
47      case 2:
48        bc2.close();
49        return;
50    }
51  };
52  var bc3_handler = e => {
53    window.top.logReceivedMessage(BC3_TARGET_NAME, e);
54    switch(sentMessageCountForBc3) {
55      case 0:
56        bc3.postMessage(BC3_FIRST_MSG);
57        break;
58      case 1:
59        bc3.postMessage(BC3_SECOND_MSG);
60        break;
61      case 2:
62        bc3.close();
63        return;
64    }
65    sentMessageCountForBc3 += 1;
66  };
67
68  window.onload = function() {
69    const params = new URLSearchParams(window.location.search);
70    if (params.get('id') === 'iframe1') {
71      bc1 = new BroadcastChannel(MULTI_FRAME_ORDERING_TEST_CHANNEL_NAME);
72      bc1.onmessage = bc1_handler;
73    } else if (params.get('id')  === 'iframe2') {
74      bc2 = new BroadcastChannel(MULTI_FRAME_ORDERING_TEST_CHANNEL_NAME);
75      bc2.onmessage = bc2_handler;
76    }
77  }
78</script>
79