-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ac-creative.js
79 lines (66 loc) · 1.78 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
/* eslint-disable @typescript-eslint/no-unused-vars */
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 =
'https://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];
var {
// Amp-ad width and height.
height: h,
width: w,
// Position in the viewport.
x: vx,
y: vy,
} = latestChange.boundingClientRect;
var {
// Visible width and height.
height: vh,
width: vw,
} = latestChange.intersectionRect;
// 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');
});
}