Skip to content

Commit c404b8f

Browse files
author
zhuxl
committed
docs(configuration): add examples and explanations for using the customIsSameOrigin option
1 parent dd9ef5e commit c404b8f

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

docs/configuration.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ These are all of the available configuration options.
77
| allowTaint | `false` | Whether to allow cross-origin images to taint the canvas
88
| backgroundColor | `#ffffff` | Canvas background color, if none is specified in DOM. Set `null` for transparent
99
| canvas | `null` | Existing `canvas` element to use as a base for drawing on
10+
| customIsSameOrigin | `null` | Custom function to determine if an image URL is same-origin. Accepts two parameters: `(src: string, oldFn: (src: string) => boolean) => boolean | Promise<boolean>` where `src` is the image URL and `oldFn` is the default same-origin check function.
1011
| foreignObjectRendering | `false` | Whether to use ForeignObject rendering if the browser supports it
1112
| imageTimeout | `15000` | Timeout for loading an image (in milliseconds). Set to `0` to disable timeout.
1213
| ignoreElements | `(element) => false` | Predicate function which removes the matching elements from the render.
@@ -26,3 +27,61 @@ These are all of the available configuration options.
2627
| windowHeight | `Window.innerHeight` | Window height to use when rendering `Element`, which may affect things like Media queries
2728

2829
If you wish to exclude certain `Element`s from getting rendered, you can add a `data-html2canvas-ignore` attribute to those elements and html2canvas-pro will exclude them from the rendering.
30+
31+
### Custom isSameOrigin usage
32+
33+
The `customIsSameOrigin` option allows you to override the default same-origin detection logic, which is particularly useful in the following scenarios:
34+
35+
1. **Handling redirects**: When an image URL from your domain redirects to an external domain
36+
2. **CDN configurations**: When your content is served from multiple domains or CDNs
37+
3. **Force CORS mode**: When you want to force all images to use CORS regardless of origin
38+
39+
#### Basic usage
40+
41+
```typescript
42+
html2canvas(element, {
43+
useCORS: true,
44+
customIsSameOrigin: (src, oldFn) => {
45+
// If old logic think it's not same origin, certainly it's not
46+
if (!oldFn(src)) {
47+
return false;
48+
}
49+
// Otherwise, we need to check if it's a redirect url
50+
const targetUrl = new URL(src);
51+
const pathname = targetUrl.pathname;
52+
// You can replace it with any logic you want. Including but not limited to: using regular expressions, using asynchronous validation logic
53+
// Here we simply suppose your biz url starts with /some-redirect-prefix and treat it as a redirect url just for example
54+
return !pathname.startsWith('/some-redirect-prefix');
55+
},
56+
// any other options..
57+
});
58+
```
59+
60+
#### Async validation
61+
62+
The function can also return a Promise for asynchronous validation:
63+
64+
```typescript
65+
html2canvas(element, {
66+
useCORS: true,
67+
customIsSameOrigin: async (src, oldFn) => {
68+
// You could check against an API that knows which URLs will redirect
69+
const response = await fetch('/api/check-redirect?url=' + encodeURIComponent(src));
70+
const data = await response.json();
71+
return !data.willRedirect;
72+
},
73+
// any other options..
74+
});
75+
```
76+
77+
#### Force all images to use CORS
78+
79+
You can use it to force all images to use CORS mode:
80+
81+
```typescript
82+
html2canvas(element, {
83+
useCORS: true,
84+
customIsSameOrigin: (src, oldFn) => false, // Always report as not same origin
85+
// any other options..
86+
});
87+
```

0 commit comments

Comments
 (0)