summaryrefslogtreecommitdiffstats
path: root/browser/extensions/webcompat/shims/optimizely.js
blob: dcda87421defc47bc1e927a98c9b7e9026f17bb7 (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
/* 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/. */

/**
 * Bug 1714431 - Shim Optimizely
 *
 * This shim stubs out window.optimizely for those sites which
 * break when it is not successfully loaded.
 */

if (!window.optimizely?.state) {
  const behavior = {
    query: () => [],
  };

  const dcp = {
    getAttributeValue() {},
    waitForAttributeValue: () => Promise.resolve(),
  };

  const data = {
    accountId: "",
    audiences: {},
    campaigns: {},
    clientName: "js",
    clientVersion: "0.166.0",
    dcpServiceId: null,
    events: {},
    experiments: {},
    groups: {},
    pages: {},
    projectId: "",
    revision: "",
    snippetId: null,
    variations: {},
  };

  const activationId = String(Date.now());

  const state = {
    getActivationId() {
      return activationId;
    },
    getActiveExperimentIds() {
      return [];
    },
    getCampaignStateLists() {
      return {};
    },
    getCampaignStates() {
      return {};
    },
    getDecisionObject() {
      return null;
    },
    getDecisionString() {
      return null;
    },
    getExperimentStates() {
      return {};
    },
    getPageStates() {
      return {};
    },
    getRedirectInfo() {
      return null;
    },
    getVariationMap() {
      return {};
    },
    isGlobalHoldback() {
      return false;
    },
  };

  const poll = (fn, to) => {
    setInterval(() => {
      try {
        fn();
      } catch (_) {}
    }, to);
  };

  const waitUntil = test => {
    let interval, resolve;
    function check() {
      try {
        if (test()) {
          clearInterval(interval);
          resolve?.();
          return true;
        }
      } catch (_) {}
      return false;
    }
    return new Promise(r => {
      resolve = r;
      if (check()) {
        resolve();
        return;
      }
      interval = setInterval(check, 250);
    });
  };

  const waitForElement = sel => {
    return waitUntil(() => {
      document.querySelector(sel);
    });
  };

  const observeSelector = (sel, fn, opts) => {
    let interval;
    const observed = new Set();
    function check() {
      try {
        for (const e of document.querySelectorAll(sel)) {
          if (observed.has(e)) {
            continue;
          }
          observed.add(e);
          try {
            fn(e);
          } catch (_) {}
          if (opts.once) {
            clearInterval(interval);
          }
        }
      } catch (_) {}
    }
    interval = setInterval(check, 250);
    const timeout = { opts };
    if (timeout) {
      setTimeout(() => {
        clearInterval(interval);
      }, timeout);
    }
  };

  const utils = {
    Promise: window.Promise,
    observeSelector,
    poll,
    waitForElement,
    waitUntil,
  };

  const visitorId = {
    randomId: "",
  };

  let browserVersion = "";
  try {
    browserVersion = navigator.userAgent.match(/rv:(.*)\)/)[1];
  } catch (_) {}

  const visitor = {
    browserId: "ff",
    browserVersion,
    currentTimestamp: Date.now(),
    custom: {},
    customBehavior: {},
    device: "desktop",
    device_type: "desktop_laptop",
    events: [],
    first_session: true,
    offset: 240,
    referrer: null,
    source_type: "direct",
    visitorId,
  };

  window.optimizely = {
    data: {
      note: "Obsolete, use optimizely.get('data') instead",
    },
    get(e) {
      switch (e) {
        case "behavior":
          return behavior;
        case "data":
          return data;
        case "dcp":
          return dcp;
        case "jquery":
          throw new Error("jQuery not implemented");
        case "session":
          return undefined;
        case "state":
          return state;
        case "utils":
          return utils;
        case "visitor":
          return visitor;
        case "visitor_id":
          return visitorId;
      }
      return undefined;
    },
    initialized: true,
    push() {},
    state: {},
  };
}