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

attach handlers to document

Gildas 2 éve
szülő
commit
9beb67f8b5

+ 3 - 4
src/lib/single-file/core/content/content-hooks-frames-inline-injection.js

@@ -42,7 +42,6 @@ function injectedScript() {
 		window.globalThis = window;
 	}
 	const document = globalThis.document;
-	const dispatchEvent = event => globalThis.dispatchEvent(event);
 	const CustomEvent = globalThis.CustomEvent;
 	const FileReader = globalThis.FileReader;
 	const Blob = globalThis.Blob;
@@ -62,20 +61,20 @@ function injectedScript() {
 	if (globalThis.FontFace) {
 		const FontFace = globalThis.FontFace;
 		globalThis.FontFace = function () {
-			getDetailObject(...arguments).then(detail => dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail })));
+			getDetailObject(...arguments).then(detail => document.dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail })));
 			return new FontFace(...arguments);
 		};
 		globalThis.FontFace.prototype = FontFace.prototype;
 		globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
 		const deleteFont = document.fonts.delete;
 		document.fonts.delete = function (fontFace) {
-			getDetailObject(fontFace.family).then(detail => dispatchEvent(new CustomEvent(DELETE_FONT_EVENT, { detail })));
+			getDetailObject(fontFace.family).then(detail => document.dispatchEvent(new CustomEvent(DELETE_FONT_EVENT, { detail })));
 			return deleteFont.call(document.fonts, fontFace);
 		};
 		document.fonts.delete.toString = function () { return "function delete() { [native code] }"; };
 		const clearFonts = document.fonts.clear;
 		document.fonts.clear = function () {
-			dispatchEvent(new CustomEvent(CLEAR_FONTS_EVENT));
+			document.dispatchEvent(new CustomEvent(CLEAR_FONTS_EVENT));
 			return clearFonts.call(document.fonts);
 		};
 		document.fonts.clear.toString = function () { return "function clear() { [native code] }"; };

+ 6 - 10
src/lib/single-file/fetch/content/content-fetch.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global browser, window, CustomEvent, setTimeout, clearTimeout */
+/* global browser, window, document, CustomEvent, setTimeout, clearTimeout */
 
 const FETCH_REQUEST_EVENT = "single-file-request-fetch";
 const FETCH_ACK_EVENT = "single-file-ack-fetch";
@@ -30,10 +30,6 @@ const ERR_HOST_FETCH = "Host fetch error (SingleFile)";
 const HOST_FETCH_MAX_DELAY = 2500;
 const USE_HOST_FETCH = Boolean(window.wrappedJSObject);
 
-const addEventListener = (type, listener, options) => window.addEventListener(type, listener, options);
-const dispatchEvent = event => window.dispatchEvent(event);
-const removeEventListener = (type, listener, options) => window.removeEventListener(type, listener, options);
-
 const fetch = (url, options) => window.fetch(url, options);
 
 let requestId = 0, pendingResponses = new Map();
@@ -95,9 +91,9 @@ async function onFetchResponse(message) {
 
 async function hostFetch(url, options) {
 	const result = new Promise((resolve, reject) => {
-		dispatchEvent(new CustomEvent(FETCH_REQUEST_EVENT, { detail: JSON.stringify({ url, options }) }));
-		addEventListener(FETCH_ACK_EVENT, onAckFetch, false);
-		addEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
+		document.dispatchEvent(new CustomEvent(FETCH_REQUEST_EVENT, { detail: JSON.stringify({ url, options }) }));
+		document.addEventListener(FETCH_ACK_EVENT, onAckFetch, false);
+		document.addEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
 		const timeout = setTimeout(() => {
 			removeListeners();
 			reject(new Error(ERR_HOST_FETCH));
@@ -127,8 +123,8 @@ async function hostFetch(url, options) {
 		}
 
 		function removeListeners() {
-			removeEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
-			removeEventListener(FETCH_ACK_EVENT, onAckFetch, false);
+			document.removeEventListener(FETCH_RESPONSE_EVENT, onResponseFetch, false);
+			document.removeEventListener(FETCH_ACK_EVENT, onAckFetch, false);
 		}
 	});
 	try {