-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathad-banner.js
More file actions
149 lines (123 loc) · 3.54 KB
/
ad-banner.js
File metadata and controls
149 lines (123 loc) · 3.54 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
;(function () {
'use strict'
let gptLoaded = !!window.googletag
var mediumUnit
const currentScript =
document.currentScript ||
(() => {
const scripts = document.getElementsByTagName('script')
return scripts[scripts.length - 1]
})()
const adSlot = currentScript.getAttribute('data-slot')
if (!adSlot) {
console.error('Ad banner script requires data-slot attribute')
return
}
let bannerContainer = null
let adTimeout = null
function generateRandomId() {
return 'div-gpt-ad-' + Date.now() + '-' + Math.floor(Math.random() * 1000000)
}
function injectGPTScripts() {
if (gptLoaded) return Promise.resolve()
return new Promise((resolve) => {
const gptScript = document.createElement('script')
gptScript.async = true
gptScript.src = 'https://securepubads.g.doubleclick.net/tag/js/gpt.js'
gptScript.crossOrigin = 'anonymous'
document.head.appendChild(gptScript)
const initScript = document.createElement('script')
initScript.textContent = `
window.googletag = window.googletag || {cmd: []};
googletag.cmd.push(function() {
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
`
document.head.appendChild(initScript)
gptLoaded = true
resolve()
})
}
function createAdBanner() {
const adId = generateRandomId()
bannerContainer = document.createElement('div')
bannerContainer.style.cssText = `
position: fixed;
bottom: 100px;
left: 10px;
width: 300px;
height: 250px;
z-index: 99999999999;
background: transparent;
transform: translateY(100%);
transition: transform 0.3s ease-out;
`
const closeButton = document.createElement('button')
closeButton.innerHTML = '×'
closeButton.style.cssText = `
position: absolute;
top: -22px;
right: 0;
background: #ffffff;
border: 1px solid #ccc;
border-radius: 50%;
font-size: 20px;
font-weight: bold;
color: #666;
cursor: pointer;
z-index: 999999999999;
line-height: 1;
padding: 0;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
`
closeButton.addEventListener('click', closeAndRecreate)
const adDiv = document.createElement('div')
adDiv.id = adId
adDiv.style.cssText = 'min-width: 300px; min-height: 250px;'
bannerContainer.appendChild(closeButton)
bannerContainer.appendChild(adDiv)
document.body.appendChild(bannerContainer)
setTimeout(() => {
bannerContainer.style.transform = 'translateY(0)'
}, 50)
window.googletag.cmd.push(function () {
mediumUnit = googletag.defineSlot(adSlot, [300, 250], adId)
mediumUnit.addService(googletag.pubads())
})
window.googletag.cmd.push(function () {
googletag.display(adId)
})
}
function closeAndRecreate() {
if (bannerContainer) {
bannerContainer.style.transform = 'translateY(100%)'
setTimeout(() => {
if (bannerContainer) {
bannerContainer.remove()
bannerContainer = null
}
}, 300)
}
if (adTimeout) {
clearTimeout(adTimeout)
}
adTimeout = setTimeout(() => {
createAdBanner()
}, 10000)
}
function init() {
injectGPTScripts().then(() => {
setTimeout(createAdBanner, 500)
})
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init)
} else {
init()
}
})()