-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathac-creative.js
87 lines (71 loc) · 2.36 KB
/
ac-creative.js
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
if (!window.context){
// window.context doesn't exist yet, must perform steps to create it
// before using it
console.log("window.context NOT READY");
// must add listener for the creation of window.context
window.addEventListener('amp-windowContextCreated', function(){
console.log("window.context created and ready to use");
window.context.onResizeSuccess(resizeSuccessCallback);
window.context.onResizeDenied(resizeDeniedCallback);
});
// load ampcontext-lib.js which will create window.context
ampContextScript = document.createElement('script');
ampContextScript.src = "https://localhost:8000/dist.3p/current/ampcontext-lib.js";
document.head.appendChild(ampContextScript);
}
function intersectionCallback(payload){
var changes = payload.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 resizeSuccessCallback(requestedHeight, requestedWidth){
console.log("Success!");
console.log(this);
resizeTo(requestedHeight, requestedWidth);
console.log(requestedHeight);
console.log(requestedWidth);
}
function resizeTo(height, width){
this.innerWidth = width;
this.innerHeight = height;
}
function resizeDeniedCallback(requestedHeight, requestedWidth){
console.log("DENIED");
console.log(requestedHeight);
console.log(requestedWidth);
}
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;
}