Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save images offline, in the snapshot #770

Merged
merged 6 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ The parameter of `rrweb.record` accepts the following options.
| packFn | - | refer to the [storage optimization recipe](./docs/recipes/optimize-storage.md) |
| sampling | - | refer to the [storage optimization recipe](./docs/recipes/optimize-storage.md) |
| recordCanvas | false | whether to record the canvas element |
| recordImages | false | whether to record the image content |
| inlineImages | false | whether to record the image content |
| collectFonts | false | whether to collect fonts in the website |
| recordLog | false | whether to record console output, refer to the [console recipe](./docs/recipes/console.md) |
| userTriggeredOnInput | false | whether to add `userTriggered` on input events that indicates if this event was triggered directly by the user or not. [What is `userTriggered`?](https://github.com/rrweb-io/rrweb/pull/495) |
Expand Down
36 changes: 20 additions & 16 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function serializeNode(
maskInputOptions: MaskInputOptions;
maskTextFn: MaskTextFn | undefined;
maskInputFn: MaskInputFn | undefined;
recordImages: boolean;
inlineImages: boolean;
recordCanvas: boolean;
keepIframeSrcFn: KeepIframeSrcFn;
},
Expand All @@ -396,7 +396,7 @@ function serializeNode(
maskInputOptions = {},
maskTextFn,
maskInputFn,
recordImages,
inlineImages,
recordCanvas,
keepIframeSrcFn,
} = options;
Expand Down Expand Up @@ -513,13 +513,17 @@ function serializeNode(
attributes.rr_dataURL = (n as HTMLCanvasElement).toDataURL();
}
// save image offline
if (tagName === 'img' && recordImages && canvasService && canvasCtx) {
if (tagName === 'img' && inlineImages && canvasService && canvasCtx) {
const image = (n as HTMLImageElement);
image.crossOrigin = 'anonymous';
canvasService.width = image.naturalWidth;
canvasService.height = image.naturalHeight;
canvasCtx.drawImage(image, 0, 0);
attributes.rr_dataURL = canvasService.toDataURL();
try {
canvasService.width = image.naturalWidth;
canvasService.height = image.naturalHeight;
canvasCtx.drawImage(image, 0, 0);
attributes.rr_dataURL = canvasService.toDataURL();
} catch {
// ignore error
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we log something here, e.g. with console.warn?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this happened on the record side, at least we need an option to do a log.

And this is out of the scope of this PR, feel free to open a new one! Thanks.

}
}
// media elements
if (tagName === 'audio' || tagName === 'video') {
Expand Down Expand Up @@ -734,7 +738,7 @@ export function serializeNodeWithId(
maskInputFn: MaskInputFn | undefined;
slimDOMOptions: SlimDOMOptions;
keepIframeSrcFn?: KeepIframeSrcFn;
recordImages?: boolean;
inlineImages?: boolean;
recordCanvas?: boolean;
preserveWhiteSpace?: boolean;
onSerialize?: (n: INode) => unknown;
Expand All @@ -755,7 +759,7 @@ export function serializeNodeWithId(
maskTextFn,
maskInputFn,
slimDOMOptions,
recordImages = false,
inlineImages = false,
recordCanvas = false,
onSerialize,
onIframeLoad,
Expand All @@ -773,7 +777,7 @@ export function serializeNodeWithId(
maskInputOptions,
maskTextFn,
maskInputFn,
recordImages,
inlineImages,
recordCanvas,
keepIframeSrcFn,
});
Expand Down Expand Up @@ -826,7 +830,7 @@ export function serializeNodeWithId(
) {
preserveWhiteSpace = false;
}
if (recordImages) {
if (inlineImages) {
initCanvasService(doc);
}
const bypassOptions = {
Expand All @@ -842,7 +846,7 @@ export function serializeNodeWithId(
maskTextFn,
maskInputFn,
slimDOMOptions,
recordImages,
inlineImages,
recordCanvas,
preserveWhiteSpace,
onSerialize,
Expand Down Expand Up @@ -895,7 +899,7 @@ export function serializeNodeWithId(
maskTextFn,
maskInputFn,
slimDOMOptions,
recordImages,
inlineImages,
recordCanvas,
preserveWhiteSpace,
onSerialize,
Expand Down Expand Up @@ -928,7 +932,7 @@ function snapshot(
maskTextFn?: MaskTextFn;
maskInputFn?: MaskTextFn;
slimDOM?: boolean | SlimDOMOptions;
recordImages?: boolean;
inlineImages?: boolean;
recordCanvas?: boolean;
preserveWhiteSpace?: boolean;
onSerialize?: (n: INode) => unknown;
Expand All @@ -943,7 +947,7 @@ function snapshot(
maskTextClass = 'rr-mask',
maskTextSelector = null,
inlineStylesheet = true,
recordImages = false,
inlineImages = false,
recordCanvas = false,
maskAllInputs = false,
maskTextFn,
Expand Down Expand Up @@ -1013,7 +1017,7 @@ function snapshot(
maskTextFn,
maskInputFn,
slimDOMOptions,
recordImages,
inlineImages,
recordCanvas,
preserveWhiteSpace,
onSerialize,
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb-snapshot/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ iframe.contentDocument.querySelector('center').clientHeight
await page.waitForSelector('img', { timeout: 1000 });

const snapshot = await page.evaluate(`${code}
const [snap] = rrweb.snapshot(document, {recordImages: true, inlineStylesheet: false});
const [snap] = rrweb.snapshot(document, {inlineImages: true, inlineStylesheet: false});
JSON.stringify(snap, null, 2);
`);

Expand Down
4 changes: 2 additions & 2 deletions packages/rrweb-snapshot/typings/snapshot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export declare function serializeNodeWithId(n: Node | INode, options: {
maskInputFn: MaskInputFn | undefined;
slimDOMOptions: SlimDOMOptions;
keepIframeSrcFn?: KeepIframeSrcFn;
recordImages?: boolean;
inlineImages?: boolean;
recordCanvas?: boolean;
preserveWhiteSpace?: boolean;
onSerialize?: (n: INode) => unknown;
Expand All @@ -36,7 +36,7 @@ declare function snapshot(n: Document, options?: {
maskTextFn?: MaskTextFn;
maskInputFn?: MaskTextFn;
slimDOM?: boolean | SlimDOMOptions;
recordImages?: boolean;
inlineImages?: boolean;
recordCanvas?: boolean;
preserveWhiteSpace?: boolean;
onSerialize?: (n: INode) => unknown;
Expand Down