image-exporter 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # image-exporter [v1.0.0]
1
+ # image-exporter [v1.0.1]
2
2
 
3
3
  image-exporter is a client-side javascript tool that downloads DOM elements as images. It can be imported using your favorite package manager or used directly the window.
4
4
 
@@ -814,6 +814,14 @@ async function captureElement(element, imageOptions, filenames) {
814
814
  // Ignores elements with data-ignore-capture attribute
815
815
  filter
816
816
  };
817
+ const styles = getComputedStyle(element);
818
+ const backgroundColor = styles.backgroundColor;
819
+ const backgroundImage = styles.backgroundImage;
820
+ let cleanUpBackground = false;
821
+ if (backgroundColor === "rgba(0, 0, 0, 0)" && backgroundImage === "none" && imageOptions.format === "jpg") {
822
+ element.style.backgroundColor = "#FFFFFF";
823
+ cleanUpBackground = true;
824
+ }
817
825
  switch (imageOptions.format) {
818
826
  case "jpg":
819
827
  dataURL = await toJpeg(element, htmlToImageOptions);
@@ -825,6 +833,10 @@ async function captureElement(element, imageOptions, filenames) {
825
833
  dataURL = await toSvg(element, htmlToImageOptions);
826
834
  break;
827
835
  }
836
+ if (cleanUpBackground) {
837
+ element.style.backgroundColor = "";
838
+ element.style.backgroundImage = "";
839
+ }
828
840
  return {
829
841
  dataURL,
830
842
  fileName: handleFileNames(imageOptions, filenames)
@@ -3384,13 +3396,6 @@ async function proxyCSS(config) {
3384
3396
  try {
3385
3397
  const response = await fetch(proxiedURL);
3386
3398
  let cssContent = await response.text();
3387
- cssContent = cssContent.replace(
3388
- /url\(['"]?(https?:\/\/[^'")\s]+)['"]?\)/g,
3389
- (match, url) => {
3390
- if (url.startsWith(config.corsProxyBaseUrl)) return match;
3391
- return `url("${config.corsProxyBaseUrl}${encodeURIComponent(url)}")`;
3392
- }
3393
- );
3394
3399
  const styleElement = document.createElement("style");
3395
3400
  styleElement.textContent = cssContent;
3396
3401
  styleElement.setAttribute(
@@ -3632,7 +3637,7 @@ async function determineTotalElements(elements) {
3632
3637
  }
3633
3638
  let windowLogging = true;
3634
3639
  let loggingLevel = "none";
3635
- async function capture(elements, userConfig) {
3640
+ async function capture(elements, userConfig = defaultConfig) {
3636
3641
  try {
3637
3642
  const config = { ...defaultConfig, ...userConfig };
3638
3643
  windowLogging = config.enableWindowLogging;
@@ -819,6 +819,14 @@
819
819
  // Ignores elements with data-ignore-capture attribute
820
820
  filter
821
821
  };
822
+ const styles = getComputedStyle(element);
823
+ const backgroundColor = styles.backgroundColor;
824
+ const backgroundImage = styles.backgroundImage;
825
+ let cleanUpBackground = false;
826
+ if (backgroundColor === "rgba(0, 0, 0, 0)" && backgroundImage === "none" && imageOptions.format === "jpg") {
827
+ element.style.backgroundColor = "#FFFFFF";
828
+ cleanUpBackground = true;
829
+ }
822
830
  switch (imageOptions.format) {
823
831
  case "jpg":
824
832
  dataURL = await toJpeg(element, htmlToImageOptions);
@@ -830,6 +838,10 @@
830
838
  dataURL = await toSvg(element, htmlToImageOptions);
831
839
  break;
832
840
  }
841
+ if (cleanUpBackground) {
842
+ element.style.backgroundColor = "";
843
+ element.style.backgroundImage = "";
844
+ }
833
845
  return {
834
846
  dataURL,
835
847
  fileName: handleFileNames(imageOptions, filenames)
@@ -3389,13 +3401,6 @@
3389
3401
  try {
3390
3402
  const response = await fetch(proxiedURL);
3391
3403
  let cssContent = await response.text();
3392
- cssContent = cssContent.replace(
3393
- /url\(['"]?(https?:\/\/[^'")\s]+)['"]?\)/g,
3394
- (match, url) => {
3395
- if (url.startsWith(config.corsProxyBaseUrl)) return match;
3396
- return `url("${config.corsProxyBaseUrl}${encodeURIComponent(url)}")`;
3397
- }
3398
- );
3399
3404
  const styleElement = document.createElement("style");
3400
3405
  styleElement.textContent = cssContent;
3401
3406
  styleElement.setAttribute(
@@ -3637,7 +3642,7 @@
3637
3642
  }
3638
3643
  let windowLogging = true;
3639
3644
  let loggingLevel = "none";
3640
- async function capture(elements, userConfig) {
3645
+ async function capture(elements, userConfig = defaultConfig) {
3641
3646
  try {
3642
3647
  const config = { ...defaultConfig, ...userConfig };
3643
3648
  windowLogging = config.enableWindowLogging;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "image-exporter",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Easily download one or more DOM elements as images",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -2,6 +2,7 @@ import * as htmlToImage from "html-to-image";
2
2
  import { Image, ParsedImageOptions } from "../types";
3
3
  import { handleFileNames } from "./handle-filenames";
4
4
  import { Options } from "html-to-image/lib/types";
5
+ import { get } from "http";
5
6
 
6
7
  /**
7
8
  * captureElement
@@ -25,7 +26,21 @@ export async function captureElement(
25
26
  filter: filter,
26
27
  };
27
28
 
28
- // Captures image b ased on format
29
+ // If element has no background and is a JPG, default to white background
30
+ const styles = getComputedStyle(element);
31
+ const backgroundColor = styles.backgroundColor;
32
+ const backgroundImage = styles.backgroundImage;
33
+ let cleanUpBackground = false;
34
+ if (
35
+ backgroundColor === "rgba(0, 0, 0, 0)" &&
36
+ backgroundImage === "none" &&
37
+ imageOptions.format === "jpg"
38
+ ) {
39
+ element.style.backgroundColor = "#FFFFFF";
40
+ cleanUpBackground = true;
41
+ }
42
+
43
+ // Captures image based on format
29
44
  switch (imageOptions.format) {
30
45
  case "jpg":
31
46
  dataURL = await htmlToImage.toJpeg(element, htmlToImageOptions);
@@ -38,6 +53,11 @@ export async function captureElement(
38
53
  break;
39
54
  }
40
55
 
56
+ if (cleanUpBackground) {
57
+ element.style.backgroundColor = "";
58
+ element.style.backgroundImage = "";
59
+ }
60
+
41
61
  return {
42
62
  dataURL,
43
63
  fileName: handleFileNames(imageOptions, filenames),
@@ -18,7 +18,7 @@ export let loggingLevel = "none";
18
18
  */
19
19
  export async function capture(
20
20
  elements: HTMLElement[] | NodeListOf<HTMLElement> | HTMLElement,
21
- userConfig: Partial<Config>
21
+ userConfig: Partial<Config> = defaultConfig
22
22
  ): Promise<Image[] | null> {
23
23
  try {
24
24
  /* --------------------------------- Config --------------------------------- */
@@ -29,17 +29,6 @@ export async function proxyCSS(config: Config) {
29
29
  const response = await fetch(proxiedURL);
30
30
  let cssContent = await response.text();
31
31
 
32
- // Proxy absolute URLs (http/https) within the CSS content
33
- cssContent = cssContent.replace(
34
- /url\(['"]?(https?:\/\/[^'")\s]+)['"]?\)/g,
35
- (match, url) => {
36
- // Skip if already proxied
37
- if (url.startsWith(config.corsProxyBaseUrl)) return match;
38
- // Otherwise return proxied URL
39
- return `url("${config.corsProxyBaseUrl}${encodeURIComponent(url)}")`;
40
- }
41
- );
42
-
43
32
  // Insert the parsed CSS content into a <style> element
44
33
  const styleElement = document.createElement("style");
45
34
  styleElement.textContent = cssContent;