Просмотр исходного кода

keep embedded image when editing a saved page

Gildas 2 лет назад
Родитель
Сommit
020c2d6231
2 измененных файлов с 19 добавлено и 1 удалено
  1. 1 0
      src/core/bg/editor.js
  2. 18 1
      src/core/content/content-bootstrap.js

+ 1 - 0
src/core/bg/editor.js

@@ -85,6 +85,7 @@ async function onMessage(message, sender) {
 					}
 				} else {
 					message.content = content;
+					options.embeddedImage = tabData.embeddedImage;
 					message.options = options;
 				}
 				await browser.tabs.sendMessage(tab.id, message);

+ 18 - 1
src/core/content/content-bootstrap.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global browser, globalThis, document, location, setTimeout, XMLHttpRequest, Node, DOMParser */
+/* global browser, globalThis, document, location, setTimeout, XMLHttpRequest, Node, DOMParser, Blob, URL, Image, OffscreenCanvas */
 
 const MAX_CONTENT_SIZE = 32 * (1024 * 1024);
 
@@ -340,12 +340,29 @@ async function openEditor(document) {
 				message.content = content.substring(blockIndex * MAX_CONTENT_SIZE, (blockIndex + 1) * MAX_CONTENT_SIZE);
 			}
 		} else {
+			message.embeddedImage = await extractEmbeddedImage(content);
 			message.content = content instanceof Uint8Array ? Array.from(content) : content;
 		}
 		await browser.runtime.sendMessage(message);
 	}
 }
 
+async function extractEmbeddedImage(content) {
+	if (content[0] == 0x89 && content[1] == 0x50 && content[2] == 0x4E && content[3] == 0x47) {
+		let blob = new Blob([content], { type: "image/png" });
+		const blobURI = URL.createObjectURL(blob);
+		const image = new Image();
+		image.src = blobURI;
+		await new Promise(resolve => image.onload = resolve);
+		const canvas = new OffscreenCanvas(image.width, image.height);
+		const context = canvas.getContext("2d");
+		context.drawImage(image, 0, 0);
+		blob = await canvas.convertToBlob({ type: "image/png" });
+		const arrayBuffer = await blob.arrayBuffer();
+		return Array.from(new Uint8Array(arrayBuffer));
+	}
+}
+
 function detectSavedPage(document) {
 	if (savedPageDetected === undefined) {
 		const helper = singlefile.helper;