فهرست منبع

remove singlefile attribute from frames after processing the page

Gildas 7 سال پیش
والد
کامیت
9841503c27
2فایلهای تغییر یافته به همراه48 افزوده شده و 0 حذف شده
  1. 3 0
      extension/core/content/content.js
  2. 45 0
      lib/frame-tree/frame-tree.js

+ 3 - 0
extension/core/content/content.js

@@ -130,6 +130,9 @@ this.singlefile.top = this.singlefile.top || (() => {
 		options.doc = document;
 		options.win = window;
 		await processor.run();
+		if (!options.saveRawPage && !options.removeFrames && this.frameTree) {
+			this.frameTree.cleanup(options);
+		}
 		if (options.confirmInfobarContent) {
 			options.infobarContent = singlefile.ui.prompt("Infobar content", options.infobarContent) || "";
 		}

+ 45 - 0
lib/frame-tree/frame-tree.js

@@ -25,6 +25,7 @@ this.frameTree = this.frameTree || (() => {
 	const MESSAGE_PREFIX = "__frameTree__::";
 	const FRAMES_CSS_SELECTOR = "iframe, frame, object[type=\"text/html\"][data]";
 	const INIT_REQUEST_MESSAGE = "initRequest";
+	const CLEANUP_REQUEST_MESSAGE = "cleanupRequest";
 	const INIT_RESPONSE_MESSAGE = "initResponse";
 	const TARGET_ORIGIN = "*";
 	const TIMEOUT_INIT_REQUEST_MESSAGE = 750;
@@ -48,6 +49,8 @@ this.frameTree = this.frameTree || (() => {
 				if (message.options.loadDeferredImages && window.lazyLoader) {
 					lazyLoader.process(message.options);
 				}
+			} else if (message.method == CLEANUP_REQUEST_MESSAGE) {
+				cleanupRequest(message);
 			} else if (message.method == INIT_RESPONSE_MESSAGE) {
 				const port = event.ports[0];
 				port.onmessage = event => initResponse(event.data);
@@ -72,6 +75,11 @@ this.frameTree = this.frameTree || (() => {
 			initRequest({ windowId, sessionId, options });
 			return sessions.get(sessionId).frames;
 		},
+		cleanup: options => {
+			const sessionId = options.sessionId || 0;
+			options = JSON.parse(JSON.stringify(options));
+			cleanupRequest({ windowId, sessionId, options });
+		},
 		initResponse,
 		TIMEOUT_INIT_REQUEST_MESSAGE
 	};
@@ -86,6 +94,15 @@ this.frameTree = this.frameTree || (() => {
 		processFrames(frameElements, message.options, windowId, sessionId);
 	}
 
+	function cleanupRequest(message) {
+		const sessionId = message.sessionId;
+		const frameElements = document.querySelectorAll(FRAMES_CSS_SELECTOR);
+		if (!TOP_WINDOW) {
+			windowId = message.windowId;
+		}
+		cleanupFrames(frameElements, message.options, windowId, sessionId);
+	}
+
 	function initResponse(message) {
 		const windowData = sessions.get(message.sessionId);
 		if (windowData) {
@@ -177,6 +194,34 @@ this.frameTree = this.frameTree || (() => {
 		sendInitResponse({ framesData, sessionId });
 	}
 
+	function cleanupFrames(frameElements, options, parentWindowId, sessionId) {
+		frameElements.forEach((frameElement, frameIndex) => {
+			const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
+			frameElement.removeAttribute(docHelper.WIN_ID_ATTRIBUTE_NAME);
+			try {
+				sendMessage(frameElement.contentWindow, { method: CLEANUP_REQUEST_MESSAGE, windowId, sessionId, options });
+			} catch (error) {
+				/* ignored */
+			}
+		});
+		frameElements.forEach((frameElement, frameIndex) => {
+			const windowId = parentWindowId + WINDOW_ID_SEPARATOR + frameIndex;
+			let frameDoc;
+			try {
+				frameDoc = frameElement.contentDocument;
+			} catch (error) {
+				// ignored
+			}
+			if (frameDoc) {
+				try {
+					cleanupFrames(frameDoc.querySelectorAll(FRAMES_CSS_SELECTOR), options, windowId, sessionId);
+				} catch (error) {
+					// ignored
+				}
+			}
+		});
+	}
+
 	function sendInitResponse(message) {
 		message.method = INIT_RESPONSE_MESSAGE;
 		try {