Procházet zdrojové kódy

fixed image issue on web.whatsapp.com

Former-commit-id: e166b372d204415571cc11dd6f32707282c10a24
Gildas před 6 roky
rodič
revize
c78526f7b3

+ 20 - 2
lib/single-file/single-file-core.js

@@ -515,6 +515,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 								imgElement.setAttribute("src", EMPTY_IMAGE);
 							} else {
 								if (imageData.currentSrc) {
+									imgElement.dataset.singleFileOriginURL = imgElement.getAttribute("src");
 									imgElement.setAttribute("src", imageData.currentSrc);
 								}
 								if (this.options.loadDeferredImages) {
@@ -1371,7 +1372,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 	// ---------------
 	const DATA_URI_PREFIX = "data:";
 	const ABOUT_BLANK_URI = "about:blank";
-	const EMPTY_DATA_URI = "data:base64,";
+	const EMPTY_DATA_URI = "data:null;base64,";
 	const REGEXP_URL_HASH = /(#.+?)$/;
 	const SINGLE_FILE_VARIABLE_NAME_PREFIX = "--sf-img-";
 	const VAR_MAX_SIZE = 1024 * 1024 * 2;
@@ -1679,7 +1680,24 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 								// ignored
 							}
 							if (Util.testValidURL(resourceURL)) {
-								const { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL, true, resourceElement.tagName == "IMG" && attributeName == "src");
+								let { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL, true, resourceElement.tagName == "IMG" && attributeName == "src");
+								if (resourceElement.dataset.singleFileOriginURL) {
+									const originURL = resourceElement.dataset.singleFileOriginURL;
+									delete resourceElement.dataset.singleFileOriginURL;
+									if (content == EMPTY_DATA_URI) {
+										try {
+											resourceURL = originURL;
+											content = (await util.getContent(resourceURL, {
+												asBinary: true,
+												maxResourceSize: options.maxResourceSize,
+												maxResourceSizeEnabled: options.maxResourceSizeEnabled,
+												frameId: options.windowId
+											})).data;
+										} catch (error) {
+											// ignored
+										}
+									}
+								}
 								if (removeElementIfMissing && content == EMPTY_DATA_URI) {
 									resourceElement.remove();
 								} else {

+ 3 - 3
lib/single-file/single-file-util.js

@@ -218,7 +218,7 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 						response = await fetchResource(resourceURL);
 					}
 				} catch (error) {
-					return { data: options.asBinary ? "data:base64," : "", resourceURL };
+					return { data: options.asBinary ? "data:null;base64," : "", resourceURL };
 				}
 				const buffer = await response.arrayBuffer();
 				resourceURL = response.url || resourceURL;
@@ -247,7 +247,7 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 							log("  // ENDED   download url =", resourceURL, "delay =", Date.now() - startTime);
 						}
 						if (options.maxResourceSizeEnabled && buffer.byteLength > options.maxResourceSize * ONE_MB) {
-							return { data: "data:base64,", resourceURL };
+							return { data: "data:null;base64,", resourceURL };
 						} else {
 							const reader = new FileReader();
 							reader.readAsDataURL(new Blob([buffer], { type: contentType }));
@@ -258,7 +258,7 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 							return { data: dataUri, resourceURL };
 						}
 					} catch (error) {
-						return { data: "data:base64,", resourceURL };
+						return { data: "data:null;base64,", resourceURL };
 					}
 				} else {
 					if (response.status >= 400 || (options.validateTextContentType && contentType && !contentType.startsWith(PREFIX_CONTENT_TYPE_TEXT))) {