Преглед изворни кода

use a frame attribute to store the window ID

Gildas пре 7 година
родитељ
комит
9a2fddc016
2 измењених фајлова са 29 додато и 24 уклоњено
  1. 1 0
      lib/frame-tree/content/frame-tree.js
  2. 28 24
      lib/single-file/single-file-core.js

+ 1 - 0
lib/frame-tree/content/frame-tree.js

@@ -136,6 +136,7 @@ this.FrameTree = this.FrameTree || (() => {
 		top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "initResponse", framesData, windowId, index }), "*");
 		frameElements.forEach((frameElement, index) => {
 			const frameWinId = windowId + "." + index;
+			frameElement.setAttribute("data-frame-tree-win-id", frameWinId);
 			let frameDoc, frameWindow, topWindow;
 			try {
 				frameDoc = frameElement.contentDocument;

+ 28 - 24
lib/single-file/single-file-core.js

@@ -205,6 +205,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 	// ------------
 	const ESCAPED_FRAGMENT = "_escaped_fragment_=";
 	const EMPTY_DATA_URI = "data:base64,";
+	const WIN_ID_ATTRIBUTE_NAME = "data-frame-tree-win-id";
 
 	const batchRequest = new BatchRequest();
 
@@ -424,30 +425,33 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 
 		async frames(initialization) {
 			const frameElements = Array.from(this.doc.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]"));
-			await Promise.all(frameElements.map(async (frameElement, frameIndex) => {
-				const frameWindowId = (this.options.windowId || "0") + "." + frameIndex;
-				const frameData = this.options.framesData.find(frame => frame.windowId == frameWindowId);
-				DomProcessorHelper.setFrameEmptySrc(frameElement);
-				frameElement.setAttribute("sandbox", "");
-				if (frameData) {
-					if (initialization) {
-						const options = Object.create(this.options);
-						options.insertSingleFileComment = false;
-						options.insertFaviconLink = false;
-						options.url = frameData.baseURI;
-						options.windowId = frameWindowId;
-						if (frameData.content) {
-							options.content = frameData.content;
-							frameData.processor = new PageProcessor(options);
-							frameData.frameElement = frameElement;
-							await frameData.processor.loadPage();
-							return frameData.processor.initialize();
-						}
-					} else {
-						if (frameData.processor) {
-							await frameData.processor.preparePageData();
-							const pageData = await frameData.processor.getPageData();
-							DomProcessorHelper.setFrameContent(frameElement, pageData.content);
+			await Promise.all(frameElements.map(async (frameElement) => {
+				const frameWindowId = frameElement.getAttribute(WIN_ID_ATTRIBUTE_NAME);
+				if (frameWindowId) {
+					const frameData = this.options.framesData.find(frame => frame.windowId == frameWindowId);
+					DomProcessorHelper.setFrameEmptySrc(frameElement);
+					frameElement.setAttribute("sandbox", "");
+					if (frameData) {
+						if (initialization) {
+							const options = Object.create(this.options);
+							options.insertSingleFileComment = false;
+							options.insertFaviconLink = false;
+							options.url = frameData.baseURI;
+							options.windowId = frameWindowId;
+							if (frameData.content) {
+								options.content = frameData.content;
+								frameData.processor = new PageProcessor(options);
+								frameData.frameElement = frameElement;
+								await frameData.processor.loadPage();
+								return frameData.processor.initialize();
+							}
+						} else {
+							if (frameData.processor) {
+								await frameData.processor.preparePageData();
+								const pageData = await frameData.processor.getPageData();
+								frameElement.removeAttribute(WIN_ID_ATTRIBUTE_NAME);
+								DomProcessorHelper.setFrameContent(frameElement, pageData.content);
+							}
 						}
 					}
 				}