Jelajahi Sumber

moved code into common.js

Gildas 7 tahun lalu
induk
melakukan
3ba231c910

+ 2 - 20
extension/core/content/content-frame.js

@@ -18,14 +18,14 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, window, top, document */
+/* global browser, window, top, document, common */
 
 this.singlefile.frame = this.singlefile.frame || (() => {
 
 	if (window != top) {
 		browser.runtime.onMessage.addListener(async message => {
 			if (message.processStart) {
-				message.options.content = getDoctype(document) + document.documentElement.outerHTML;
+				message.options.content = common.getDoctype(document) + document.documentElement.outerHTML;
 				message.options.frameId = null;
 				message.options.url = document.location.href;
 				top.postMessage("__SingleFile__::" + JSON.stringify(message), "*");
@@ -35,22 +35,4 @@ this.singlefile.frame = this.singlefile.frame || (() => {
 	}
 	return true;
 
-	function getDoctype(doc) {
-		const docType = doc.doctype;
-		let docTypeString;
-		if (docType) {
-			docTypeString = "<!DOCTYPE " + docType.nodeName;
-			if (docType.publicId) {
-				docTypeString += " PUBLIC \"" + docType.publicId + "\"";
-				if (docType.systemId)
-					docTypeString += " \"" + docType.systemId + "\"";
-			} else if (docType.systemId)
-				docTypeString += " SYSTEM \"" + docType.systemId + "\"";
-			if (docType.internalSubset)
-				docTypeString += " [" + docType.internalSubset + "]";
-			return docTypeString + ">\n";
-		}
-		return "";
-	}
-
 })();

+ 50 - 1
lib/single-file/common.js

@@ -25,7 +25,10 @@ this.common = this.common || (() => {
 
 	return {
 		preProcessDoc,
-		postProcessDoc
+		postProcessDoc,
+		getCanvasData,
+		getEmptyStyleRulesText,
+		getDoctype
 	};
 
 	function preProcessDoc(doc, win, options) {
@@ -70,4 +73,50 @@ this.common = this.common || (() => {
 		}
 	}
 
+	function getCanvasData(doc) {
+		if (doc) {
+			const canvasData = [];
+			doc.querySelectorAll("canvas").forEach(canvasElement => {
+				try {
+					canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
+				} catch (error) {
+					canvasData.push(null);
+				}
+			});
+			return canvasData;
+		}
+	}
+
+	function getEmptyStyleRulesText(doc) {
+		if (doc) {
+			const textData = [];
+			doc.querySelectorAll("style").forEach(styleElement => {
+				if (!styleElement.textContent) {
+					textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
+				}
+			});
+			return textData;
+		}
+	}
+
+	function getDoctype(doc) {
+		const docType = doc.doctype;
+		let docTypeStr;
+		if (docType) {
+			docTypeStr = "<!DOCTYPE " + docType.nodeName;
+			if (docType.publicId) {
+				docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
+				if (docType.systemId) {
+					docTypeStr += " \"" + docType.systemId + "\"";
+				}
+			} else if (docType.systemId) {
+				docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
+			} if (docType.internalSubset) {
+				docTypeStr += " [" + docType.internalSubset + "]";
+			}
+			return docTypeStr + ">\n";
+		}
+		return "";
+	}
+
 })();

+ 6 - 52
lib/single-file/frame-tree/content/frame-tree.js

@@ -49,9 +49,9 @@ this.FrameTree = this.FrameTree || (() => {
 				method: "FrameTree.getDataResponse",
 				windowId: message.windowId,
 				tabId: message.tabId,
-				content: getDoctype(document) + document.documentElement.outerHTML,
-				emptyStyleRulesText: getEmptyStyleRulesText(document),
-				canvasData: getCanvasData(document),
+				content: common.getDoctype(document) + document.documentElement.outerHTML,
+				emptyStyleRulesText: common.getEmptyStyleRulesText(document),
+				canvasData: common.getCanvasData(document),
 				baseURI: document.baseURI,
 				title: document.title
 			}).catch(() => {/* ignored */ });
@@ -164,9 +164,9 @@ this.FrameTree = this.FrameTree || (() => {
 					if (message.method == "getDataRequest" && message.windowId == frameWinId) {
 						topWindow.removeEventListener("message", onMessage, false);
 						common.preProcessDoc(frameDoc, frameWindow, message.options);
-						const content = getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
-						const emptyStyleRulesText = getEmptyStyleRulesText(frameDoc);
-						const canvasData = getCanvasData(frameDoc);
+						const content = common.getDoctype(frameDoc) + frameDoc.documentElement.outerHTML;
+						const emptyStyleRulesText = common.getEmptyStyleRulesText(frameDoc);
+						const canvasData = common.getCanvasData(frameDoc);
 						top.postMessage(MESSAGE_PREFIX + "::" + JSON.stringify({ method: "getDataResponse", windowId: message.windowId, content, baseURI: frameDoc.baseURI, title: document.title, emptyStyleRulesText, canvasData }), "*");
 						common.postProcessDoc(frameDoc, frameWindow, message.options);
 					}
@@ -188,50 +188,4 @@ this.FrameTree = this.FrameTree || (() => {
 		dataRequestCallbacks.get(message.windowId)(message);
 	}
 
-	function getDoctype(doc) {
-		const docType = doc.doctype;
-		let docTypeStr;
-		if (docType) {
-			docTypeStr = "<!DOCTYPE " + docType.nodeName;
-			if (docType.publicId) {
-				docTypeStr += " PUBLIC \"" + docType.publicId + "\"";
-				if (docType.systemId) {
-					docTypeStr += " \"" + docType.systemId + "\"";
-				}
-			} else if (docType.systemId) {
-				docTypeStr += " SYSTEM \"" + docType.systemId + "\"";
-			} if (docType.internalSubset) {
-				docTypeStr += " [" + docType.internalSubset + "]";
-			}
-			return docTypeStr + ">\n";
-		}
-		return "";
-	}
-
-	function getEmptyStyleRulesText(doc) {
-		if (doc) {
-			const textData = [];
-			doc.querySelectorAll("style").forEach(styleElement => {
-				if (!styleElement.textContent) {
-					textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
-				}
-			});
-			return textData;
-		}
-	}
-
-	function getCanvasData(doc) {
-		if (doc) {
-			const canvasData = [];
-			doc.querySelectorAll("canvas").forEach(canvasElement => {
-				try {
-					canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
-				} catch (error) {
-					canvasData.push(null);
-				}
-			});
-			return canvasData;
-		}
-	}
-
 })();

+ 8 - 0
lib/single-file/single-file-browser.js

@@ -141,6 +141,14 @@ this.SingleFile = this.SingleFile || (() => {
 		static postProcessDoc(doc, options) {
 			common.postProcessDoc(doc, options);
 		}
+
+		static getEmptyStyleRulesText(doc) {
+			return common.getEmptyStyleRulesText(doc);
+		}
+
+		static getCanvasData(doc) {
+			return common.getCanvasData(doc);
+		}
 	}
 
 	return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };

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

@@ -77,8 +77,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			this.options.url = this.options.url || this.options.doc.location.href;
 			this.processor = new DOMProcessor(options);
 			if (this.options.doc) {
-				this.options.canvasData = this.processor.getCanvasData();
-				this.options.emptyStyleRulesText = this.processor.getEmptyStyleRulesText();
+				this.options.canvasData = DOM.getCanvasData(this.options.doc);
+				this.options.emptyStyleRulesText = DOM.getEmptyStyleRulesText(this.options.doc);
 				DOM.preProcessDoc(this.options.doc, this.options.win, this.options);
 			}
 			this.options.content = this.options.content || (this.options.doc ? DOM.serialize(this.options.doc, false) : null);
@@ -324,28 +324,6 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			this.options.doc.querySelectorAll("[" + WIN_ID_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(WIN_ID_ATTRIBUTE_NAME));
 		}
 
-		getEmptyStyleRulesText() {
-			const textData = [];
-			this.options.doc.querySelectorAll("style").forEach(styleElement => {
-				if (!styleElement.textContent) {
-					textData.push(Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n"));
-				}
-			});
-			return textData;
-		}
-
-		getCanvasData() {
-			const canvasData = [];
-			this.options.doc.querySelectorAll("canvas").forEach(canvasElement => {
-				try {
-					canvasData.push({ dataURI: canvasElement.toDataURL("image/png", ""), width: canvasElement.clientWidth, height: canvasElement.clientHeight });
-				} catch (error) {
-					canvasData.push(null);
-				}
-			});
-			return canvasData;
-		}
-
 		enableDisabledNoscriptTags(noscriptTags) {
 			noscriptTags.forEach(element => {
 				const noscriptElement = this.options.doc.createElement("noscript");