Skip to content

Commit

Permalink
Merge branch 'tmmschmit-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
IDisposable committed Feb 14, 2024
2 parents 866a5a6 + 47a8573 commit 3f077a7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,19 @@ domtoimage.toCanvas(document.getElementById('my-node')).then(function (canvas) {
});
```

Adjust cloned nodes before/after children are cloned [sample fiddle](https://jsfiddle.net/IDisposable/grLtjwe5/12/)
Adjust cloned nodes before/after children are cloned
[sample fiddle](https://jsfiddle.net/IDisposable/grLtjwe5/12/)

```javascript
const adjustClone = (node, clone, after) => {
if (!after && clone.id === 'element') {
clone.style.transform = 'translateY(100px)';
}
return clone;
}
if (!after && clone.id === 'element') {
clone.style.transform = 'translateY(100px)';
}
return clone;
};

const wrapper = document.getElementById('wrapper');
const blob = domtoimage.toBlob(wrapper, { adjustClonedNode: adjustClone});
const blob = domtoimage.toBlob(wrapper, { adjustClonedNode: adjustClone });
```

---
Expand Down Expand Up @@ -203,6 +204,11 @@ Set to true to enable the copying of the default styles of elements. This will m
process faster. Try disabling it if seeing extra padding and using resetting / normalizing
in CSS. Defaults to true.

#### useCredentialFeatures

Allows optionally setting the `useCredentials` option if the resource matches a pattern in
the `useCredentialFilters` array.

### Alternative Solutions to CORS Policy Issue

Are you facing a [CORS policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
Expand Down Expand Up @@ -344,7 +350,8 @@ Klimas (fixes), Edgardo Di Gesto (fixes), 樊冬 Fan Dong (fixes), Shrijan Tripa
SNDST00M (optimize), Joseph White (performance CSS), Phani Rithvij (test), David
DOLCIMASCOLO (packaging), Zee (ZM) @zm-cttae (many major updates), Joshua Walsh
@JoshuaWalsh (Firefox issues), Emre Coban @emrecoban (documentation), Nate Stuyvesant
@nstuyvesant (fixes), King Wang @eachmawzw (CORS image proxy)
@nstuyvesant (fixes), King Wang @eachmawzw (CORS image proxy), TMM Schmit @tmmschmit
(useCredentialsFilters)

## License

Expand Down
4 changes: 2 additions & 2 deletions dist/dom-to-image-more.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dom-to-image-more.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"Joshua Walsh @JoshuaWalsh",
"Emre Coban @emrecoban",
"Nate Stuyvesant @nstuyvesant",
"King Wang @eachmawzw"
"King Wang @eachmawzw",
"TMM Schmit @tmmschmit"
],
"license": "MIT",
"bugs": {
Expand Down
4 changes: 2 additions & 2 deletions spec/dom-to-image-more.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
it('should handle adjustClonedNode', function (done) {
function oncloned(_node, clone, after) {
/* jshint unused:false */
if(!after) {
if (!after) {
if (clone.id === 'element') {
clone.style.transform = 'translateY(100px)';
}
}
return clone;
return clone;
}

loadTestPage(
Expand Down
21 changes: 19 additions & 2 deletions src/dom-to-image-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
cacheBust: false,
// Use (existing) authentication credentials for external URIs (CORS requests)
useCredentials: false,
// Use (existing) authentication credentials for external URIs (CORS requests) on some filtered requests only
useCredentialsFilters: [],
// Default resolve timeout
httpTimeout: 30000,
// Style computation cache tag rules (options are strict, relaxed)
Expand Down Expand Up @@ -281,6 +283,13 @@
domtoimage.impl.options.useCredentials = options.useCredentials;
}

if (typeof options.useCredentialsFilters === 'undefined') {
domtoimage.impl.options.useCredentialsFilters =
defaultOptions.useCredentialsFilters;
} else {
domtoimage.impl.options.useCredentialsFilters = options.useCredentialsFilters;
}

if (typeof options.httpTimeout === 'undefined') {
domtoimage.impl.options.httpTimeout = defaultOptions.httpTimeout;
} else {
Expand Down Expand Up @@ -819,6 +828,14 @@
request.ontimeout = timeout;
request.responseType = 'blob';
request.timeout = httpTimeout;

if (domtoimage.impl.options.useCredentialsFilters.length > 0) {
domtoimage.impl.options.useCredentials =
domtoimage.impl.options.useCredentialsFilters.filter(
(credentialsFilter) => url.search(credentialsFilter) >= 0
).length > 0;
}

if (domtoimage.impl.options.useCredentials) {
request.withCredentials = true;
}
Expand Down Expand Up @@ -1404,8 +1421,8 @@
const docType = document.doctype;
const docTypeDeclaration = docType
? `<!DOCTYPE ${escapeHTML(docType.name)} ${escapeHTML(
docType.publicId
)} ${escapeHTML(docType.systemId)}`.trim() + '>'
docType.publicId
)} ${escapeHTML(docType.systemId)}`.trim() + '>'
: '';

// Create a hidden sandbox <iframe> element within we can create default HTML elements and query their
Expand Down

0 comments on commit 3f077a7

Please sign in to comment.