1 |
|
2 | const defaultPartytownForwardPropertySettings = {
|
3 | preserveBehavior: false
|
4 | };
|
5 |
|
6 | const resolvePartytownForwardProperty = propertyOrPropertyWithSettings => {
|
7 | if ("string" == typeof propertyOrPropertyWithSettings) {
|
8 | return [ propertyOrPropertyWithSettings, defaultPartytownForwardPropertySettings ];
|
9 | }
|
10 | const [property, settings = defaultPartytownForwardPropertySettings] = propertyOrPropertyWithSettings;
|
11 | return [ property, {
|
12 | ...defaultPartytownForwardPropertySettings,
|
13 | ...settings
|
14 | } ];
|
15 | };
|
16 |
|
17 | const arrayMethods = Object.freeze((obj => {
|
18 | const properties = new Set;
|
19 | let currentObj = obj;
|
20 | do {
|
21 | Object.getOwnPropertyNames(currentObj).forEach((item => {
|
22 | "function" == typeof currentObj[item] && properties.add(item);
|
23 | }));
|
24 | } while ((currentObj = Object.getPrototypeOf(currentObj)) !== Object.prototype);
|
25 | return Array.from(properties);
|
26 | })([]));
|
27 |
|
28 | !function(win, doc, nav, top, useAtomics, config, libPath, timeout, scripts, sandbox, mainForwardFn = win, isReady) {
|
29 | function ready() {
|
30 | if (!isReady) {
|
31 | isReady = 1;
|
32 | libPath = (config.lib || "/~partytown/") + (false !== config.debug ? "debug/" : "");
|
33 | if ("/" == libPath[0]) {
|
34 | scripts = doc.querySelectorAll('script[type="text/partytown"]');
|
35 | if (top != win) {
|
36 | top.dispatchEvent(new CustomEvent("pt1", {
|
37 | detail: win
|
38 | }));
|
39 | } else {
|
40 | timeout = setTimeout(fallback, 1e4);
|
41 | doc.addEventListener("pt0", clearFallback);
|
42 | useAtomics ? loadSandbox(1) : nav.serviceWorker ? nav.serviceWorker.register(libPath + (config.swPath || "partytown-sw.js"), {
|
43 | scope: libPath
|
44 | }).then((function(swRegistration) {
|
45 | if (swRegistration.active) {
|
46 | loadSandbox();
|
47 | } else if (swRegistration.installing) {
|
48 | swRegistration.installing.addEventListener("statechange", (function(ev) {
|
49 | "activated" == ev.target.state && loadSandbox();
|
50 | }));
|
51 | } else {
|
52 | console.warn(swRegistration);
|
53 | }
|
54 | }), console.error) : fallback();
|
55 | }
|
56 | } else {
|
57 | console.warn('Partytown config.lib url must start with "/"');
|
58 | }
|
59 | }
|
60 | }
|
61 | function loadSandbox(isAtomics) {
|
62 | sandbox = doc.createElement(isAtomics ? "script" : "iframe");
|
63 | win._pttab = Date.now();
|
64 | if (!isAtomics) {
|
65 | sandbox.style.display = "block";
|
66 | sandbox.style.width = "0";
|
67 | sandbox.style.height = "0";
|
68 | sandbox.style.border = "0";
|
69 | sandbox.style.visibility = "hidden";
|
70 | sandbox.setAttribute("aria-hidden", !0);
|
71 | }
|
72 | sandbox.src = libPath + "partytown-" + (isAtomics ? "atomics.js?v=0.10.2" : "sandbox-sw.html?" + win._pttab);
|
73 | doc.querySelector(config.sandboxParent || "body").appendChild(sandbox);
|
74 | }
|
75 | function fallback(i, script) {
|
76 | console.warn("Partytown script fallback");
|
77 | clearFallback();
|
78 | top == win && (config.forward || []).map((function(forwardProps) {
|
79 | const [property] = resolvePartytownForwardProperty(forwardProps);
|
80 | delete win[property.split(".")[0]];
|
81 | }));
|
82 | for (i = 0; i < scripts.length; i++) {
|
83 | script = doc.createElement("script");
|
84 | script.innerHTML = scripts[i].innerHTML;
|
85 | script.nonce = config.nonce;
|
86 | doc.head.appendChild(script);
|
87 | }
|
88 | sandbox && sandbox.parentNode.removeChild(sandbox);
|
89 | }
|
90 | function clearFallback() {
|
91 | clearTimeout(timeout);
|
92 | }
|
93 | config = win.partytown || {};
|
94 | top == win && (config.forward || []).map((function(forwardProps) {
|
95 | const [property, {preserveBehavior: preserveBehavior}] = resolvePartytownForwardProperty(forwardProps);
|
96 | mainForwardFn = win;
|
97 | property.split(".").map((function(_, i, forwardPropsArr) {
|
98 | mainForwardFn = mainForwardFn[forwardPropsArr[i]] = i + 1 < forwardPropsArr.length ? mainForwardFn[forwardPropsArr[i]] || (propertyName => arrayMethods.includes(propertyName) ? [] : {})(forwardPropsArr[i + 1]) : (() => {
|
99 | let originalFunction = null;
|
100 | if (preserveBehavior) {
|
101 | const {methodOrProperty: methodOrProperty, thisObject: thisObject} = ((window, properties) => {
|
102 | let thisObject = window;
|
103 | for (let i = 0; i < properties.length - 1; i += 1) {
|
104 | thisObject = thisObject[properties[i]];
|
105 | }
|
106 | return {
|
107 | thisObject: thisObject,
|
108 | methodOrProperty: properties.length > 0 ? thisObject[properties[properties.length - 1]] : void 0
|
109 | };
|
110 | })(win, forwardPropsArr);
|
111 | "function" == typeof methodOrProperty && (originalFunction = (...args) => methodOrProperty.apply(thisObject, ...args));
|
112 | }
|
113 | return function() {
|
114 | let returnValue;
|
115 | originalFunction && (returnValue = originalFunction(arguments));
|
116 | (win._ptf = win._ptf || []).push(forwardPropsArr, arguments);
|
117 | return returnValue;
|
118 | };
|
119 | })();
|
120 | }));
|
121 | }));
|
122 | if ("complete" == doc.readyState) {
|
123 | ready();
|
124 | } else {
|
125 | win.addEventListener("DOMContentLoaded", ready);
|
126 | win.addEventListener("load", ready);
|
127 | }
|
128 | }(window, document, navigator, top, window.crossOriginIsolated); |
\ | No newline at end of file |