瀏覽代碼

emulate big screen instead of emulating scrolling

Gildas 7 年之前
父節點
當前提交
322eebd875
共有 3 個文件被更改,包括 25 次插入56 次删除
  1. 9 7
      lib/lazy/content/content-lazy-loader.js
  2. 8 14
      lib/lazy/web/web-lazy-loader-after.js
  3. 8 35
      lib/lazy/web/web-lazy-loader-before.js

+ 9 - 7
lib/lazy/content/content-lazy-loader.js

@@ -23,7 +23,6 @@
 this.lazyLoader = this.lazyLoader || (() => {
 
 	const SCRIPT_TAG_NAME = "script";
-	const MONITORED_ATTRIBUTES = ["src", "srcset"];
 	const ATTRIBUTES_MUTATION_TYPE = "attributes";
 	const SCRIPT_BEFORE_PATH = "lib/lazy/web/web-lazy-loader-before.js";
 	const SCRIPT_AFTER_PATH = "lib/lazy/web/web-lazy-loader-after.js";
@@ -36,20 +35,23 @@ this.lazyLoader = this.lazyLoader || (() => {
 			const maxTimeoutId = await setTimeout(() => {
 				clearTimeout(timeoutId);
 				lazyLoadEnd(maxTimeoutId, idleTimeoutId, observer, options, resolve);
-			}, options.maxLazyLoadImagesIdleTime * 3);
+			}, options.maxLazyLoadImagesIdleTime * 5);
 			const observer = new MutationObserver(async mutations => {
 				mutations = mutations.filter(mutation => mutation.type == ATTRIBUTES_MUTATION_TYPE);
 				if (mutations.length) {
-					mutations.forEach(mutation => {
-						if (mutation.target.src) {
+					const updated = mutations.filter(mutation => {
+						if (mutation.attributeName == "src") {
 							mutation.target.setAttribute("data-lazy-loaded-src", mutation.target.src);
 						}
+						return mutation.attributeName != "data-lazy-loaded-src";
 					});
-					srcAttributeChanged = true;
-					timeoutId = await deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve);
+					if (updated.length) {
+						srcAttributeChanged = true;
+						timeoutId = await deferLazyLoadEnd(timeoutId, maxTimeoutId, idleTimeoutId, observer, options, resolve);
+					}
 				}
 			});
-			observer.observe(document, { attributeFilter: MONITORED_ATTRIBUTES, subtree: true, childList: true, attributes: true });
+			observer.observe(document, { subtree: true, childList: true, attributes: true });
 			const idleTimeoutId = await setTimeout(() => {
 				if (!srcAttributeChanged) {
 					clearTimeout(timeoutId);

+ 8 - 14
lib/lazy/web/web-lazy-loader-after.js

@@ -18,22 +18,16 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global window, Element, document, dispatchEvent, UIEvent, clearTimeout */
+/* global screen, window, document, dispatchEvent, UIEvent */
 
 (() => {
 
-	if (window._singleFile_getBoundingClientRect) {
-		Element.prototype.getBoundingClientRect = window._singleFile_getBoundingClientRect;
-		delete window._singleFile_getBoundingClientRect;
-		dispatchEvent(new UIEvent("scroll"));
-	}
-	if (window._singleFile_timeoutScroll) {
-		clearTimeout(window._singleFile_timeoutScroll);
-		delete window._singleFile_timeoutScroll;
-		delete document.documentElement.scrollTop;
-		window.__defineGetter__("pageYOffset", () => document.documentElement.scrollTop);
-		window.__defineGetter__("scrollY", () => document.documentElement.scrollTop);
-		dispatchEvent(new UIEvent("scroll"));
-	}
+	window.__defineGetter__("innerHeight", () => document.documentElement.clientHeight);
+	window.__defineGetter__("innerWidth", () => document.documentElement.clientWidth);
+	delete document.documentElement.clientHeight;
+	delete document.documentElement.clientWidth;
+	delete screen.height;
+	delete screen.width;
+	dispatchEvent(new UIEvent("resize"));
 
 })();

+ 8 - 35
lib/lazy/web/web-lazy-loader-before.js

@@ -18,43 +18,16 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global window, Element, document, dispatchEvent, UIEvent, setTimeout */
+/* global screen, window, document, dispatchEvent, UIEvent */
 
 (() => {
 
-	const SCROLL_LENGTH = 200;
-	const SCROLL_DELAY = 100;
-
-	window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
-	Element.prototype.getBoundingClientRect = function () {
-		const boundingRect = window._singleFile_getBoundingClientRect.call(this);
-		const quarterLeft = Math.floor(window.innerWidth / 4);
-		const quarterTop = Math.floor(window.innerHeight / 4);
-		const top = (boundingRect.top > 0 && boundingRect.top < window.innerHeight) ? boundingRect.top : boundingRect.top > window.innerHeight ? window.innerHeight - quarterTop : quarterTop;
-		const left = (boundingRect.left > 0 && boundingRect.left < window.innerHeight) ? boundingRect.left : boundingRect.left > window.innerHeight ? window.innerHeight - quarterLeft : quarterLeft;
-		const bottom = top + boundingRect.height;
-		const right = left + boundingRect.width;
-		return { x: boundingRect.x, y: boundingRect.y, top, bottom, left, right, width: boundingRect.width, height: boundingRect.height };
-	};
-	dispatchEvent(new UIEvent("scroll"));
-	const scrollTop = document.documentElement.scrollTop;
-	scroll(SCROLL_LENGTH, document.documentElement.scrollHeight - window.innerHeight);
-
-	function scroll(offsetY, maxOffsetY) {
-		document.documentElement.__defineGetter__("scrollTop", () => offsetY);
-		window.__defineGetter__("scrollY", () => offsetY);
-		window.__defineGetter__("pageYOffset", () => offsetY);
-		processScroll();
-
-		function processScroll() {
-			if (offsetY < maxOffsetY) {
-				offsetY += SCROLL_LENGTH;
-			} else {
-				offsetY = scrollTop;
-			}
-			dispatchEvent(new UIEvent("scroll"));
-			window._singleFile_timeoutScroll = setTimeout(processScroll, SCROLL_DELAY);
-		}
-	}
+	window.__defineGetter__("innerHeight", () => document.documentElement.scrollHeight);
+	window.__defineGetter__("innerWidth", () => document.documentElement.scrollWidth);
+	document.documentElement.__defineGetter__("clientHeight", () => document.documentElement.scrollHeight);
+	document.documentElement.__defineGetter__("clientWidth", () => document.documentElement.scrollWidth);
+	screen.__defineGetter__("height", () => document.documentElement.scrollHeight);
+	screen.__defineGetter__("width", () => document.documentElement.scrollWidth);
+	dispatchEvent(new UIEvent("resize"));
 
 })();