@bentolabs/sdk 1.1.0 → 1.2.1

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/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { generateSelector } from 'rrweb';
2
+ export { generateSelector };
1
3
  interface SDKConfig {
2
4
  apiKey: string;
3
5
  endpoint: string;
@@ -97,5 +99,5 @@ export declare class BentoLabsSDK {
97
99
  */
98
100
  trackCustomEvent(eventName: string, data?: Record<string, any>): void;
99
101
  }
100
- declare const _default: BentoLabsSDK;
101
- export default _default;
102
+ declare const sdk: BentoLabsSDK;
103
+ export default sdk;
package/dist/index.js CHANGED
@@ -50,7 +50,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
50
50
  var index_exports = {};
51
51
  __export(index_exports, {
52
52
  BentoLabsSDK: () => BentoLabsSDK,
53
- default: () => index_default
53
+ default: () => index_default,
54
+ generateSelector: () => generateSelector
54
55
  });
55
56
  module.exports = __toCommonJS(index_exports);
56
57
 
@@ -9093,6 +9094,115 @@ function inDom(n2) {
9093
9094
  if (!doc) return false;
9094
9095
  return index.contains(doc, n2) || shadowHostInDom(n2);
9095
9096
  }
9097
+ function isStableId(id) {
9098
+ if (!id) return false;
9099
+ if (id.length > 30) return false;
9100
+ if (/\d{3,}/.test(id)) return false;
9101
+ if (/^:r[a-z0-9]+:?$/i.test(id)) return false;
9102
+ if (/^radix-/i.test(id)) return false;
9103
+ if (/^headlessui-/i.test(id)) return false;
9104
+ if (/^mui-/i.test(id)) return false;
9105
+ if (/[a-f0-9]{8,}/i.test(id)) return false;
9106
+ if (/^(sc|css|emotion|styled)-/i.test(id)) return false;
9107
+ return true;
9108
+ }
9109
+ function normalizeHref(href) {
9110
+ if (!href) return null;
9111
+ if (href.startsWith("javascript:") || href.startsWith("data:")) return null;
9112
+ if (href.startsWith("#")) return href;
9113
+ try {
9114
+ const url = new URL(href, "http://x");
9115
+ return url.pathname;
9116
+ } catch (e) {
9117
+ return href.split("?")[0].split("#")[0];
9118
+ }
9119
+ }
9120
+ function escapeValue(value) {
9121
+ return value.replace(/\|/g, "%7C").replace(/=/g, "%3D");
9122
+ }
9123
+ function truncate(value, maxLength) {
9124
+ if (value.length <= maxLength) return value;
9125
+ return value.substring(0, maxLength - 3) + "...";
9126
+ }
9127
+ function getDirectText$1(element) {
9128
+ let text = "";
9129
+ for (const node2 of Array.from(element.childNodes)) {
9130
+ if (node2.nodeType === Node.TEXT_NODE) {
9131
+ text += node2.textContent || "";
9132
+ }
9133
+ }
9134
+ return text.replace(/\s+/g, " ").trim();
9135
+ }
9136
+ function buildBentoSelector(element) {
9137
+ const parts = [];
9138
+ const tag = element.tagName.toLowerCase();
9139
+ parts.push(tag);
9140
+ const testId = element.getAttribute("data-testid");
9141
+ if (testId) {
9142
+ parts.push(`testid=${escapeValue(truncate(testId, 50))}`);
9143
+ }
9144
+ const dataTest = element.getAttribute("data-test");
9145
+ if (dataTest) {
9146
+ parts.push(`test=${escapeValue(truncate(dataTest, 50))}`);
9147
+ }
9148
+ const dataCy = element.getAttribute("data-cy");
9149
+ if (dataCy) {
9150
+ parts.push(`cy=${escapeValue(truncate(dataCy, 50))}`);
9151
+ }
9152
+ const id = element.id;
9153
+ if (id && isStableId(id)) {
9154
+ parts.push(`id=${escapeValue(id)}`);
9155
+ }
9156
+ const name = element.getAttribute("name");
9157
+ if (name) {
9158
+ parts.push(`name=${escapeValue(truncate(name, 50))}`);
9159
+ }
9160
+ const type = element.getAttribute("type");
9161
+ if (type) {
9162
+ parts.push(`type=${escapeValue(type.toLowerCase())}`);
9163
+ }
9164
+ const role = element.getAttribute("role");
9165
+ if (role) {
9166
+ parts.push(`role=${escapeValue(role)}`);
9167
+ }
9168
+ const ariaLabel = element.getAttribute("aria-label");
9169
+ if (ariaLabel) {
9170
+ parts.push(`label=${escapeValue(truncate(ariaLabel, 50))}`);
9171
+ }
9172
+ const href = normalizeHref(element.getAttribute("href"));
9173
+ if (href) {
9174
+ parts.push(`href=${escapeValue(href)}`);
9175
+ }
9176
+ const placeholder = element.getAttribute("placeholder");
9177
+ if (placeholder) {
9178
+ parts.push(`placeholder=${escapeValue(truncate(placeholder, 50))}`);
9179
+ }
9180
+ const alt = element.getAttribute("alt");
9181
+ if (alt) {
9182
+ parts.push(`alt=${escapeValue(truncate(alt, 50))}`);
9183
+ }
9184
+ const title = element.getAttribute("title");
9185
+ if (title) {
9186
+ parts.push(`title=${escapeValue(truncate(title, 50))}`);
9187
+ }
9188
+ const interactiveTags = ["button", "a", "label", "span", "div", "li", "td", "th", "h1", "h2", "h3", "h4", "h5", "h6", "p"];
9189
+ if (interactiveTags.includes(tag)) {
9190
+ const text = getDirectText$1(element);
9191
+ if (text) {
9192
+ parts.push(`text=${escapeValue(truncate(text, 50))}`);
9193
+ }
9194
+ }
9195
+ return parts.join("|");
9196
+ }
9197
+ function generateSelector(outerHTML) {
9198
+ const parser2 = new DOMParser();
9199
+ const doc = parser2.parseFromString(outerHTML, "text/html");
9200
+ const element = doc.body.firstElementChild;
9201
+ if (!element) {
9202
+ throw new Error("Invalid outerHTML: could not parse element");
9203
+ }
9204
+ return buildBentoSelector(element);
9205
+ }
9096
9206
  var DEFAULT_OPTIONS = {
9097
9207
  maxTextLength: 500,
9098
9208
  includeOuterHTML: true,
@@ -9117,28 +9227,6 @@ function truncateText(text, maxLength) {
9117
9227
  }
9118
9228
  return text.substring(0, maxLength - 3) + "...";
9119
9229
  }
9120
- function getLimitedOuterHTML(element, maxDepth, doc) {
9121
- if (maxDepth <= 0) {
9122
- const clone2 = element.cloneNode(false);
9123
- return clone2.outerHTML;
9124
- }
9125
- const clone = element.cloneNode(false);
9126
- for (const child of Array.from(element.childNodes)) {
9127
- if (child.nodeType === Node.TEXT_NODE) {
9128
- const text = child.textContent || "";
9129
- const truncated = text.length > 100 ? text.substring(0, 97) + "..." : text;
9130
- clone.appendChild(doc.createTextNode(truncated));
9131
- } else if (child.nodeType === Node.ELEMENT_NODE) {
9132
- const childHtml = getLimitedOuterHTML(child, maxDepth - 1, doc);
9133
- const template = doc.createElement("template");
9134
- template.innerHTML = childHtml;
9135
- if (template.content.firstChild) {
9136
- clone.appendChild(template.content.firstChild.cloneNode(true));
9137
- }
9138
- }
9139
- }
9140
- return clone.outerHTML;
9141
- }
9142
9230
  function isInputElement(element) {
9143
9231
  const tagName = element.tagName.toUpperCase();
9144
9232
  return tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT";
@@ -9152,7 +9240,6 @@ function extractElementContext(element, options = {}) {
9152
9240
  } = options;
9153
9241
  const tagName = element.tagName.toLowerCase();
9154
9242
  const htmlElement = element;
9155
- const doc = element.ownerDocument;
9156
9243
  const context = {
9157
9244
  tagName
9158
9245
  };
@@ -9206,13 +9293,9 @@ function extractElementContext(element, options = {}) {
9206
9293
  if (alt) {
9207
9294
  context.alt = alt;
9208
9295
  }
9209
- if (isClickEvent && opts.includeOuterHTML) {
9296
+ if (isClickEvent) {
9210
9297
  try {
9211
- context.outerHTML = getLimitedOuterHTML(
9212
- element,
9213
- opts.outerHTMLDepth,
9214
- doc
9215
- );
9298
+ context.bentoSelector = buildBentoSelector(element);
9216
9299
  } catch (e) {
9217
9300
  }
9218
9301
  }
@@ -12885,7 +12968,21 @@ var BentoLabsSDK = class {
12885
12968
  }
12886
12969
  }
12887
12970
  };
12888
- var index_default = new BentoLabsSDK();
12971
+ var sdk = new BentoLabsSDK();
12972
+ if (typeof window !== "undefined") {
12973
+ window.BentoLabsSDK = __spreadProps(__spreadValues({}, sdk), {
12974
+ init: sdk.init.bind(sdk),
12975
+ stop: sdk.stop.bind(sdk),
12976
+ getSessionId: sdk.getSessionId.bind(sdk),
12977
+ isRecordingActive: sdk.isRecordingActive.bind(sdk),
12978
+ getConfig: sdk.getConfig.bind(sdk),
12979
+ getEventQueueLength: sdk.getEventQueueLength.bind(sdk),
12980
+ flushEvents: sdk.flushEvents.bind(sdk),
12981
+ trackCustomEvent: sdk.trackCustomEvent.bind(sdk),
12982
+ generateSelector
12983
+ });
12984
+ }
12985
+ var index_default = sdk;
12889
12986
  /*! Bundled license information:
12890
12987
 
12891
12988
  rrweb/dist/rrweb.js: