@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
163 lines (162 loc) • 5.21 kB
JavaScript
import { logger as E } from "../../shared-lib/index.js";
import { getUser as ao } from "../Core/get-user.js";
import { BRAZE_MUST_BE_INITIALIZED_ERROR as w } from "../common/constants.js";
import r from "../managers/braze-instance.js";
import { keys as C } from "../util/code-utils.js";
import User from "../User/user.js";
export const buildBrazeBridge = (t, e) => {
const o = { display: {}, web: {} },
requestPushPermission = function () {
return function () {
const t = arguments;
import("../Push/request-push-permission.js").then((e) => {
r.ao()
? e.requestPushPermission(...Array.prototype.slice.call(t))
: E.error(w);
});
};
},
n = {
requestImmediateDataFlush: function () {
const t = arguments;
import("../Core/request-immediate-data-flush.js").then(
({ requestImmediateDataFlush: requestImmediateDataFlush }) => {
r.ao()
? requestImmediateDataFlush(...Array.prototype.slice.call(t))
: E.error(w);
},
);
},
logCustomEvent: function () {
const t = arguments;
import("../Core/log-custom-event.js").then(
({ logCustomEvent: logCustomEvent }) => {
if (!r.ao()) return void E.error(w);
logCustomEvent(...Array.prototype.slice.call(t));
},
);
},
logPurchase: function () {
const t = arguments;
import("../Core/log-purchase.js").then(
({ logPurchase: logPurchase }) => {
if (!r.ao()) return void E.error(w);
logPurchase(...Array.prototype.slice.call(t));
},
);
},
unregisterPush: function () {
const t = arguments;
import("../Push/unregister-push.js").then(
({ unregisterPush: unregisterPush }) => {
r.ao()
? unregisterPush(...Array.prototype.slice.call(t))
: E.error(w);
},
);
},
requestPushPermission: requestPushPermission(),
changeUser: function () {
const t = arguments;
import("../Core/change-user.js").then(({ changeUser: changeUser }) => {
if (!r.ao()) return void E.error(w);
changeUser(...Array.prototype.slice.call(t));
});
},
},
s = function (t) {
return function () {
n[t](...Array.prototype.slice.call(arguments));
};
};
for (const t of C(n)) o[t] = s(t);
const i = [
"setFirstName",
"setLastName",
"setEmail",
"setGender",
"setDateOfBirth",
"setCountry",
"setHomeCity",
"setEmailNotificationSubscriptionType",
"setLanguage",
"addAlias",
"setPushNotificationSubscriptionType",
"setPhoneNumber",
"setCustomUserAttribute",
"addToCustomAttributeArray",
"removeFromCustomAttributeArray",
"incrementCustomUserAttribute",
"setCustomLocationAttribute",
"addToSubscriptionGroup",
"removeFromSubscriptionGroup",
"setLineId",
],
u = function (t) {
return function () {
const e = ao();
e && e[t](...Array.prototype.slice.call(arguments));
};
},
c = {};
for (let t = 0; t < i.length; t++) c[i[t]] = u(i[t]);
o.getUser = function () {
return c;
};
const a = {},
m = function (r) {
return function () {
const o = arguments;
"function" != typeof e
? a[r](...Array.prototype.slice.call(o))
: e(t, function () {
a[r](...Array.prototype.slice.call(o));
});
};
},
f = o.display;
for (const t of C(a)) f[t] = m(t);
const l = { registerAppboyPushMessages: requestPushPermission() },
p = function (t) {
return function () {
l[t](...Array.prototype.slice.call(arguments));
};
},
y = o.web;
for (const t of C(l)) y[t] = p(t);
return (
(o.NotificationSubscriptionTypes = User.NotificationSubscriptionTypes), o
);
};
export const applyNonceToDynamicallyCreatedTags = (t, e, r) => {
const o = `([\\w]+)\\s*=\\s*document.createElement\\(['"]${r}['"]\\)`,
n = t.match(new RegExp(o));
if (n) {
const r = `${n[1]}.setAttribute("nonce", "${e}")`;
return `${t.slice(0, n.index + n[0].length)};${r};${t.slice(
n.index + n[0].length,
)}`;
}
return null;
};
export const attachHtmlToIframeWithNonce = (t, e, r) => {
let o = null;
if (null != r) {
(o = document.createElement("html")), (o.innerHTML = e || "");
const t = o.getElementsByTagName("style");
for (let e = 0; e < t.length; e++) t[e].setAttribute("nonce", r);
const n = o.getElementsByTagName("script");
for (let t = 0; t < n.length; t++) {
n[t].setAttribute("nonce", r),
(n[t].innerHTML = n[t].innerHTML.replace(
/<style>/g,
`<style nonce='${r}'>`,
));
const e = applyNonceToDynamicallyCreatedTags(n[t].innerHTML, r, "script");
e && (n[t].innerHTML = e);
const o = applyNonceToDynamicallyCreatedTags(n[t].innerHTML, r, "style");
o && (n[t].innerHTML = o);
}
}
t.srcdoc = o ? o.innerHTML : e || "";
};