|
| 1 | +/* global NoticeManager */ |
| 2 | + |
| 3 | +// TODO: how do we know no ad has been loaded (but without an error)? |
| 4 | +const adLoaded = new ReactiveVar(false); |
| 5 | +let adElement = undefined; |
| 6 | + |
| 7 | +Meteor.startup(function () { |
| 8 | + const data = { |
| 9 | + placementid: '240165609743803_240166086410422', |
| 10 | + format: 'native', |
| 11 | + testmode: false, |
| 12 | + onAdLoaded: function(element) { |
| 13 | + console.log('Audience Network [240165609743803_240166086410422] ad loaded'); |
| 14 | + adElement = element; |
| 15 | + adLoaded.set(true); |
| 16 | + }, |
| 17 | + onAdError: function(errorCode, errorMessage) { |
| 18 | + console.log('Audience Network [240165609743803_240166086410422] error (' + errorCode + ') ' + errorMessage); |
| 19 | + adLoaded.set(false); |
| 20 | + }, |
| 21 | + }; |
| 22 | + |
| 23 | + (function(w, l, d, t) { |
| 24 | + const a = t(); |
| 25 | + const b = d.currentScript || (function() { |
| 26 | + const c = d.getElementsByTagName('script'); |
| 27 | + return c[c.length - 1]; |
| 28 | + })(); |
| 29 | + const e = b.parentElement; |
| 30 | + e.dataset.placementid = data.placementid; |
| 31 | + const f = function(v) { |
| 32 | + try { |
| 33 | + return v.document.referrer; |
| 34 | + } catch (e) { |
| 35 | + undefined; |
| 36 | + } |
| 37 | + return ''; |
| 38 | + }; |
| 39 | + const g = function(h) { |
| 40 | + const i = h.indexOf('/', h.indexOf('://') + 3); |
| 41 | + if (i === -1) { |
| 42 | + return h; |
| 43 | + } |
| 44 | + return h.substring(0, i); |
| 45 | + }; |
| 46 | + const j = [l.href]; |
| 47 | + let k = false; |
| 48 | + let m = false; |
| 49 | + if (w !== w.parent) { |
| 50 | + let n; |
| 51 | + let o = w; |
| 52 | + while (o !== n) { |
| 53 | + let h; |
| 54 | + try { |
| 55 | + m = m || (o.$sf && o.$sf.ext); |
| 56 | + h = o.location.href; |
| 57 | + } catch (e) { |
| 58 | + k = true; |
| 59 | + } |
| 60 | + j.push(h || f(n)); |
| 61 | + n = o; |
| 62 | + o = o.parent; |
| 63 | + } |
| 64 | + } |
| 65 | + const p = l.ancestorOrigins; |
| 66 | + if (p) { |
| 67 | + if (p.length > 0) { |
| 68 | + data.domain = p[p.length - 1]; |
| 69 | + } else { |
| 70 | + data.domain = g(j[j.length - 1]); |
| 71 | + } |
| 72 | + } |
| 73 | + data.url = j[j.length - 1]; |
| 74 | + data.channel = g(j[0]); |
| 75 | + data.width = screen.width; |
| 76 | + data.height = screen.height; |
| 77 | + data.pixelratio = w.devicePixelRatio; |
| 78 | + data.placementindex = w.ADNW && w.ADNW.Ads ? w.ADNW.Ads.length : 0; |
| 79 | + data.crossdomain = k; |
| 80 | + data.safeframe = !!m; |
| 81 | + const q = {}; |
| 82 | + q.iframe = e.firstElementChild; |
| 83 | + let r = 'https://www.facebook.com/audiencenetwork/web/?sdk=5.3'; |
| 84 | + for (const s in data) { |
| 85 | + q[s] = data[s]; |
| 86 | + if (typeof(data[s]) !== 'function') { |
| 87 | + r += '&' + s + '=' + encodeURIComponent(data[s]); |
| 88 | + } |
| 89 | + } |
| 90 | + q.iframe.src = r; |
| 91 | + q.tagJsInitTime = a; |
| 92 | + q.rootElement = e; |
| 93 | + q.events = []; |
| 94 | + w.addEventListener('message', function(u) { |
| 95 | + if (u.source !== q.iframe.contentWindow) { |
| 96 | + return; |
| 97 | + } |
| 98 | + u.data.receivedTimestamp = t(); |
| 99 | + if (this.sdkEventHandler) { |
| 100 | + this.sdkEventHandler(u.data); |
| 101 | + } else { |
| 102 | + this.events.push(u.data); |
| 103 | + } |
| 104 | + }.bind(q), false); |
| 105 | + q.tagJsIframeAppendedTime = t(); |
| 106 | + w.ADNW = w.ADNW || {}; |
| 107 | + w.ADNW.Ads = w.ADNW.Ads || []; |
| 108 | + w.ADNW.Ads.push(q); |
| 109 | + w.ADNW.init && w.ADNW.init(q); |
| 110 | + })(window, location, document, Date.now || function() { |
| 111 | + return +new Date; |
| 112 | + }); |
| 113 | + |
| 114 | + jQuery.getScript('https://connect.facebook.net/en_US/fbadnw.js'); |
| 115 | +}); |
| 116 | + |
| 117 | +NoticeManager.provide('ad', function() { |
| 118 | + if (!adLoaded.get()) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + adElement.style.display = 'block'; |
| 123 | + return { |
| 124 | + template: 'ad', |
| 125 | + }; |
| 126 | +}); |
0 commit comments