Forráskód Böngészése

moved code related to lazy loading in /lib/lazy

Gildas 7 éve
szülő
commit
301f274394

+ 3 - 0
extension/core/bg/script-loader.js

@@ -70,6 +70,9 @@ singlefile.scriptLoader = (() => {
 			"/lib/single-file/css-medias-minifier.js",
 			"/lib/single-file/css-rules-matcher.js",
 			"/lib/single-file/css-rules-minifier.js"
+		],
+		lazyLoadImages: [
+			"/lib/lazy/content-lazy-loader.js"
 		]
 	};
 

+ 2 - 37
extension/core/content/content.js

@@ -18,12 +18,11 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, SingleFile, singlefile, frameTree, document, Blob, MouseEvent, getSelection, prompt, addEventListener, Node, window, timeout, MutationObserver */
+/* global browser, SingleFile, singlefile, frameTree, document, Blob, MouseEvent, getSelection, prompt, addEventListener, Node, window, lazyLoader */
 
 this.singlefile.top = this.singlefile.top || (() => {
 
 	const MESSAGE_PREFIX = "__SingleFile__::";
-	const TIMEOUT_LAZY_LOADING = 500;
 	const SingleFileClass = SingleFile.getClass();
 
 	let processing = false;
@@ -109,7 +108,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 			}
 		};
 		if (options.lazyLoadImages) {
-			await lazyLoadResources();
+			await lazyLoader.process();
 		}
 		await processor.initialize();
 		await processor.preparePageData();
@@ -128,40 +127,6 @@ this.singlefile.top = this.singlefile.top || (() => {
 		return page;
 	}
 
-	async function lazyLoadResources() {
-		const scriptURL = browser.runtime.getURL("lib/single-file/lazy-loader-before.js");
-		const scriptBeforeElement = document.createElement("script");
-		scriptBeforeElement.src = scriptURL;
-		document.body.appendChild(scriptBeforeElement);
-		let timeoutId;
-		const promise = new Promise(resolve => {
-			scriptBeforeElement.onload = () => scriptBeforeElement.remove();
-			const observer = new MutationObserver(mutationsList => {
-				mutationsList.forEach(mutation => {
-					if (mutation.attributeName == "src" || mutation.attributeName == "srcset") {
-						timeoutId = deferLazyLoadEnd(timeoutId, observer, resolve);
-					}
-				});
-			});
-			observer.observe(document, { attributes: true, childList: true, subtree: true });
-			timeoutId = deferLazyLoadEnd(resolve);
-		});
-		return promise;
-	}
-
-	function deferLazyLoadEnd(timeoutId, observer, resolve) {
-		timeout.clear(timeoutId);
-		return timeout.set(() => {
-			resolve();
-			const scriptURL = browser.runtime.getURL("lib/single-file/lazy-loader-after.js");
-			const scriptAfterElement = document.createElement("script");
-			scriptAfterElement.src = scriptURL;
-			document.body.appendChild(scriptAfterElement);
-			scriptAfterElement.onload = () => scriptAfterElement.remove();
-			observer.disconnect();
-		}, TIMEOUT_LAZY_LOADING);
-	}
-
 	function markSelectedContent() {
 		const selection = getSelection();
 		const range = selection.rangeCount ? selection.getRangeAt(0) : null;

+ 72 - 0
lib/lazy/content-lazy-loader.js

@@ -0,0 +1,72 @@
+/*
+ * Copyright 2018 Gildas Lormeau
+ * contact : gildas.lormeau <at> gmail.com
+ * 
+ * This file is part of SingleFile.
+ *
+ *   SingleFile is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Lesser General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SingleFile is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Lesser General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Lesser General Public License
+ *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* global browser, document, timeout, MutationObserver */
+
+this.lazyLoader = this.lazyLoader || (() => {
+
+	const LAZY_LOADING_TIMEOUT = 500;
+	const MAX_LAZY_LOADING_TIMEOUT = 30000;
+
+	return {
+		process: () => lazyLoadResources()
+	};
+
+	async function lazyLoadResources() {
+		const scriptURL = browser.runtime.getURL("lib/lazy/lazy-loader-before.js");
+		const scriptBeforeElement = document.createElement("script");
+		scriptBeforeElement.src = scriptURL;
+		document.body.appendChild(scriptBeforeElement);
+		let timeoutId;
+		const promise = new Promise(resolve => {
+			scriptBeforeElement.onload = () => scriptBeforeElement.remove();
+			const observer = new MutationObserver(mutationsList => {
+				mutationsList.forEach(mutation => {
+					if (mutation.attributeName == "src" || mutation.attributeName == "srcset") {
+						timeoutId = deferLazyLoadEnd(timeoutId, observer, resolve);
+					}
+				});
+			});
+			observer.observe(document, { attributes: true, childList: true, subtree: true });
+			timeoutId = deferLazyLoadEnd(timeoutId, observer, resolve);
+			timeout.set(() => {
+				timeout.clear(timeoutId);
+				lazyLoadEnd(observer, resolve);
+			}, MAX_LAZY_LOADING_TIMEOUT);
+		});
+		return promise;
+	}
+
+	function deferLazyLoadEnd(timeoutId, observer, resolve) {
+		timeout.clear(timeoutId);
+		return timeout.set(() => lazyLoadEnd(observer, resolve), LAZY_LOADING_TIMEOUT);
+	}
+
+	function lazyLoadEnd(observer, resolve) {
+		resolve();
+		const scriptURL = browser.runtime.getURL("lib/lazy/lazy-loader-after.js");
+		const scriptAfterElement = document.createElement("script");
+		scriptAfterElement.src = scriptURL;
+		document.body.appendChild(scriptAfterElement);
+		scriptAfterElement.onload = () => scriptAfterElement.remove();
+		observer.disconnect();
+	}
+
+})();

+ 0 - 0
lib/single-file/lazy-loader-after.js → lib/lazy/lazy-loader-after.js


+ 0 - 0
lib/single-file/lazy-loader-before.js → lib/lazy/lazy-loader-before.js


+ 2 - 2
manifest.json

@@ -115,8 +115,8 @@
 	},
 	"incognito": "spanning",
 	"web_accessible_resources": [
-		"lib/single-file/lazy-loader-before.js",
-		"lib/single-file/lazy-loader-after.js"
+		"lib/lazy/lazy-loader-before.js",
+		"lib/lazy/lazy-loader-after.js"
 	],
 	"manifest_version": 2,
 	"default_locale": "en"