Explorar o código

added new mechanism to trigger lazy loaded images

Gildas %!s(int64=7) %!d(string=hai) anos
pai
achega
16ff160980

+ 18 - 0
extension/core/content/content.js

@@ -82,6 +82,9 @@ this.singlefile.top = this.singlefile.top || (() => {
 				options.selected = false;
 			}
 		}
+		if (options.lazyLoadImages) {
+			await lazyLoadResources();
+		}
 		await processor.initialize();
 		await processor.preparePageData();
 		const page = processor.getPageData();
@@ -101,6 +104,21 @@ this.singlefile.top = this.singlefile.top || (() => {
 		return page;
 	}
 
+	async function lazyLoadResources() {
+		const scriptURL = browser.runtime.getURL("lib/single-file/lazy-loader-before.js");
+		const scriptElement = document.createElement("script");
+		scriptElement.src = scriptURL;
+		document.body.appendChild(scriptElement);
+		const promise = new Promise(resolve => scriptElement.onload = () => setTimeout(() => {
+			const scriptURL = browser.runtime.getURL("lib/single-file/lazy-loader-after.js");
+			const scriptElement = document.createElement("script");
+			scriptElement.src = scriptURL;
+			document.body.appendChild(scriptElement);
+			resolve();
+		}, 100));
+		return promise;
+	}
+
 	function revokeDownloadURL(page) {
 		URL.revokeObjectURL(page.url);
 	}

+ 1 - 0
lib/browser-polyfill/custom-browser-polyfill.js

@@ -214,6 +214,7 @@
 						}
 					})
 				),
+				getURL: (path) => chrome.runtime.getURL(path),
 				onInstalled: {
 					addListener: listener => chrome.runtime.onInstalled.addListener(listener)
 				},

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

@@ -0,0 +1,9 @@
+/* global window, dispatchEvent, Element, UIEvent */
+
+(() => {
+
+	Element.prototype.getBoundingClientRect = window._singleFile_getBoundingClientRect;
+	delete window._singleFile_getBoundingClientRect;
+	dispatchEvent(new UIEvent("scroll"));
+
+})();

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

@@ -0,0 +1,16 @@
+/* global window, screen, dispatchEvent, Element, UIEvent */
+
+(() => {
+
+	window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
+	Element.prototype.getBoundingClientRect = function () {
+		const boundingRect = window._singleFile_getBoundingClientRect.call(this);
+		const top = (boundingRect.top > 0 && boundingRect.top < screen.height) ? boundingRect.top : 0;
+		const left = (boundingRect.left > 0 && boundingRect.left < screen.width) ? boundingRect.left : 0;
+		const bottom = (boundingRect.bottom > 0 && boundingRect.bottom < screen.height) ? boundingRect.bottom : screen.height;
+		const right = (boundingRect.right > 0 && boundingRect.right < screen.width) ? boundingRect.bottom : screen.width;
+		return { x: boundingRect.x, y: boundingRect.y, top, bottom, left, right, width: boundingRect.width, height: boundingRect.height };
+	};
+	dispatchEvent(new UIEvent("scroll"));
+
+})();

+ 4 - 0
manifest.json

@@ -102,5 +102,9 @@
 		}
 	},
 	"incognito": "spanning",
+	"web_accessible_resources": [
+		"lib/single-file/lazy-loader-before.js",
+		"lib/single-file/lazy-loader-after.js"
+	],
 	"manifest_version": 2
 }