Explorar o código

use globalthis

Gildas %!s(int64=5) %!d(string=hai) anos
pai
achega
0cf671ed45

+ 62 - 54
lib/single-file/index.js

@@ -21,66 +21,74 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile = this.singlefile || {
-	lib: {
-		init(initOptions) {
-			const util = this.util.getInstance(initOptions);
-			this.SingleFile = this.core.getClass(util, this.vendor.cssTree);
-		},
-		processors: {
-			frameTree: {
-				content: {}
-			},
-			hooks: {
-				content: {}
+this.singlefile = this.singlefile || (globalThis => {
+
+	return {
+		lib: {
+			init,
+			getPageData,
+			processors: {
+				frameTree: {
+					content: {}
+				},
+				hooks: {
+					content: {}
+				},
+				lazy: {
+					content: {}
+				}
 			},
-			lazy: {
-				content: {}
-			}
-		},
-		vendor: {},
-		modules: {},
-		async getPageData(options = {}, initOptions, doc = window.document, win = window) {
-			const frames = this.processors.frameTree.content.frames;
-			let framesSessionId;
-			this.init(initOptions);
-			if (doc && window) {
-				this.helper.initDoc(doc);
-				const preInitializationPromises = [];
-				if (!options.saveRawPage) {
-					if (!options.removeFrames && frames && window.frames && window.frames.length) {
-						let frameTreePromise;
-						if (options.loadDeferredImages) {
-							frameTreePromise = new Promise(resolve => window.setTimeout(() => resolve(frames.getAsync(options)), options.loadDeferredImagesMaxIdleTime - frames.TIMEOUT_INIT_REQUEST_MESSAGE));
-						} else {
-							frameTreePromise = frames.getAsync(options);
-						}
-						preInitializationPromises.push(frameTreePromise);
-					}
+			vendor: {},
+			modules: {},
+		}
+	};
+
+	function init(initOptions) {
+		const util = this.util.getInstance(initOptions);
+		this.SingleFile = this.core.getClass(util, this.vendor.cssTree);
+	}
+
+	async function getPageData(options = {}, initOptions, doc = globalThis.document, win = globalThis) {
+		const frames = this.processors.frameTree.content.frames;
+		let framesSessionId;
+		this.init(initOptions);
+		if (doc && win) {
+			this.helper.initDoc(doc);
+			const preInitializationPromises = [];
+			if (!options.saveRawPage) {
+				if (!options.removeFrames && frames && globalThis.frames && globalThis.frames.length) {
+					let frameTreePromise;
 					if (options.loadDeferredImages) {
-						preInitializationPromises.push(this.processors.lazy.content.loader.process(options));
+						frameTreePromise = new Promise(resolve => globalThis.setTimeout(() => resolve(frames.getAsync(options)), options.loadDeferredImagesMaxIdleTime - frames.TIMEOUT_INIT_REQUEST_MESSAGE));
+					} else {
+						frameTreePromise = frames.getAsync(options);
 					}
+					preInitializationPromises.push(frameTreePromise);
 				}
-				[options.frames] = await Promise.all(preInitializationPromises);
-				framesSessionId = options.frames && options.frames.sessionId;
-			}
-			options.doc = doc;
-			options.win = win;
-			options.insertSingleFileComment = true;
-			options.insertCanonicalLink = true;
-			options.onprogress = event => {
-				if (event.type === event.RESOURCES_INITIALIZED && doc && window && options.loadDeferredImages) {
-					this.processors.lazy.content.loader.resetZoomLevel(options);
+				if (options.loadDeferredImages) {
+					preInitializationPromises.push(this.processors.lazy.content.loader.process(options));
 				}
-			};
-			const processor = new this.SingleFile(options);
-			await processor.run();
-			if (framesSessionId) {
-				frames.cleanup(framesSessionId);
 			}
-			return await processor.getPageData();
+			[options.frames] = await Promise.all(preInitializationPromises);
+			framesSessionId = options.frames && options.frames.sessionId;
 		}
+		options.doc = doc;
+		options.win = win;
+		options.insertSingleFileComment = true;
+		options.insertCanonicalLink = true;
+		options.onprogress = event => {
+			if (event.type === event.RESOURCES_INITIALIZED && doc && win && options.loadDeferredImages) {
+				this.processors.lazy.content.loader.resetZoomLevel(options);
+			}
+		};
+		const processor = new this.SingleFile(options);
+		await processor.run();
+		if (framesSessionId) {
+			frames.cleanup(framesSessionId);
+		}
+		return await processor.getPageData();
 	}
-};
+
+})(typeof globalThis == "object" ? globalThis : window);

+ 5 - 5
lib/single-file/modules/css-fonts-alt-minifier.js

@@ -21,13 +21,13 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fontsAltMinifier || (() => {
+this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fontsAltMinifier || (globalThis => {
 
 	const singlefile = this.singlefile;
 
-	const FontFace = window.FontFace;
+	const FontFace = globalThis.FontFace;
 
 	const REGEXP_URL_SIMPLE_QUOTES_FN = /url\s*\(\s*'(.*?)'\s*\)/i;
 	const REGEXP_URL_DOUBLE_QUOTES_FN = /url\s*\(\s*"(.*?)"\s*\)/i;
@@ -232,7 +232,7 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 										await Promise.race(
 											[
 												fontFace.load().then(() => fontFace.loaded).then(() => source.valid = true),
-												new Promise(resolve => window.setTimeout(() => { source.valid = true; resolve(); }, FONT_MAX_LOAD_DELAY))
+												new Promise(resolve => globalThis.setTimeout(() => { source.valid = true; resolve(); }, FONT_MAX_LOAD_DELAY))
 											]);
 									} catch (error) {
 										// ignored
@@ -328,4 +328,4 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 		};
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 5 - 5
lib/single-file/modules/css-fonts-minifier.js

@@ -21,9 +21,9 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMinifier || (() => {
+this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMinifier || (globalThis => {
 
 	const singlefile = this.singlefile;
 
@@ -61,11 +61,11 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		});
 		workStyleElement.remove();
 		docContent += doc.body.innerText;
-		if (window.getComputedStyle && options.doc) {
+		if (globalThis.getComputedStyle && options.doc) {
 			fontsInfo.used = fontsInfo.used.map(fontNames => fontNames.map(familyName => {
 				const matchedVar = familyName.match(/^var\((--.*)\)$/);
 				if (matchedVar && matchedVar[1]) {
-					const computedFamilyName = window.getComputedStyle(options.doc.body).getPropertyValue(matchedVar[1]);
+					const computedFamilyName = globalThis.getComputedStyle(options.doc.body).getPropertyValue(matchedVar[1]);
 					return (computedFamilyName && computedFamilyName.split(",").map(name => singlefile.lib.helper.normalizeFontFamily(name))) || familyName;
 				}
 				return familyName;
@@ -354,4 +354,4 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		return "\\u{" + range + "}";
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 20 - 21
lib/single-file/processors/frame-tree/content/content-frame-tree.js

@@ -21,9 +21,9 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.processors.frameTree.content.frames || (() => {
+this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.processors.frameTree.content.frames || (globalThis => {
 
 	const singlefile = this.singlefile;
 
@@ -39,17 +39,16 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 	const TIMEOUT_INIT_RESPONSE_MESSAGE = 10000;
 	const TOP_WINDOW_ID = "0";
 	const WINDOW_ID_SEPARATOR = ".";
-	const TOP_WINDOW = window == window.top;
+	const TOP_WINDOW = globalThis.window == globalThis.top;
 
-	const browser = this.browser;
-	const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-	const top = window.top;
-	const MessageChannel = window.MessageChannel;
-	const document = window.document;
+	const browser = globalThis.browser;
+	const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
+	const top = globalThis.top;
+	const MessageChannel = globalThis.MessageChannel;
+	const document = globalThis.document;
 
 	const sessions = new Map();
 	let windowId;
-
 	if (TOP_WINDOW) {
 		windowId = TOP_WINDOW_ID;
 		if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
@@ -75,7 +74,7 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 					sendMessage(event.source, { method: ACK_INIT_REQUEST_MESSAGE, windowId: message.windowId, sessionId: message.sessionId });
 				}
 				if (!TOP_WINDOW) {
-					window.stop();
+					globalThis.stop();
 					if (message.options.loadDeferredImages && singlefile.lib.processors.lazy.content.loader) {
 						singlefile.lib.processors.lazy.content.loader.process(message.options);
 					}
@@ -138,21 +137,21 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 	}
 
 	function getNewSessionId() {
-		return window.crypto.getRandomValues(new Uint32Array(32)).join("");
+		return globalThis.crypto.getRandomValues(new Uint32Array(32)).join("");
 	}
 
 	function initRequestSync(message) {
 		const waitForUserScript = singlefile.lib.helper.waitForUserScript;
 		const sessionId = message.sessionId;
 		if (!TOP_WINDOW) {
-			windowId = window.frameId = message.windowId;
+			windowId = globalThis.frameId = message.windowId;
 		}
 		processFrames(document, message.options, windowId, sessionId);
 		if (!TOP_WINDOW) {
 			if (message.options.userScriptEnabled && waitForUserScript) {
 				waitForUserScript(singlefile.lib.helper.ON_BEFORE_CAPTURE_EVENT_NAME);
 			}
-			sendInitResponse({ frames: [getFrameData(document, window, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
+			sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
 			if (message.options.userScriptEnabled && waitForUserScript) {
 				waitForUserScript(singlefile.lib.helper.ON_AFTER_CAPTURE_EVENT_NAME);
 			}
@@ -164,14 +163,14 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 		const waitForUserScript = singlefile.lib.helper.waitForUserScript;
 		const sessionId = message.sessionId;
 		if (!TOP_WINDOW) {
-			windowId = window.frameId = message.windowId;
+			windowId = globalThis.frameId = message.windowId;
 		}
 		processFrames(document, message.options, windowId, sessionId);
 		if (!TOP_WINDOW) {
 			if (message.options.userScriptEnabled && waitForUserScript) {
 				await waitForUserScript(singlefile.lib.helper.ON_BEFORE_CAPTURE_EVENT_NAME);
 			}
-			sendInitResponse({ frames: [getFrameData(document, window, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
+			sendInitResponse({ frames: [getFrameData(document, globalThis, windowId, message.options)], sessionId, requestedFrameId: document.documentElement.dataset.requestedFrameId && windowId });
 			if (message.options.userScriptEnabled && waitForUserScript) {
 				await waitForUserScript(singlefile.lib.helper.ON_AFTER_CAPTURE_EVENT_NAME);
 			}
@@ -258,7 +257,7 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 			} catch (error) {
 				// ignored
 			}
-			requestTimeouts[windowId] = window.setTimeout(() => sendInitResponse({ frames: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
+			requestTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId, processed: true }], sessionId }), TIMEOUT_INIT_REQUEST_MESSAGE);
 		});
 		delete doc.documentElement.dataset.requestedFrameId;
 	}
@@ -294,7 +293,7 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 		if (session && session[type]) {
 			const timeout = session[type][windowId];
 			if (timeout) {
-				window.clearTimeout(timeout);
+				globalThis.clearTimeout(timeout);
 				delete session[type][windowId];
 			}
 		}
@@ -303,7 +302,7 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 	function createFrameResponseTimeout(sessionId, windowId) {
 		const session = sessions.get(sessionId);
 		if (session && session.responseTimeouts) {
-			session.responseTimeouts[windowId] = window.setTimeout(() => sendInitResponse({ frames: [{ windowId: windowId, processed: true }], sessionId: sessionId }), TIMEOUT_INIT_RESPONSE_MESSAGE);
+			session.responseTimeouts[windowId] = globalThis.setTimeout(() => sendInitResponse({ frames: [{ windowId: windowId, processed: true }], sessionId: sessionId }), TIMEOUT_INIT_RESPONSE_MESSAGE);
 		}
 	}
 
@@ -358,9 +357,9 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 		}
 	}
 
-	function getFrameData(document, window, windowId, options) {
+	function getFrameData(document, globalThis, windowId, options) {
 		const helper = singlefile.lib.helper;
-		const docData = helper.preProcessDoc(document, window, options);
+		const docData = helper.preProcessDoc(document, globalThis, options);
 		const content = helper.serialize(document);
 		helper.postProcessDoc(document, docData.markedElements);
 		const baseURI = document.baseURI.split("#")[0];
@@ -392,4 +391,4 @@ this.singlefile.lib.processors.frameTree.content.frames = this.singlefile.lib.pr
 		return frames;
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 62 - 62
lib/single-file/processors/hooks/content/content-hooks-frames-web.js

@@ -21,9 +21,9 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-(() => {
+(globalThis => {
 
 	const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
 	const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
@@ -49,16 +49,16 @@
 		featureSettings: "font-feature-settings"
 	};
 
-	const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-	const dispatchEvent = event => window.dispatchEvent(event);
-	const CustomEvent = window.CustomEvent;
-	const document = window.document;
-	const screen = window.screen;
-	const Element = window.Element;
-	const UIEvent = window.UIEvent;
-	const FileReader = window.FileReader;
-	const Blob = window.Blob;
-	const console = window.console;
+	const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
+	const dispatchEvent = event => globalThis.dispatchEvent(event);
+	const CustomEvent = globalThis.CustomEvent;
+	const document = globalThis.document;
+	const screen = globalThis.screen;
+	const Element = globalThis.Element;
+	const UIEvent = globalThis.UIEvent;
+	const FileReader = globalThis.FileReader;
+	const Blob = globalThis.Blob;
+	const console = globalThis.console;
 	const warn = (console && console.warn && ((...args) => console.warn(...args))) || (() => { });
 
 	const observers = new Map();
@@ -81,15 +81,15 @@
 		scrollingElement.__defineGetter__("clientWidth", () => scrollWidth);
 		screen.__defineGetter__("height", () => scrollHeight);
 		screen.__defineGetter__("width", () => scrollWidth);
-		window._singleFile_innerHeight = window.innerHeight;
-		window._singleFile_innerWidth = window.innerWidth;
-		window.__defineGetter__("innerHeight", () => scrollHeight);
-		window.__defineGetter__("innerWidth", () => scrollWidth);
+		globalThis._singleFile_innerHeight = globalThis.innerHeight;
+		globalThis._singleFile_innerWidth = globalThis.innerWidth;
+		globalThis.__defineGetter__("innerHeight", () => scrollHeight);
+		globalThis.__defineGetter__("innerWidth", () => scrollWidth);
 		if (!keepZoomLevel) {
-			if (!window._singleFile_getBoundingClientRect) {
-				window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
+			if (!globalThis._singleFile_getBoundingClientRect) {
+				globalThis._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
 				Element.prototype.getBoundingClientRect = function () {
-					const boundingRect = window._singleFile_getBoundingClientRect.call(this);
+					const boundingRect = globalThis._singleFile_getBoundingClientRect.call(this);
 					if (this == scrollingElement) {
 						boundingRect.__defineGetter__("height", () => scrollHeight);
 						boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
@@ -100,10 +100,10 @@
 				};
 			}
 		}
-		if (!window._singleFileImage) {
-			const Image = window.Image;
-			window._singleFileImage = window.Image;
-			window.__defineGetter__("Image", function () {
+		if (!globalThis._singleFileImage) {
+			const Image = globalThis.Image;
+			globalThis._singleFileImage = globalThis.Image;
+			globalThis.__defineGetter__("Image", function () {
 				return function () {
 					const image = new Image(...arguments);
 					const result = new Image(...arguments);
@@ -137,8 +137,8 @@
 			zoomFactorX = clientHeight / scrollHeight;
 			zoomFactorY = clientWidth / scrollWidth;
 		} else {
-			zoomFactorX = (clientHeight + window.scrollY) / scrollHeight;
-			zoomFactorY = (clientWidth + window.scrollX) / scrollWidth;
+			zoomFactorX = (clientHeight + globalThis.scrollY) / scrollHeight;
+			zoomFactorY = (clientWidth + globalThis.scrollX) / scrollWidth;
 		}
 		const zoomFactor = Math.min(zoomFactorX, zoomFactorY);
 		if (zoomFactor < 1) {
@@ -208,15 +208,15 @@
 			element.removeAttribute(LAZY_LOAD_ATTRIBUTE);
 		});
 		if (!keepZoomLevel) {
-			if (window._singleFile_getBoundingClientRect) {
-				Element.prototype.getBoundingClientRect = window._singleFile_getBoundingClientRect;
-				delete window._singleFile_getBoundingClientRect;
+			if (globalThis._singleFile_getBoundingClientRect) {
+				Element.prototype.getBoundingClientRect = globalThis._singleFile_getBoundingClientRect;
+				delete globalThis._singleFile_getBoundingClientRect;
 			}
 		}
-		if (window._singleFileImage) {
-			delete window.Image;
-			window.Image = window._singleFileImage;
-			delete window._singleFileImage;
+		if (globalThis._singleFileImage) {
+			delete globalThis.Image;
+			globalThis.Image = globalThis._singleFileImage;
+			delete globalThis._singleFileImage;
 		}
 		if (!keepZoomLevel) {
 			dispatchResizeEvent();
@@ -225,13 +225,13 @@
 
 	function resetScreenSize() {
 		const scrollingElement = document.scrollingElement || document.documentElement;
-		if (window._singleFile_innerHeight != null) {
-			window.innerHeight = window._singleFile_innerHeight;
-			delete window._singleFile_innerHeight;
+		if (globalThis._singleFile_innerHeight != null) {
+			globalThis.innerHeight = globalThis._singleFile_innerHeight;
+			delete globalThis._singleFile_innerHeight;
 		}
-		if (window._singleFile_innerWidth != null) {
-			window.innerWidth = window._singleFile_innerWidth;
-			delete window._singleFile_innerWidth;
+		if (globalThis._singleFile_innerWidth != null) {
+			globalThis.innerWidth = globalThis._singleFile_innerWidth;
+			delete globalThis._singleFile_innerWidth;
 		}
 		delete scrollingElement.clientHeight;
 		delete scrollingElement.clientWidth;
@@ -252,33 +252,33 @@
 	});
 
 	addEventListener(BLOCK_STORAGE_START_EVENT, () => {
-		if (!window._singleFile_localStorage) {
-			window._singleFile_localStorage = window.localStorage;
-			window.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
+		if (!globalThis._singleFile_localStorage) {
+			globalThis._singleFile_localStorage = globalThis.localStorage;
+			globalThis.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
 		}
-		if (!window._singleFile_indexedDB) {
-			window._singleFile_indexedDB = window.indexedDB;
-			window.__defineGetter__("indexedDB", () => { throw new Error("indexedDB temporary blocked by SingleFile"); });
+		if (!globalThis._singleFile_indexedDB) {
+			globalThis._singleFile_indexedDB = globalThis.indexedDB;
+			globalThis.__defineGetter__("indexedDB", () => { throw new Error("indexedDB temporary blocked by SingleFile"); });
 		}
 	});
 
 	addEventListener(BLOCK_STORAGE_END_EVENT, () => {
-		if (window._singleFile_localStorage) {
-			delete window.localStorage;
-			window.localStorage = window._singleFile_localStorage;
-			delete window._singleFile_localStorage;
+		if (globalThis._singleFile_localStorage) {
+			delete globalThis.localStorage;
+			globalThis.localStorage = globalThis._singleFile_localStorage;
+			delete globalThis._singleFile_localStorage;
 		}
-		if (!window._singleFile_indexedDB) {
-			delete window.indexedDB;
-			window.indexedDB = window._singleFile_indexedDB;
-			delete window._singleFile_indexedDB;
+		if (!globalThis._singleFile_indexedDB) {
+			delete globalThis.indexedDB;
+			globalThis.indexedDB = globalThis._singleFile_indexedDB;
+			delete globalThis._singleFile_indexedDB;
 		}
 	});
 
-	if (window.FontFace) {
-		const FontFace = window.FontFace;
+	if (globalThis.FontFace) {
+		const FontFace = globalThis.FontFace;
 		let warningFontFaceDisplayed;
-		window.FontFace = function () {
+		globalThis.FontFace = function () {
 			if (!warningFontFaceDisplayed) {
 				warn("SingleFile is hooking the FontFace constructor to get font URLs."); // eslint-disable-line no-console
 				warningFontFaceDisplayed = true;
@@ -306,13 +306,13 @@
 			}
 			return new FontFace(...arguments);
 		};
-		window.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
+		globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
 	}
 
-	if (window.IntersectionObserver) {
-		const IntersectionObserver = window.IntersectionObserver;
+	if (globalThis.IntersectionObserver) {
+		const IntersectionObserver = globalThis.IntersectionObserver;
 		let warningIntersectionObserverDisplayed;
-		window.IntersectionObserver = function () {
+		globalThis.IntersectionObserver = function () {
 			if (!warningIntersectionObserverDisplayed) {
 				warn("SingleFile is hooking the IntersectionObserver API to detect and load deferred images."); // eslint-disable-line no-console
 				warningIntersectionObserverDisplayed = true;
@@ -351,8 +351,8 @@
 			observers.set(intersectionObserver, { callback, options });
 			return intersectionObserver;
 		};
-		window.IntersectionObserver.prototype = IntersectionObserver.prototype;
-		window.IntersectionObserver.toString = function () { return "function IntersectionObserver() { [native code] }"; };
+		globalThis.IntersectionObserver.prototype = IntersectionObserver.prototype;
+		globalThis.IntersectionObserver.toString = function () { return "function IntersectionObserver() { [native code] }"; };
 	}
 
 	function dispatchResizeEvent() {
@@ -363,4 +363,4 @@
 		}
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 16 - 16
lib/single-file/processors/hooks/content/content-hooks-frames.js

@@ -21,9 +21,9 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.processors.hooks.content.frames = this.singlefile.lib.processors.hooks.content.frames || (() => {
+this.singlefile.lib.processors.hooks.content.frames = this.singlefile.lib.processors.hooks.content.frames || (globalThis => {
 
 	const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
 	const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
@@ -39,14 +39,14 @@ this.singlefile.lib.processors.hooks.content.frames = this.singlefile.lib.proces
 	const IMAGE_LOADED_EVENT = "single-file-image-loaded";
 	const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
 
-	const browser = this.browser;
-	const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-	const dispatchEvent = event => window.dispatchEvent(event);
-	const CustomEvent = window.CustomEvent;
-	const document = window.document;
-	const HTMLDocument = window.HTMLDocument;
-	const FileReader = window.FileReader;
-	const Blob = window.Blob;
+	const browser = globalThis.browser;
+	const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
+	const dispatchEvent = event => globalThis.dispatchEvent(event);
+	const CustomEvent = globalThis.CustomEvent;
+	const document = globalThis.document;
+	const HTMLDocument = globalThis.HTMLDocument;
+	const FileReader = globalThis.FileReader;
+	const Blob = globalThis.Blob;
 
 	const fontFaces = [];
 
@@ -120,7 +120,7 @@ this.singlefile.lib.processors.hooks.content.frames = this.singlefile.lib.proces
 	}
 
 	function injectedScript() {
-		const console = window.console;
+		const console = globalThis.console;
 		const warn = (console && console.warn && ((...args) => console.warn(...args))) || (() => { });
 		const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
 		const FONT_STYLE_PROPERTIES = {
@@ -133,10 +133,10 @@ this.singlefile.lib.processors.hooks.content.frames = this.singlefile.lib.proces
 			featureSettings: "font-feature-settings"
 		};
 
-		if (window.FontFace) {
-			const FontFace = window.FontFace;
+		if (globalThis.FontFace) {
+			const FontFace = globalThis.FontFace;
 			let warningFontFaceDisplayed;
-			window.FontFace = function () {
+			globalThis.FontFace = function () {
 				if (!warningFontFaceDisplayed) {
 					warn("SingleFile is hooking the FontFace constructor to get font URLs."); // eslint-disable-line no-console
 					warningFontFaceDisplayed = true;
@@ -164,8 +164,8 @@ this.singlefile.lib.processors.hooks.content.frames = this.singlefile.lib.proces
 				}
 				return new FontFace(...arguments);
 			};
-			window.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
+			globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
 		}
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 7 - 7
lib/single-file/processors/hooks/content/content-hooks-web.js

@@ -21,17 +21,17 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-(() => {
+(globalThis => {
 
 	const FETCH_REQUEST_EVENT = "single-file-request-fetch";
 	const FETCH_RESPONSE_EVENT = "single-file-response-fetch";
 
-	const CustomEvent = window.CustomEvent;
-	const fetch = window.fetch;
-	const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-	const dispatchEvent = event => window.dispatchEvent(event);
+	const CustomEvent = globalThis.CustomEvent;
+	const fetch = globalThis.fetch;
+	const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
+	const dispatchEvent = event => globalThis.dispatchEvent(event);
 
 	addEventListener(FETCH_REQUEST_EVENT, async event => {
 		const url = event.detail;
@@ -45,4 +45,4 @@
 		dispatchEvent(new CustomEvent(FETCH_RESPONSE_EVENT, { detail }));
 	});
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 6 - 6
lib/single-file/processors/hooks/content/content-hooks.js

@@ -21,13 +21,13 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.processors.hooks.content.main = this.singlefile.lib.processors.hooks.content.main || (() => {
+this.singlefile.lib.processors.hooks.content.main = this.singlefile.lib.processors.hooks.content.main || (globalThis => {
 
-	const browser = this.browser;
-	const document = window.document;
-	const HTMLDocument = window.HTMLDocument;
+	const browser = globalThis.browser;
+	const document = globalThis.document;
+	const HTMLDocument = globalThis.HTMLDocument;
 
 	if (document instanceof HTMLDocument) {
 		const scriptElement = document.createElement("script");
@@ -41,4 +41,4 @@ this.singlefile.lib.processors.hooks.content.main = this.singlefile.lib.processo
 	}
 	return {};
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 12 - 12
lib/single-file/processors/lazy/content/content-lazy-loader.js

@@ -21,18 +21,18 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.processors.lazy.content.loader || (() => {
+this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.processors.lazy.content.loader || (globalThis => {
 
 	const ATTRIBUTES_MUTATION_TYPE = "attributes";
 
 	const singlefile = this.singlefile;
-	const browser = this.browser;
-	const document = window.document;
-	const MutationObserver = window.MutationObserver;
-	const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-	const removeEventListener = (type, listener, options) => window.removeEventListener(type, listener, options);
+	const browser = globalThis.browser;
+	const document = globalThis.document;
+	const MutationObserver = globalThis.MutationObserver;
+	const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
+	const removeEventListener = (type, listener, options) => globalThis.removeEventListener(type, listener, options);
 	const timeouts = new Map();
 
 	if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
@@ -57,7 +57,7 @@ this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.process
 			timeouts.clear();
 			const maxScrollY = Math.max(document.documentElement.scrollHeight - (document.documentElement.clientHeight * 1.5), 0);
 			const maxScrollX = Math.max(document.documentElement.scrollWidth - (document.documentElement.clientWidth * 1.5), 0);
-			if (window.scrollY <= maxScrollY && window.scrollX <= maxScrollX) {
+			if (globalThis.scrollY <= maxScrollY && globalThis.scrollX <= maxScrollX) {
 				return triggerLazyLoading(options);
 			}
 		}
@@ -180,10 +180,10 @@ this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.process
 		} else {
 			const timeoutId = timeouts.get(type);
 			if (timeoutId) {
-				window.clearTimeout(timeoutId);
+				globalThis.clearTimeout(timeoutId);
 			}
 			timeouts.set(type, callback);
-			window.setTimeout(callback, delay);
+			globalThis.setTimeout(callback, delay);
 		}
 	}
 
@@ -194,9 +194,9 @@ this.singlefile.lib.processors.lazy.content.loader = this.singlefile.lib.process
 			const previousTimeoutId = timeouts.get(type);
 			timeouts.delete(type);
 			if (previousTimeoutId) {
-				window.clearTimeout(previousTimeoutId);
+				globalThis.clearTimeout(previousTimeoutId);
 			}
 		}
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 6 - 6
lib/single-file/single-file-helper.js

@@ -21,9 +21,9 @@
  *   Source.
  */
 
-/* global window, CustomEvent */
+/* global window, globalThis, CustomEvent */
 
-this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
+this.singlefile.lib.helper = this.singlefile.lib.helper || (globalThis => {
 
 	const singlefile = this.singlefile;
 
@@ -60,8 +60,8 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 	const COMMENT_HEADER = "Page saved with SingleFile";
 	const COMMENT_HEADER_LEGACY = "Archive processed by SingleFile";
 	const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
-	const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-	const dispatchEvent = event => window.dispatchEvent(event);
+	const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
+	const dispatchEvent = event => globalThis.dispatchEvent(event);
 
 	addEventListener("single-file-user-script-init", () => singlefile.lib.helper.waitForUserScript = async eventPrefixName => {
 		const event = new CustomEvent(eventPrefixName + "-request", { cancelable: true });
@@ -332,7 +332,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 	}
 
 	function getShadowRoot(element) {
-		const chrome = window.chrome;
+		const chrome = globalThis.chrome;
 		if (element.openOrClosedShadowRoot) {
 			return element.openOrClosedShadowRoot;
 		} else if (chrome && chrome.dom && chrome.dom.openOrClosedShadowRoot) {
@@ -506,4 +506,4 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		return array.flat ? array.flat() : array.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);

+ 11 - 11
lib/single-file/single-file-util.js

@@ -21,9 +21,9 @@
  *   Source.
  */
 
-/* global window */
+/* global window, globalThis */
 
-this.singlefile.lib.util = this.singlefile.lib.util || (() => {
+this.singlefile.lib.util = this.singlefile.lib.util || (globalThis => {
 
 	const DEBUG = false;
 	const ONE_MB = 1024 * 1024;
@@ -31,14 +31,14 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 	const DEFAULT_REPLACED_CHARACTERS = ["~", "+", "\\\\", "?", "%", "*", ":", "|", "\"", "<", ">", "\x00-\x1f", "\x7F"];
 	const DEFAULT_REPLACEMENT_CHARACTER = "_";
 
-	const URL = window.URL;
-	const DOMParser = window.DOMParser;
-	const Blob = window.Blob;
-	const FileReader = window.FileReader;
-	const fetch = url => window.fetch(url);
-	const crypto = window.crypto;
-	const TextDecoder = window.TextDecoder;
-	const TextEncoder = window.TextEncoder;
+	const URL = globalThis.URL;
+	const DOMParser = globalThis.DOMParser;
+	const Blob = globalThis.Blob;
+	const FileReader = globalThis.FileReader;
+	const fetch = url => globalThis.fetch(url);
+	const crypto = globalThis.crypto;
+	const TextDecoder = globalThis.TextDecoder;
+	const TextEncoder = globalThis.TextEncoder;
 	const singlefile = this.singlefile;
 
 	return {
@@ -379,4 +379,4 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 		console.log("S-File <browser>", ...args); // eslint-disable-line no-console
 	}
 
-})();
+})(typeof globalThis == "object" ? globalThis : window);