summaryrefslogtreecommitdiffstats
path: root/browser/extensions/webcompat/shims/adnexus-ast.js
blob: ae07fa6a0363ffa5c540713925bd98862a448bef (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * Bug 1734130 - Shim AdNexus AST
 *
 * Some sites expect AST to successfully load, or they break.
 * This shim mitigates that breakage.
 */

if (!window.apntag?.loaded) {
  const anq = window.apntag?.anq || [];

  const gTags = new Map();
  const gAds = new Map();
  const gEventHandlers = {};

  const Ad = class {
    adType = "banner";
    auctionId = "-";
    banner = {
      width: 1,
      height: 1,
      content: "",
      trackers: {
        impression_urls: [],
        video_events: {},
      },
    };
    brandCategoryId = 0;
    buyerMemberId = 0;
    cpm = 0.1;
    cpm_publisher_currency = 0.1;
    creativeId = 0;
    dealId = undefined;
    height = 1;
    mediaSubtypeId = 1;
    mediaTypeId = 1;
    publisher_currency_code = "US";
    source = "-";
    tagId = -1;
    targetId = "";
    width = 1;

    constructor(tagId, targetId) {
      this.tagId = tagId;
      this.targetId = targetId;
    }
  };

  const fireAdEvent = (type, adObj) => {
    const { targetId } = adObj;
    const handlers = gEventHandlers[type]?.[targetId];
    if (!handlers) {
      return Promise.resolve();
    }
    const evt = { adObj, type };
    return new Promise(done => {
      setTimeout(() => {
        for (const cb of handlers) {
          try {
            cb(evt);
          } catch (e) {
            console.error(e);
          }
        }
        done();
      }, 1);
    });
  };

  const refreshTag = targetId => {
    const tag = gTags.get(targetId);
    if (!tag) {
      return;
    }
    if (!gAds.has(targetId)) {
      gAds.set(targetId, new Ad(tag.tagId, targetId));
    }
    const adObj = gAds.get(targetId);
    fireAdEvent("adRequested", adObj).then(() => {
      // TODO: do some sites expect adAvailable+adLoaded instead of adNoBid?
      fireAdEvent("adNoBid", adObj);
    });
  };

  const off = (type, targetId, cb) => {
    gEventHandlers[type]?.[targetId]?.delete(cb);
  };

  const on = (type, targetId, cb) => {
    gEventHandlers[type] = gEventHandlers[type] || {};
    gEventHandlers[type][targetId] =
      gEventHandlers[type][targetId] || new Set();
    gEventHandlers[type][targetId].add(cb);
  };

  const Tag = class {
    static #nextId = 0;
    debug = undefined;
    displayed = false;
    initialHeight = 1;
    initialWidth = 1;
    keywords = {};
    member = 0;
    showTagCalled = false;
    sizes = [];
    targetId = "";
    utCalled = true;
    utDivId = "";
    utiframeId = "";
    uuid = "";

    constructor(raw) {
      const { keywords, sizes, targetId } = raw;
      this.tagId = Tag.#nextId++;
      this.keywords = keywords || {};
      this.sizes = sizes || [];
      this.targetId = targetId || "";
    }
    modifyTag() {}
    off(type, cb) {
      off(type, this.targetId, cb);
    }
    on(type, cb) {
      on(type, this.targetId, cb);
    }
    setKeywords(kw) {
      this.keywords = kw;
    }
  };

  window.apntag = {
    anq,
    attachClickTrackers() {},
    checkAdAvailable() {},
    clearPageTargeting() {},
    clearRequest() {},
    collapseAd() {},
    debug: false,
    defineTag(dfn) {
      const { targetId } = dfn;
      if (!targetId) {
        return;
      }
      gTags.set(targetId, new Tag(dfn));
    },
    disableDebug() {},
    dongle: undefined,
    emitEvent(adObj, type) {
      fireAdEvent(type, adObj);
    },
    enableCookieSet() {},
    enableDebug() {},
    fireImpressionTrackers() {},
    getAdMarkup: () => "",
    getAdWrap() {},
    getAstVersion: () => "0.49.0",
    getPageTargeting() {},
    getTag(targetId) {
      return gTags.get(targetId);
    },
    handleCb() {},
    handleMediationBid() {},
    highlightAd() {},
    loaded: true,
    loadTags() {
      for (const tagName of gTags.keys()) {
        refreshTag(tagName);
      }
    },
    modifyTag() {},
    notify() {},
    offEvent(type, target, cb) {
      off(type, target, cb);
    },
    onEvent(type, target, cb) {
      on(type, target, cb);
    },
    recordErrorEvent() {},
    refresh() {},
    registerRenderer() {},
    requests: {},
    resizeAd() {},
    setEndpoint() {},
    setKeywords() {},
    setPageOpts() {},
    setPageTargeting() {},
    setSafeFrameConfig() {},
    setSizes() {},
    showTag() {},
  };

  const push = function (fn) {
    if (typeof fn === "function") {
      try {
        fn();
      } catch (e) {
        console.trace(e);
      }
    }
  };

  anq.push = push;

  anq.forEach(push);
}