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

more robust way to find single file comment

Gildas 7 лет назад
Родитель
Сommit
2009a43c93
1 измененных файлов с 6 добавлено и 2 удалено
  1. 6 2
      extension/ui/content/infobar.js

+ 6 - 2
extension/ui/content/infobar.js

@@ -28,8 +28,8 @@ this.singlefile.infobar = this.singlefile.infobar || (() => {
 
 	if (window == top) {
 		document.addEventListener("DOMContentLoaded", async () => {
-			const singleFileComment = document.documentElement.childNodes[0];
-			if (singleFileComment.nodeType == Node.COMMENT_NODE && singleFileComment.textContent.includes(SINGLEFILE_COMMENT)) {
+			const singleFileComment = findSingleFileComment();
+			if (singleFileComment) {
 				const info = singleFileComment.textContent.split("\n");
 				const [, , url, saveDate] = info;
 				const config = await browser.runtime.sendMessage({ getConfig: true });
@@ -41,6 +41,10 @@ this.singlefile.infobar = this.singlefile.infobar || (() => {
 	}
 	return true;
 
+	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);
+	}
+
 	function initInfobar(url, saveDate) {
 		let infobarElement = document.querySelector(INFOBAR_TAGNAME);
 		if (!infobarElement) {