فهرست منبع

avoid native APIs being overloaded when running in puppeter

Former-commit-id: 3e29e43a01e1777af791409f5dc93eeda17d3146
Gildas 6 سال پیش
والد
کامیت
8df38d4029

+ 8 - 1
lib/fetch/content/content-fetch-resources.js

@@ -21,13 +21,20 @@
  *   Source.
  */
 
-/* global browser, fetch, CustomEvent, dispatchEvent, addEventListener, removeEventListener */
+/* global window */
 
 this.singlefile.lib.fetch.content.resources = this.singlefile.lib.fetch.content.resources || (() => {
 
 	const FETCH_REQUEST_EVENT = "single-file-request-fetch";
 	const FETCH_RESPONSE_EVENT = "single-file-response-fetch";
 
+	const addEventListener = window.addEventListener;
+	const fetch = window.fetch;
+	const CustomEvent = window.CustomEvent;
+	const dispatchEvent = window.dispatchEvent;
+	const browser = window.browser;
+	const removeEventListener = window.removeEventListener;
+
 	return {
 		fetch: async url => {
 			try {

+ 12 - 5
lib/frame-tree/content/content-frame-tree.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global window, top, document, addEventListener, MessageChannel, browser, setTimeout */
+/* global window */
 
 this.singlefile.lib.frameTree.content.frames = this.singlefile.lib.frameTree.content.frames || (() => {
 
@@ -37,14 +37,21 @@ this.singlefile.lib.frameTree.content.frames = this.singlefile.lib.frameTree.con
 	const TIMEOUT_INIT_REQUEST_MESSAGE = 750;
 	const TOP_WINDOW_ID = "0";
 	const WINDOW_ID_SEPARATOR = ".";
-	const TOP_WINDOW = window == top;
+	const TOP_WINDOW = window == window.top;
+
+	const addEventListener = window.addEventListener;
+	const top = window.top;
+	const MessageChannel = window.MessageChannel;
+	const document = window.document;
+	const browser = window.browser;
+	const setTimeout = window.setTimeout;
 
 	const sessions = new Map();
 	let windowId;
 
 	if (TOP_WINDOW) {
 		windowId = TOP_WINDOW_ID;
-		if (this.browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
+		if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
 			browser.runtime.onMessage.addListener(message => {
 				if (message.method == INIT_RESPONSE_MESSAGE) {
 					initResponse(message);
@@ -66,7 +73,7 @@ this.singlefile.lib.frameTree.content.frames = this.singlefile.lib.frameTree.con
 				initRequest(message);
 			} else if (message.method == CLEANUP_REQUEST_MESSAGE) {
 				cleanupRequest(message);
-			} else if ((!this.browser || !browser.runtime) && message.method == INIT_RESPONSE_MESSAGE) {
+			} else if ((!browser || !browser.runtime) && message.method == INIT_RESPONSE_MESSAGE) {
 				const port = event.ports[0];
 				port.onmessage = event => initResponse(event.data);
 			}
@@ -244,7 +251,7 @@ this.singlefile.lib.frameTree.content.frames = this.singlefile.lib.frameTree.con
 	}
 
 	function sendMessage(targetWindow, message, useChannel) {
-		if (targetWindow == top && this.browser && browser.runtime && browser.runtime.sendMessage) {
+		if (targetWindow == top && browser && browser.runtime && browser.runtime.sendMessage) {
 			browser.runtime.sendMessage(message);
 		} else {
 			if (useChannel) {

+ 11 - 1
lib/hooks/content/content-hooks-frames-web.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global window, addEventListener, dispatchEvent, CustomEvent, document, screen, Element, UIEvent, FileReader, Blob */
+/* global window */
 
 (() => {
 
@@ -44,8 +44,18 @@
 		featureSettings: "font-feature-settings"
 	};
 
+	const addEventListener = window.addEventListener;
+	const dispatchEvent = window.dispatchEvent;
+	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 requestAnimationFrame = window.requestAnimationFrame;
 	const cancelAnimationFrame = window.cancelAnimationFrame;
+
 	const observers = new Map();
 	const observedElements = new Map();
 	let loadDeferredImages;

+ 10 - 2
lib/hooks/content/content-hooks-frames.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global browser, addEventListener, dispatchEvent, CustomEvent, document, HTMLDocument */
+/* global window */
 
 this.singlefile.lib.hooks.content.frames = this.singlefile.lib.hooks.content.frames || (() => {
 
@@ -34,11 +34,19 @@ this.singlefile.lib.hooks.content.frames = this.singlefile.lib.hooks.content.fra
 	const LOAD_IMAGE_EVENT = "single-file-load-image";
 	const IMAGE_LOADED_EVENT = "single-file-image-loaded";
 	const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
+
+	const browser = window.browser;
+	const addEventListener = window.addEventListener;
+	const dispatchEvent = window.dispatchEvent;
+	const CustomEvent = window.CustomEvent;
+	const document = window.document;
+	const HTMLDocument = window.HTMLDocument;
+
 	const fontFaces = [];
 
 	if (document instanceof HTMLDocument) {
 		let scriptElement = document.createElement("script");
-		if (this.browser && browser.runtime && browser.runtime.getURL) {
+		if (browser && browser.runtime && browser.runtime.getURL) {
 			scriptElement.src = browser.runtime.getURL("/lib/hooks/content/content-hooks-frames-web.js");
 			scriptElement.async = false;
 		} else if (this.singlefile.lib.getFileContent) {

+ 7 - 1
lib/hooks/content/content-hooks-web.js

@@ -21,14 +21,20 @@
  *   Source.
  */
 
-/* global history, dispatchEvent, CustomEvent, fetch, addEventListener */
+/* global window */
 
 (() => {
 
 	const FETCH_REQUEST_EVENT = "single-file-request-fetch";
 	const FETCH_RESPONSE_EVENT = "single-file-response-fetch";
 
+	const history = window.history;
+	const dispatchEvent = window.dispatchEvent;
+	const CustomEvent = window.CustomEvent;
+	const fetch = window.fetch;
+	const addEventListener = window.addEventListener;
 	const pushState = history.pushState;
+
 	let warningDisplayed;
 	history.pushState = function (state, title, url) {
 		if (!warningDisplayed) {

+ 6 - 2
lib/hooks/content/content-hooks.js

@@ -21,14 +21,18 @@
  *   Source.
  */
 
-/* global browser, document, HTMLDocument */
+/* global window */
 
 this.singlefile.lib.hooks.content.main = this.singlefile.lib.hooks.content.main || (() => {
 
+	const document = window.document;
+	const browser = window.browser;
+	const HTMLDocument = window.HTMLDocument;
+
 	if (document instanceof HTMLDocument) {
 		const scriptElement = document.createElement("script");
 		scriptElement.async = false;
-		if (this.browser && browser.runtime && browser.runtime.getURL) {
+		if (browser && browser.runtime && browser.runtime.getURL) {
 			scriptElement.src = browser.runtime.getURL("/lib/hooks/content/content-hooks-web.js");
 			scriptElement.async = false;
 		} else if (this.singlefile.lib.getFileContent) {

+ 10 - 3
lib/lazy/content/content-lazy-loader.js

@@ -21,11 +21,18 @@
  *   Source.
  */
 
-/* global browser, document, MutationObserver, setTimeout, clearTimeout, addEventListener, removeEventListener, scrollY, scrollX */
+/* global window, scrollY, scrollX */
 
 this.singlefile.lib.lazy.content.loader = this.singlefile.lib.lazy.content.loader || (() => {
 
 	const singlefile = this.singlefile;
+	const browser = window.browser;
+	const document = window.document;
+	const MutationObserver = window.MutationObserver;
+	const setTimeout = window.setTimeout;
+	const clearTimeout = window.clearTimeout;
+	const addEventListener = window.addEventListener;
+	const removeEventListener = window.removeEventListener;
 
 	const ATTRIBUTES_MUTATION_TYPE = "attributes";
 	const SINGLE_FILE_UI_ELEMENT_CLASS = "single-file-ui-element";
@@ -129,7 +136,7 @@ this.singlefile.lib.lazy.content.loader = this.singlefile.lib.lazy.content.loade
 	}
 
 	async function setAsyncTimeout(callback, delay) {
-		if (this.browser && browser.runtime && browser.runtime.sendMessage) {
+		if (browser && browser.runtime && browser.runtime.sendMessage) {
 			const timeoutId = await browser.runtime.sendMessage({ method: "lazyTimeout.setTimeout", delay });
 			const timeoutCallback = message => {
 				if (message.method == "content.onLazyTimeout" && message.id == timeoutId) {
@@ -146,7 +153,7 @@ this.singlefile.lib.lazy.content.loader = this.singlefile.lib.lazy.content.loade
 	}
 
 	async function clearAsyncTimeout(timeoutId) {
-		if (this.browser && browser.runtime && browser.runtime.sendMessage) {
+		if (browser && browser.runtime && browser.runtime.sendMessage) {
 			await browser.runtime.sendMessage({ method: "lazyTimeout.clearTimeout", id: timeoutId });
 		} else {
 			return clearTimeout(timeoutId);

+ 6 - 1
lib/single-file/single-file-util.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global DOMParser, URL, Blob, FileReader */
+/* global window */
 
 this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 
@@ -29,6 +29,11 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 	const ONE_MB = 1024 * 1024;
 	const PREFIX_CONTENT_TYPE_TEXT = "text/";
 
+	const URL = window.URL;
+	const DOMParser = window.DOMParser;
+	const Blob = window.Blob;
+	const FileReader = window.FileReader;
+
 	return {
 		getInstance: (modules, util) => {
 			if (modules.serializer === undefined) {

+ 7 - 7
lib/single-file/single-file.js

@@ -21,17 +21,17 @@
  *   Source.
  */
 
-/* global	
-	crypto,
-	fetch, 
-	Blob, 
-	FileReader, 
-	TextDecoder,
-	TextEncoder */
+/* global window */
 
 this.singlefile.lib.SingleFile = this.singlefile.lib.SingleFile || (() => {
 
 	const singlefile = this.singlefile;
+	const crypto = window.crypto;
+	const fetch = window.fetch;
+	const Blob = window.Blob;
+	const FileReader = window.FileReader;
+	const TextDecoder = window.TextDecoder;
+	const TextEncoder = window.TextEncoder;
 
 	const modules = {
 		helper: singlefile.lib.helper,