From 6540a89fdbd61fa7a5d59fd2fd9144c8fe33ff9b Mon Sep 17 00:00:00 2001 From: Michael Dellanoce Date: Fri, 2 Feb 2024 15:51:28 -0500 Subject: [PATCH] Replace Array.from with clean implementation This work is to try to provide support where rrweb might be included in applications with various tools that might override Array.from so that the 2nd parameter (the map function) will always work for rrweb. --- .changeset/eleven-bobcats-peel.md | 6 ++++++ packages/rrweb/src/record/index.ts | 14 ++++++++++++++ packages/rrweb/src/types.ts | 1 + 3 files changed, 21 insertions(+) create mode 100644 .changeset/eleven-bobcats-peel.md diff --git a/.changeset/eleven-bobcats-peel.md b/.changeset/eleven-bobcats-peel.md new file mode 100644 index 0000000000..edd30c9213 --- /dev/null +++ b/.changeset/eleven-bobcats-peel.md @@ -0,0 +1,6 @@ +--- +'rrweb-snapshot': patch +'rrweb': patch +--- + +try to provide clean implementation for Array.from to support older libraries that might drop the 2nd parameter diff --git a/packages/rrweb/src/record/index.ts b/packages/rrweb/src/record/index.ts index 7be978199d..71dc621ab5 100644 --- a/packages/rrweb/src/record/index.ts +++ b/packages/rrweb/src/record/index.ts @@ -46,6 +46,20 @@ let takeFullSnapshot!: (isCheckout?: boolean) => void; let canvasManager!: CanvasManager; let recording = false; +// Multiple tools (i.e. MooTools, Prototype.js) override Array.from and drop support for the 2nd parameter +// Try to pull a clean implementation from a newly created iframe +try { + if (window.Array.from([1], (x) => x * 2)[0] !== 2) { + const cleanFrame = document.createElement('iframe'); + document.body.appendChild(cleanFrame); + window.Array.from = + cleanFrame.contentWindow?.Array.from || window.Array.from; + document.body.removeChild(cleanFrame); + } +} catch (err) { + console.debug('Unable to override Array.from', err); +} + const mirror = createMirror(); function record( options: recordOptions = {}, diff --git a/packages/rrweb/src/types.ts b/packages/rrweb/src/types.ts index 10ffb62b43..03c9a59fdc 100644 --- a/packages/rrweb/src/types.ts +++ b/packages/rrweb/src/types.ts @@ -207,6 +207,7 @@ export type missingNodeMap = { declare global { interface Window { FontFace: typeof FontFace; + Array: typeof Array; } }