forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inabox.host.html
96 lines (87 loc) · 2.9 KB
/
inabox.host.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<title>amp-inabox-host example</title>
</head>
<body>
<iframe
src="//ads.localhost:8000/examples/inabox.amp.max.html"
scrolling="no"
width="300" height="200">
</iframe>
<h2>Scroll down to see more ...</h2>
<div style="height: 100vh"></div>
<h2>Inabox nested in friendly iframe </h2>
<iframe
srcdoc="<iframe src='//ads.localhost:8000/examples/inabox.amp.max.html' width='300' height='200' scrolling='no' frameborder=0></iframe>"
scrolling="no"
marginwidth="0"
marginheight="0"
width="300" height="200">
</iframe>
<div style="height: 20vh"></div>
<h2>A huge doc inabox</h2>
<iframe
src="//ads.localhost:8000/examples/inabox-more.amp.max.html"
scrolling="no"
width="300" height="1000">
</iframe>
<script>
initInaboxHost(window);
// Enable amp-inabox APIs for all iframes
Array.prototype.push.apply(
window.ampInaboxIframes, document.querySelectorAll('iframe'));
/**
* A sample bootstrap script that is to be run in ads tag.
*
* @param {!Window} win
*/
function initInaboxHost(win) {
const hostScriptUrl = 'http://localhost:8000/dist/amp-inabox-host.js';
preloadScript(win, hostScriptUrl);
// Optional: preload a4a-v0.js as well.
// Keep all iframes that potentially can render AMP content in this array
win.ampInaboxIframes = win.ampInaboxIframes || [];
// Keep all amp-inabox post-messages in this array.
win.ampInaboxPendingMessages = win.ampInaboxPendingMessages || [];
let hostScriptRequested = false;
const listener = event => {
if (!isInaboxMessage(event.data)) {
return;
}
win.ampInaboxPendingMessages.push(event);
// Load and execute host script once.
if (hostScriptRequested) {
return;
}
hostScriptRequested = true;
// Load inabox-host.js when the 1st inabox message is received.
loadScript(win, hostScriptUrl, () => {
win.removeEventListener('message', listener);
console.log('a4a-host.js loaded.');
});
};
win.addEventListener('message', listener);
console.log('A4A host bootstrapped.');
}
function isInaboxMessage(message) {
return typeof message === 'string' && message.indexOf('amp-') == 0;
}
function preloadScript(win, url) {
const link = win.document.createElement('link');
link.rel = 'preload';
link.href = url;
link.as = 'script';
win.document.head.appendChild(link);
}
function loadScript(win, url, callback) {
const script = win.document.createElement('script');
script.src = url;
script.onload = callback;
win.document.body.appendChild(script);
}
</script>
</body>
</html>