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

refactored infobar icon displaying

Gildas пре 7 година
родитељ
комит
4a3e0d96db
1 измењених фајлова са 25 додато и 12 уклоњено
  1. 25 12
      extension/ui/content/infobar.js

+ 25 - 12
extension/ui/content/infobar.js

@@ -27,22 +27,35 @@ this.singlefile.infobar = this.singlefile.infobar || (() => {
 	const SINGLEFILE_COMMENT = "Archive processed by SingleFile";
 
 	if (window == top) {
-		document.addEventListener("DOMContentLoaded", async () => {
-			const singleFileComment = findSingleFileComment();
-			if (singleFileComment) {
-				const info = singleFileComment.textContent.split("\n");
-				const [, , url, saveDate] = info;
-				const config = await browser.runtime.sendMessage({ getConfig: true });
-				if (config.displayInfobar) {
-					initInfobar(url, saveDate);
-				}
-			}
-		});
+		if (document.readyState == "loading") {
+			document.addEventListener("DOMContentLoaded", displayIcon, false);
+		} else {
+			displayIcon();
+		}
 	}
 	return true;
 
+	async function displayIcon() {
+		let singleFileComment = document.documentElement.childNodes[0];
+		if (!isInfobar(singleFileComment)) {
+			singleFileComment = findSingleFileComment();
+		}
+		if (singleFileComment) {
+			const info = singleFileComment.textContent.split("\n");
+			const [, , url, saveDate] = info;
+			const config = await browser.runtime.sendMessage({ getConfig: true });
+			if (config.displayInfobar) {
+				initInfobar(url, saveDate);
+			}
+		}
+	}
+
 	function findSingleFileComment(node = document.documentElement) {
-		return node.childNodes && node.childNodes.length ? Array.from(node.childNodes).find(findSingleFileComment) : node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
+		return node.childNodes && node.childNodes.length ? Array.from(node.childNodes).find(findSingleFileComment) : isInfobar(node);
+	}
+
+	function isInfobar(node) {
+		return node.nodeType == Node.COMMENT_NODE && node.textContent.includes(SINGLEFILE_COMMENT);
 	}
 
 	function initInfobar(url, saveDate) {