UNPKG

3.07 kBJavaScriptView Raw
1/*
2 * This file is used to share the snippet injection code between PSClickWrap.js and PSBrowseWrap.js. It is mostly the same as the existing snippet code, only exception is the
3 * PS.JS source location is paramaterized.
4 */
5
6function injectSnippet(scriptURL, backupScriptURL) {
7 (function (w, d, s, c, f, n, t, g, a, b, l) {
8 // Defines the global _ps object and initializes the _ps() function
9 // that will queue commands until the PactSafe Library is ready.
10 w.PactSafeObject = n;
11 w[n] = w[n] || function () {
12 (w[n].q = w[n].q || []).push(arguments);
13 }, w[n].on = function () {
14 // Defines the event functions for the global _ps object.
15 (w[n].e = w[n].e || []).push(arguments);
16 }, w[n].once = function () {
17 (w[n].eo = w[n].eo || []).push(arguments);
18 }, w[n].off = function () {
19 (w[n].o = w[n].o || []).push(arguments);
20 }, w[n].t = 1 * new Date(), w[n].l = 0; // Marks the time that the script is inserted.
21
22 // Inserts a new script element to load the PactSafe Library JS file (ps.js).
23 a = d.createElement(s);
24 b = d.getElementsByTagName(s)[0];
25 a.async = 1;
26 a.src = c;
27
28 // Marks that the script has started loading or failed to load.
29 a.onload = a.onreadystatechange = function () {
30 w[n].l = 1;
31 };
32 a.onerror = a.onabort = function () {
33 w[n].l = 0;
34 };
35
36 // Insert the script tag to the DOM, n a testing context, no script tags exist so b is undefined.
37 if (b) {
38 b.parentNode.insertBefore(a, b);
39 } else {
40 document.body.appendChild(a);
41 }
42
43 // Retry loading the script from a fallback location after 4 seconds.
44 setTimeout(function () {
45 if (!w[n].l && !w[n].loaded) {
46 w[n].error = 1;
47 a = d.createElement(s);
48 a.async = 1;
49 a.src = f;
50 a.onload = a.onreadystatechange = function () {
51 w[n].l = 1;
52 };
53 a.onerror = a.onabort = function () {
54 w[n].l = 0;
55 };
56 b.parentNode.insertBefore(a, b);
57
58 // Log the loading error via beacon.
59 l = function l(u, e) {
60 try {
61 e = d.createElement("img");
62 e.src = "https://d3r8bdci515tjv.cloudfront.net/error.gif?t=" + w[n].t + "&u=" + encodeURIComponent(u);
63 d.getElementsByTagName("body")[0].appendChild(e);
64 } catch (x) {}
65 };
66 l(c);
67
68 // Call the optional error callback function after a second failed attempt.
69 setTimeout(function () {
70 if (!w[n].l && !w[n].loaded) {
71 w[n].error = 1;
72 if (g && "function" == typeof g) {
73 g.call(this);
74 }
75 l(f);
76 }
77 }, t);
78 }
79 }, t);
80 })(window, document, "script", scriptURL, backupScriptURL, "_ps", 4000, function optionalErrorCallback() {
81 console.log("Unable to load the PactSafe PS.JS Library.");
82 });
83}
84
85function isSnippetLoaded(psScriptURL) {
86 var scripts = document.getElementsByTagName('script');
87 if (window._ps && window._ps.loaded && window._ps.realThang === 317) return true;
88 for (var i = 0; i < scripts.length; i += 1) {
89 if (scripts[i].src.indexOf(psScriptURL) !== -1) {
90 return true;
91 }
92 }
93 return false;
94}
95
96module.exports = {
97 injectSnippet: injectSnippet,
98 isSnippetLoaded: isSnippetLoaded
99};
\No newline at end of file