PageSourceSearch

https://www.edigest.hk/wp-content/plugins/nmg-lazy-load/assets/js/…09.1e0a45a50f4e9db54a87b7f60f3fd710a1.6.141

js edigest.hk collected 2026-09-24 09:25:12 UTC 14,317 bytes, 467 lines download raw bytes

1/**
2 * Adgpt: google DFP adapter.
3 * version: 2.4.2
4 * Author: levi
5 *plugin url:https://support.google.com/dfp_premium/answer/4578089?hl=zh-Hans
6 *prebid url:http://prebid.org/dev-docs/examples/adunit-refresh.html
7 * desc: 广告流程: 
8 1.引入GFP 资源库  <script async src="https://www.googletagservices.com/tag/js/gpt.js"></script>
9 2.定义广告 defineSlot
10 3.开启服务资源 googletag.enableServices();
11 4.setTargeting  
12 4.显示广告 googletag.display('bottomAd');
13 5.刷新广告 googletag.pubads().refresh([bottomSlot])
14 6.清除广告 googletag.pubads().clear();
15 */
16
17;(function($, window) {
18
19	'use strict'
20
21	window.Adgpt = window.Adgpt || function(){};
22
23	Adgpt.defaults = {
24		deviceList: ['all', 'desktop', 'mobile', 'tablet'], //device list for device filter, default all
25		orientationList: ['all', 'portrait', 'landscape'], //orientation list for orientation filter, default all
26		async: true, //whether render DPF async
27		adSelector: '._gpt_ads', //the class selector of slot div 
28		containerSelector: 'article', //the class selector of post div 
29		dataKey: 'from', //the key of clone data
30		prebidTimeout: 700, //prebid timeout
31	};
32
33	//device info, window.getDeviceInfo function provided too
34	Adgpt.deviceInfo = {device: '', orientation: ''};
35	Adgpt.handles = {}; //slot queue
36	Adgpt.confs = {}; // config queue
37	Adgpt.targets = []; //target array
38	Adgpt.includeIds = []; //included slot id queue
39	Adgpt.excludeIds = []; //excluded slot id queue
40	Adgpt.prebid = function(){};
41
42	Adgpt.init = function(conf) {
43		$.extend(Adgpt.defaults, conf);
44		Adgpt._bindEvent();
45		Adgpt._initDeviceDetect();
46    	Adgpt._initDFP();
47	};
48
49	//init DFP
50	Adgpt._initDFP = function() {
51		var the = this;
52		googletag.cmd.push(function() {
53			$(document).triggerHandler('Adgpt.beforeInit', [the]);
54			if (Adgpt.defaults.async) {
55				googletag.pubads().disableInitialLoad();
56				//googletag.pubads().enableSingleRequest();
57				googletag.pubads().collapseEmptyDivs(true, true);
58				googletag.pubads().addEventListener('slotRenderEnded', function(event) {
59					return false;
60				});
61			}
62			googletag.enableServices();  //开启广告服务
63		});
64		Adgpt.display(); //显示广告
65		Adgpt.refresh(); //刷新广告
66		googletag.cmd.push(function() {
67			$(document).triggerHandler('Adgpt.afterInit', [the]);  //设置监控点
68		});
69	};
70
71	Adgpt._bindEvent = function () {
72		//extra processing, 
73    	$(document).on('Adgpt.afterInit', function() {
74    		$(Adgpt.defaults.containerSelector, document.body).find(Adgpt.defaults.adSelector).each(function() {
75				Adgpt.updateFilter('exclude', $(this).attr('id'));
76			});
77    	});
78	};
79
80	//init device detector
81	Adgpt._initDeviceDetect = function() {
82		Adgpt.loadDeviceInfo();
83		if ($.type(window.getDeviceInfo) != 'function') {
84			window.addEventListener('onorientationchange' in window ? 'orientationchange' : 'resize', Adgpt.loadDeviceInfo);
85		}
86	};
87
88	//the slot filters //过滤系统 
89	Adgpt._filterBus = function(slotIds) {
90		slotIds = Adgpt._includeFilter(slotIds);
91		slotIds = Adgpt._excludeFilter(slotIds); //广告只refresh一次
92		slotIds = Adgpt._deviceFilter(slotIds);
93		slotIds = Adgpt._slotDivFilter(slotIds);
94		return slotIds;
95	};
96
97	//include filter
98	Adgpt._includeFilter = function(slotIds) {
99		if (Adgpt.includeIds.length) {
100			slotIds = $.grep(slotIds, function(n, i) {
101				return $.inArray(n, Adgpt.includeIds) === -1;
102			}, true);
103		}
104		return slotIds;
105	};
106
107	//exclude filter
108	Adgpt._excludeFilter = function(slotIds) {
109		if (Adgpt.excludeIds.length) {
110			slotIds = $.grep(slotIds, function(n, i) {
111				return $.inArray(n, Adgpt.excludeIds) === -1;
112			});
113		}
114		return slotIds;
115	};
116
117	//device filter
118	Adgpt._deviceFilter = function(slotIds) {
119		Adgpt.loadDeviceInfo();
120		if (!$.isEmptyObject(Adgpt.confs)) {
121			slotIds = $.grep(slotIds, function(n, i) {
122				if ($.type(Adgpt.confs[n]) == 'undefined') return false;
123				if ($.type(Adgpt.confs[n].device) == 'string') {
124					if (Adgpt.confs[n].device == 'all') return true;
125					if (Adgpt.confs[n].orientation == 'all') {
126						return Adgpt.deviceInfo.device == Adgpt.confs[n].device;
127					} else {
128						return Adgpt.deviceInfo.device == Adgpt.confs[n].device && Adgpt.deviceInfo.orientation == Adgpt.confs[n].orientation;
129					}
130				} else if ($.type(Adgpt.confs[n].device) == 'array') {
131					if ($.inArray('all', Adgpt.confs[n].device) !== -1) return true;
132					if ($.inArray('all', Adgpt.confs[n].orientation) !== -1) {
133						return $.inArray(Adgpt.deviceInfo.device, Adgpt.confs[n].device) !== -1;
134					} else {
135						return $.inArray(Adgpt.deviceInfo.device, Adgpt.confs[n].device) !== -1 && $.inArray(Adgpt.deviceInfo.orientation, Adgpt.confs[n].orientation) !== -1;
136					}
137				}
138				return false;
139			});
140		}
141		return slotIds;
142	};
143
144	//slot div filter
145	Adgpt._slotDivFilter = function(slotIds) {
146		slotIds = $.grep(slotIds, function(n, i) {
147			return $('#' + n).length ? true : false;
148		});
149		return slotIds;
150	};
151
152	//custom array unique func
153	Adgpt._uniqueArray = function(arr) {
154		var res = [],
155			hash = {};
156	    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
157	    	if (hash[elem]) continue;
158	        res.push(elem);
159	        hash[elem] = true;
160	    }
161	    return res;
162	};
163
164	//computes the difference of arrays
165	Adgpt._diff = function(arr1, arr2) {
166		return arr1.filter(function(i) {
167			return arr2.indexOf(i) < 0;
168		});
169	};
170
171	//detect the device by comparing patterns against User-Agent string.
172	Adgpt._detectDevice = function() {
173		var md = new MobileDetect(window.navigator.userAgent);
174		if (md.mobile()) {
175			if (md.phone()) {
176				Adgpt.deviceInfo.device = Adgpt.defaults.deviceList[2];
177			} else if (md.tablet()) {
178				Adgpt.deviceInfo.device = Adgpt.defaults.deviceList[3];
179			} else {
180				return false;
181			}
182		} else {
183			Adgpt.deviceInfo.device = Adgpt.defaults.deviceList[1];
184		}
185	};
186
187	//detect the device orientation
188	Adgpt._detectOrientation = function() {
189		if (window.orientation === 180 || window.orientation === 0) {
190			Adgpt.deviceInfo.orientation = Adgpt.defaults.orientationList[2];
191		} else if (window.orientation === 90 || window.orientation === -90) { 
192			Adgpt.deviceInfo.orientation = Adgpt.defaults.orientationList[1];
193        } else {
194        	Adgpt.deviceInfo.orientation = Adgpt.defaults.orientationList[0];
195        }
196	};
197
198	//load device info immediately
199	Adgpt.loadDeviceInfo = function() {
200		if ($.type(window.getDeviceInfo) == 'function') {
201			Adgpt.deviceInfo = window.getDeviceInfo();
202		} else {
203			Adgpt._detectDevice();
204			Adgpt._detectOrientation();
205		}
206	};
207
208	//update the include and exclude array
209	Adgpt.updateFilter = function(name, value) {
210		switch (name) {
211			case 'include':
212				if ($.type(value) == 'array') {
213					value = Adgpt._uniqueArray(value);
214					Adgpt['includeIds'] = Adgpt['includeIds'].concat(value);
215				} else {
216					Adgpt['includeIds'].push(value);
217				}
218				break;
219			case 'exclude':
220				if ($.type(value) == 'array') {
221					value = Adgpt._uniqueArray(value);
222					Adgpt['excludeIds'] = Adgpt['excludeIds'].concat(value);
223				} else {
224					Adgpt['excludeIds'].push(value);
225				}
226				break;
227			default:
228		}
229	};
230
231	//define slots and add to handle queue
232	Adgpt.defineSlot = function(conf) {
233		if ($.type(conf) == 'object') {
234			if ($.type(conf.device) == 'undefined') {
235				conf.device = Adgpt.defaults.deviceList[0];
236			}
237			if ($.type(conf.orientation) == 'undefined') {
238				conf.orientation = Adgpt.defaults.orientationList[0];
239			}
240			if ($.type(Adgpt.confs[conf.slotId]) == 'undefined') {
241				Adgpt.confs[conf.slotId] = conf;
242			}
243
244		}
245	};
246
247	//destroy slots by handle queue
248	Adgpt.destroySlots = function(slotIds) {
249		if ($.type(slotIds) == 'array') {
250			slotIds = Adgpt._uniqueArray(slotIds);
251			googletag.cmd.push(function() {
252				var slots = [];
253				for (var i in slotIds) {
254					if ($.type(Adgpt.handles[slotIds[i]]) == 'undefined') continue;
255					slots.push(Adgpt.handles[slotIds[i]]);
256				}
257				if (!slots.length) return;
258				googletag.destroySlots(slots);
259			});
260		} else {
261			googletag.cmd.push(function() {
262				googletag.destroySlots();
263			});
264		}
265	};
266
267	//display with define function merged, etc.
268	Adgpt._display = function(slotIds) {
269		slotIds = Adgpt._filterBus(slotIds);
270		for (var i in slotIds) {
271			//define slot
272			if ($.type(Adgpt.handles[slotIds[i]]) == 'undefined') {
273				Adgpt.handles[slotIds[i]] = googletag.defineSlot(Adgpt.confs[slotIds[i]].slotPath, Adgpt.confs[slotIds[i]].slotSize, slotIds[i]).addService(googletag.pubads());
274				Adgpt.prebid.addAdUnits([slotIds[i]]); //add ad unit by prebid
275			}
276			//set targeting
277			Adgpt._setTargeting(slotIds[i]);
278			//display slot
279			if ($.type(Adgpt.handles[slotIds[i]]) != 'undefined') {
280				googletag.display(slotIds[i]);
281			}
282		}
283		//after slot display trigger
284		$(document).triggerHandler('Adgpt.afterDisplay', [slotIds, Adgpt]);
285	}
286
287	//display slots by slot id array
288	Adgpt.display = function(slotIds) {
289		if ($.type(slotIds) == 'array') {
290			slotIds = Adgpt._uniqueArray(slotIds);
291			googletag.cmd.push(function() {
292				Adgpt._display(slotIds);
293			});
294		} else {
295			googletag.cmd.push(function() {
296				slotIds = [];
297				for (var i in Adgpt.confs) {
298					slotIds.push(i);
299				}
300				Adgpt._display(slotIds);
301			});
302		}
303	};
304
305	//use google refresh or prebid refresh to display slots
306	Adgpt.refresh = function(slotIds, changeCorrelator) {
307		if (!Adgpt.defaults.async) return;
308		changeCorrelator = $.type(changeCorrelator) == 'boolean' ? changeCorrelator : true;
309		if ($.type(slotIds) == 'array') {
310			slotIds = Adgpt._uniqueArray(slotIds);
311			googletag.cmd.push(function() {
312				slotIds = Adgpt._filterBus(slotIds);
313				var prebidSlotIds = Adgpt.prebid._filter(slotIds);
314				Adgpt.prebid.refreshBid(prebidSlotIds, changeCorrelator);
315				Adgpt._refresh(Adgpt._diff(slotIds, prebidSlotIds), changeCorrelator);
316			});
317		} else {
318			googletag.cmd.push(function() {
319				var allSlots = googletag.pubads().getSlots(),
320					slotIds = [],
321					prebidSlotIds = [];
322				for (var i in allSlots) {
323					slotIds.push(allSlots[i].getSlotElementId());
324				}
325				slotIds = Adgpt._filterBus(slotIds);
326				prebidSlotIds = Adgpt.prebid._filter(slotIds);
327				Adgpt.prebid.refreshBid(prebidSlotIds, changeCorrelator);
328				Adgpt._refresh(Adgpt._diff(slotIds, prebidSlotIds), changeCorrelator);
329			});
330		}
331	};
332
333	//refresh and show the DFP with slot id array
334	Adgpt._refresh = function(slotIds, changeCorrelator) {
335		var slots = [];
336		for (var i in slotIds) {
337			if ($.type(Adgpt.handles[slotIds[i]]) == 'undefined') continue;
338			slots.push(Adgpt.handles[slotIds[i]]);
339		}
340		if (!slots.length) return;
341		googletag.pubads().refresh(slots, {changeCorrelator: changeCorrelator});
342	};
343
344	//clear and close the slot
345	Adgpt.clear = function(slotIds) {
346		if ($.type(slotIds) == 'array') {
347			slotIds = Adgpt._uniqueArray(slotIds);
348			googletag.cmd.push(function() {
349				slotIds = Adgpt._filterBus(slotIds);
350				var slots = [];
351				for (var i in slotIds) {
352					if ($.type(Adgpt.handles[slotIds[i]]) == 'undefined') continue;
353					slots.push(Adgpt.handles[slotIds[i]]);
354				}
355				if (!slots.length) return;
356				googletag.pubads().clear(slots);
357			});
358		} else {
359			googletag.cmd.push(function() {
360				googletag.pubads().clear();
361			});
362		}
363	};
364
365	//set targeting parameters queue
366	Adgpt.setTargeting = function(slotId, params) {
367		if ($.type(Adgpt.confs[slotId]) == 'undefined' || $.type(params) != 'object') return;
368		Adgpt.targets.push({
369			slotId: slotId,
370			params: params,
371			isset: false
372		});
373	};
374
375	//set targeting parameters by slot id
376	Adgpt._setTargeting = function(slotId) {
377		var res = false;
378		for (var i in Adgpt.targets) {
379			if (Adgpt.targets[i].slotId != slotId || Adgpt.targets[i].isset) continue;
380			for (var k in Adgpt.targets[i].params) {
381				if ($.type(Adgpt.handles[Adgpt.targets[i].slotId]) == 'undefined') continue;
382				Adgpt.handles[Adgpt.targets[i].slotId].setTargeting(k, Adgpt.targets[i].params[k]);
383				Adgpt.targets[i].isset = true;
384				res = true;
385			}
386		}
387		return res;
388	};
389
390	//get slot ids that has bids params
391	Adgpt.prebid._filter = function(slotIds) {
392		var pSlotIds = [];
393		if ($.type(window.pbjs) == 'undefined' || !slotIds.length) return pSlotIds;
394		for (var i in slotIds) {
395			if ($.type(Adgpt.confs[slotIds[i]].bids) == 'undefined') continue;
396			pSlotIds.push(slotIds[i]);
397		}
398		return pSlotIds;
399	};
400
401	//add Prebid ad units by slot ids
402	Adgpt.prebid.addAdUnits = function(slotIds) {
403		slotIds = Adgpt.prebid._filter(slotIds);
404		if ($.type(window.pbjs) == 'undefined' || !slotIds.length) return;
405		var adUnits = [];
406		for (var i in slotIds) {
407			if($.type(Adgpt.confs[slotIds[i]].renderer) != 'undefined'){
408				adUnits.push({
409					code: slotIds[i],
410					mediaTypes: Adgpt.confs[slotIds[i]].mediaTypes,
411					bids: Adgpt.prebid.convertBids(slotIds[i]),
412					renderer: Adgpt.confs[slotIds[i]].renderer
413				});
414			}else{
415				adUnits.push({
416					code: slotIds[i],
417					mediaTypes: Adgpt.confs[slotIds[i]].mediaTypes,
418					bids: Adgpt.prebid.convertBids(slotIds[i])
419				});
420			}
421			
422		}
423		Adgpt.prebid._addAdUnits(adUnits);
424	};
425
426	//add Prebid ad unit
427	Adgpt.prebid._addAdUnits = function(adUnits) {
428		if ($.type(window.pbjs) == 'undefined' || !adUnits.length) return;
429		pbjs.que.push(function() {
430			pbjs.addAdUnits(adUnits);
431		});
432	};
433
434	//set targeting and refresh ad unit
435	Adgpt.prebid.refreshBid = function(slotIds, changeCorrelator) {
436		if ($.type(window.pbjs) == 'undefined' || !slotIds.length) return;
437		pbjs.que.push(function() {
438			pbjs.requestBids({
439				timeout: Adgpt.defaults.prebidTimeout,
440				adUnitCodes: slotIds,
441				bidsBackHandler: function() {
442					pbjs.setTargetingForGPTAsync(slotIds);
443					Adgpt._refresh(slotIds, changeCorrelator);
444				}
445			});
446		});
447	};
448
449	//convert bids format
450	Adgpt.prebid.convertBids = function(slotId) {
451		var bids = Adgpt.confs[slotId].bids;	
452		//paging bids
453		var slotIDArr = slotId.split('-'),
454			isPaging = slotIDArr.length == 7 ? true : false;
455		if (isPaging) {
456			var pageId = slotIDArr[slotIDArr.length - 1];
457			for (var i in bids) {
458				if (typeof(bids[i].bidder) == 'undefined' || bids[i].bidder != 'audienceNetwork')continue;
459				if (typeof(audienceNetwork_lrec2_placementIds) != 'undefined' && typeof(audienceNetwork_lrec2_placementIds[pageId - 1]) != 'undefined') {
460					bids[i].params.placementId =  audienceNetwork_lrec2_placementIds[pageId - 1];
461				}
462			}
463		}
464		return bids;
465	};
466
467}(jQuery, window));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.