/
app.html
130 lines (110 loc) · 2.78 KB
/
app.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
<!DOCTYPE html>
<html>
<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, maximum-scale=1.0, user-scalable=no">
<title>External App</title>
<style type="text/css">
html {
background-color: green;
}
html,
body {
margin: 0;
padding: 0;
}
.bloco {
width: 100px;
height: 100px;
background-color: blue;
overflow: hidden;
}
</style>
</head>
<body>
<main role="main">
<h1>External App</h1>
<button type="button" id="add">Add</button>
<button type="button" id="rm">Remove</button>
</main>
<script type="text/javascript">
'use strict';
// App stuff
var cc = 0;
var btnAdd = document.querySelector('#add');
var btnRm = document.querySelector('#rm');
btnAdd.addEventListener('click', function() {
btnRm.insertAdjacentHTML('afterend', '<div class="bloco">' + cc + '</div>');
cc++;
}, false);
btnRm.addEventListener('click', function() {
var b = document.querySelector('.bloco');
if (b) {
b.remove();
cc--;
}
}, false);
// End App stuff
// ---
// postMessage
var source, origin, intervalo;
var html = document.querySelector('html');
var body = document.querySelector('body');
var currentHeight = 0;
var clientMsg = 'TExClient';
var isInIframe = (window.parent !== window);
var hasRAF = 'requestAnimationFrame' in window;
function updateHeight() {
var bodyBounds = body.getBoundingClientRect();
var htmlBounds = html.getBoundingClientRect();
return Math.max(htmlBounds.height, bodyBounds.height);
}
function envia(h) {
if (source && origin) {
source.postMessage(h, origin);
}
}
function checkHeight() {
var h = updateHeight();
if (currentHeight !== h) {
currentHeight = h;
envia(currentHeight);
}
if (hasRAF) {
window.requestAnimationFrame(checkHeight);
}
}
function recebe(event) {
if (event.data === clientMsg) {
checkHeight();
}
}
function initObserver() {
if (hasRAF) {
window.requestAnimationFrame(checkHeight);
} else {
intervalo = setInterval(checkHeight, 100);
}
}
function waitingParentOrigin(event) {
if (event.data === 'luke i am your father') {
origin = event.origin;
source = event.source;
initObserver();
window.removeEventListener('message', waitingParentOrigin, false);
window.addEventListener('message', recebe, false);
source.postMessage(
JSON.stringify({
'app': 'TExAppReady',
'msg': clientMsg,
'origin': window.location.origin
}), origin);
}
}
if (isInIframe) {
window.addEventListener('message', waitingParentOrigin, false);
}
</script>
</body>
</html>