Skip to content

Commit

Permalink
fix: modify embedded images to allow canvas render
Browse files Browse the repository at this point in the history
  • Loading branch information
iamogbz committed Dec 23, 2024
1 parent 16a70c3 commit afd4596
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions scripts/export-element/index.user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ import { html2canvas } from "libraries/html2canvas";

const selectedText = selection.toString();
const matchText = sanitizeText(selectedText);
params.matchWords = matchText.split(" ");
params.matchWords = matchText.split(" ").filter(Boolean);

params.target = findLastNodeWithPredicate(
selection.anchorNode ?? selection.focusNode,
(node) => {
if (params.matchWords.length === 0) return true;
const nodeText = sanitizeText(node.textContent || "");
return containsAll(nodeText, params.matchWords);
},
Expand Down Expand Up @@ -212,6 +213,24 @@ import { html2canvas } from "libraries/html2canvas";
*/
async function cloneAndDownloadImage(node: Node) {
const clone = cloneNodeWithStyles(window, node);
const modifyElements = (
parent: Node,
selector: string,
modifier: (el: Node) => void,
) => {
if (!(parent instanceof Element)) return;
parent.querySelectorAll(selector).forEach((el) => {
modifier(el);
if (el.shadowRoot) {
modifyElements(el.shadowRoot, selector, modifier);
}
});
};
modifyElements(clone, "*", (img) => {
if (!(img instanceof HTMLImageElement)) return;
img.setAttribute("crossOrigin", "anonymous"); // enable CORS
img.setAttribute("src", img.src + "?t=" + Date.now()); // prevent caching
});
const backgroundColor = findBackgroundColor(node);
const nodeSize = (
findLastNodeWithPredicate(
Expand Down Expand Up @@ -273,7 +292,11 @@ import { html2canvas } from "libraries/html2canvas";
};

// https://stackoverflow.com/questions/3906142/how-to-save-a-png-from-javascript-variable
const canvas = await html2canvas(modalContent);
// https://stackoverflow.com/questions/75128079/html2canvas-not-including-fetched-image-in-downloaded-png
const canvas = await html2canvas(modalContent, {
allowTaint: true,
useCORS: true,
});
positionPreview(); // wait for clone to be rendered before positioning it
const imageType = "image/png";
const dataBlob = await new Promise<Blob>((resolve) => {
Expand Down

0 comments on commit afd4596

Please sign in to comment.