@perspective-ai/sdk 1.1.0 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.cjs CHANGED
@@ -38,7 +38,7 @@ __export(browser_exports, {
38
38
  module.exports = __toCommonJS(browser_exports);
39
39
 
40
40
  // src/constants.ts
41
- var SDK_VERSION = "1.1.0";
41
+ var SDK_VERSION = "1.1.3";
42
42
  var FEATURES = {
43
43
  RESIZE: 1 << 0,
44
44
  // 0b0001
@@ -84,7 +84,9 @@ var DATA_ATTRS = {
84
84
  brand: "data-perspective-brand",
85
85
  brandDark: "data-perspective-brand-dark",
86
86
  theme: "data-perspective-theme",
87
- noStyle: "data-perspective-no-style"
87
+ noStyle: "data-perspective-no-style",
88
+ autoOpen: "data-perspective-auto-open",
89
+ showOnce: "data-perspective-show-once"
88
90
  };
89
91
  var MESSAGE_TYPES = {
90
92
  // SDK -> Iframe (initialization)
@@ -118,9 +120,78 @@ var THEME_VALUES = {
118
120
  system: "system"
119
121
  };
120
122
  var STORAGE_KEYS = {
121
- anonId: "perspective-anon-id"
123
+ anonId: "perspective-anon-id",
124
+ triggerShown: "perspective-trigger-shown"
122
125
  };
123
126
 
127
+ // src/triggers.ts
128
+ function parseTriggerAttr(value) {
129
+ const trimmed = value.trim();
130
+ if (trimmed.startsWith("timeout:")) {
131
+ const delay = parseInt(trimmed.slice("timeout:".length), 10);
132
+ if (isNaN(delay) || delay < 0) {
133
+ throw new Error(`Invalid timeout delay: "${value}"`);
134
+ }
135
+ return { type: "timeout", delay };
136
+ }
137
+ if (trimmed === "timeout") {
138
+ return { type: "timeout", delay: 5e3 };
139
+ }
140
+ if (trimmed === "exit-intent") {
141
+ return { type: "exit-intent" };
142
+ }
143
+ throw new Error(
144
+ `Unknown trigger type: "${value}". Expected "timeout:<ms>" or "exit-intent".`
145
+ );
146
+ }
147
+ function setupTrigger(config, callback) {
148
+ if (config.type === "timeout") {
149
+ const timer = setTimeout(callback, config.delay);
150
+ return () => clearTimeout(timer);
151
+ }
152
+ if (config.type === "exit-intent") {
153
+ const handler = (e) => {
154
+ if (e.clientY <= 0) {
155
+ callback();
156
+ document.removeEventListener("mouseleave", handler);
157
+ }
158
+ };
159
+ document.addEventListener("mouseleave", handler);
160
+ return () => document.removeEventListener("mouseleave", handler);
161
+ }
162
+ const _exhaustive = config;
163
+ throw new Error(
164
+ `Unknown trigger type: ${_exhaustive.type}`
165
+ );
166
+ }
167
+ function storageKey(researchId) {
168
+ return `${STORAGE_KEYS.triggerShown}:${researchId}`;
169
+ }
170
+ function parseShowOnceAttr(value) {
171
+ if (!value) return "session";
172
+ const trimmed = value.trim();
173
+ if (trimmed === "visitor") return "visitor";
174
+ if (trimmed === "false") return false;
175
+ return "session";
176
+ }
177
+ function shouldShow(researchId, showOnce) {
178
+ if (showOnce === false) return true;
179
+ try {
180
+ const storage = showOnce === "visitor" ? localStorage : sessionStorage;
181
+ return storage.getItem(storageKey(researchId)) === null;
182
+ } catch {
183
+ return true;
184
+ }
185
+ }
186
+ function markShown(researchId, showOnce) {
187
+ if (showOnce === false) return;
188
+ try {
189
+ const storage = showOnce === "visitor" ? localStorage : sessionStorage;
190
+ storage.setItem(storageKey(researchId), "1");
191
+ } catch {
192
+ }
193
+ }
194
+
124
195
  // src/config.ts
125
196
  var DEFAULT_HOST = "https://getperspective.ai";
126
197
  var globalConfig = {};
@@ -220,6 +291,8 @@ function hexToRgba(hex, alpha) {
220
291
  // src/iframe.ts
221
292
  function isAllowedRedirectUrl(url) {
222
293
  if (!url || typeof url !== "string") return false;
294
+ if (url.startsWith("/") && !url.startsWith("//") || url.startsWith("?") || url.startsWith("#"))
295
+ return true;
223
296
  try {
224
297
  const parsed = new URL(url, window.location.origin);
225
298
  const protocol = parsed.protocol.toLowerCase();
@@ -1598,6 +1671,7 @@ function createFullpage(config) {
1598
1671
 
1599
1672
  // src/browser.ts
1600
1673
  var instances = /* @__PURE__ */ new Map();
1674
+ var triggerCleanups = /* @__PURE__ */ new Map();
1601
1675
  var configCache = /* @__PURE__ */ new Map();
1602
1676
  var styledButtons = /* @__PURE__ */ new Map();
1603
1677
  var buttonThemeMediaQuery = null;
@@ -1777,8 +1851,13 @@ function destroy(researchId) {
1777
1851
  function destroyAll() {
1778
1852
  instances.forEach((instance) => instance.unmount());
1779
1853
  instances.clear();
1854
+ triggerCleanups.forEach((cleanup) => cleanup());
1855
+ triggerCleanups.clear();
1780
1856
  styledButtons.clear();
1781
1857
  teardownButtonThemeListener();
1858
+ if (hasDom()) {
1859
+ document.querySelectorAll("[data-perspective-initialized]").forEach((el) => el.removeAttribute("data-perspective-initialized"));
1860
+ }
1782
1861
  }
1783
1862
  function autoInit() {
1784
1863
  if (!hasDom()) return;
@@ -1803,9 +1882,29 @@ function autoInit() {
1803
1882
  if (el.hasAttribute("data-perspective-initialized")) return;
1804
1883
  el.setAttribute("data-perspective-initialized", "true");
1805
1884
  const researchId = el.getAttribute(DATA_ATTRS.popup);
1806
- if (researchId) {
1807
- const params = parseParamsAttr(el);
1808
- const brandConfig = extractBrandConfig(el);
1885
+ if (!researchId) return;
1886
+ const params = parseParamsAttr(el);
1887
+ const brandConfig = extractBrandConfig(el);
1888
+ const autoOpenAttr = el.getAttribute(DATA_ATTRS.autoOpen);
1889
+ if (autoOpenAttr) {
1890
+ try {
1891
+ const trigger = parseTriggerAttr(autoOpenAttr);
1892
+ const showOnce = parseShowOnceAttr(
1893
+ el.getAttribute(DATA_ATTRS.showOnce)
1894
+ );
1895
+ if (shouldShow(researchId, showOnce)) {
1896
+ triggerCleanups.get(researchId)?.();
1897
+ const cleanup = setupTrigger(trigger, () => {
1898
+ triggerCleanups.delete(researchId);
1899
+ markShown(researchId, showOnce);
1900
+ init({ researchId, type: "popup", params, ...brandConfig });
1901
+ });
1902
+ triggerCleanups.set(researchId, cleanup);
1903
+ }
1904
+ } catch (e) {
1905
+ console.warn("[Perspective]", e.message);
1906
+ }
1907
+ } else {
1809
1908
  styleButton(el, DEFAULT_THEME, brandConfig);
1810
1909
  el.addEventListener("click", (e) => {
1811
1910
  e.preventDefault();