UNPKG

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