forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathampcontext-creative.html
101 lines (88 loc) · 2.78 KB
/
ampcontext-creative.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
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
background: lightblue;
}
</style>
<script>
if (!window.context) {
// window.context doesn't exist yet, must perform steps to create it
// before using it
console.log('window.context NOT READY');
// load ampcontext-lib.js which will create window.context
ampContextScript = document.createElement('script');
ampContextScript.src =
'http://localhost:8000/dist.3p/current/ampcontext-lib.js';
document.head.appendChild(ampContextScript);
}
function intersectionCallback(changes) {
// Code below is simply an example.
var latestChange = changes[changes.length - 1];
// Amp-ad width and height.
var w = latestChange.boundingClientRect.width;
var h = latestChange.boundingClientRect.height;
// Visible width and height.
var vw = latestChange.intersectionRect.width;
var vh = latestChange.intersectionRect.height;
// Position in the viewport.
var vx = latestChange.boundingClientRect.x;
var vy = latestChange.boundingClientRect.y;
// Viewable percentage.
var viewablePerc = ((vw * vh) / (w * h)) * 100;
console.log(viewablePerc, w, h, vw, vh, vx, vy);
}
function dummyCallback(changes) {
console.log(changes);
}
var shouldStopVis = false;
var stopVisFunc;
var shouldStopInt = false;
var stopIntFunc;
function toggleObserveIntersection() {
if (shouldStopInt) {
stopIntFunc();
} else {
stopIntFunc = window.context.observeIntersection(
intersectionCallback
);
}
shouldStopInt = !shouldStopInt;
}
function toggleObserveVisibility() {
if (shouldStopVis) {
stopVisFunc();
} else {
stopVisFunc = window.context.observePageVisibility(dummyCallback);
}
shouldStopVis = !shouldStopVis;
}
function resizeAd() {
window.context
.requestResize(500, 600)
.then(function () {
console.log('Success!');
this.innerWidth = 500;
this.innerHeight = 600;
})
.catch(function () {
console.log('DENIED');
});
}
</script>
</head>
<body>
Test Ad using ampcontext.js to create window.context
<button onclick="toggleObserveIntersection()">
Toggle Observe Intersections
</button>
<button onclick="toggleObserveVisibility()">
Toggle Observe visibility
</button>
<button onclick="resizeAd()">resize ad</button>
<button onclick="console.log(window.context)">
console.log(window.context)
</button>
</body>
</html>