single-file-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,404 @@
1
+ /*
2
+ * Copyright 2010-2020 Gildas Lormeau
3
+ * contact : gildas.lormeau <at> gmail.com
4
+ *
5
+ * This file is part of SingleFile.
6
+ *
7
+ * The code in this file is free software: you can redistribute it and/or
8
+ * modify it under the terms of the GNU Affero General Public License
9
+ * (GNU AGPL) as published by the Free Software Foundation, either version 3
10
+ * of the License, or (at your option) any later version.
11
+ *
12
+ * The code in this file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
15
+ * General Public License for more details.
16
+ *
17
+ * As additional permission under GNU AGPL version 3 section 7, you may
18
+ * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
19
+ * AGPL normally required by section 4, provided you include this license
20
+ * notice and a URL through which recipients can access the Corresponding
21
+ * Source.
22
+ */
23
+
24
+ /* global window, globalThis */
25
+
26
+ (globalThis => {
27
+
28
+ const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
29
+ const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
30
+ const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT = "single-file-load-deferred-images-keep-zoom-level-start";
31
+ const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT = "single-file-load-deferred-images-keep-zoom-level-end";
32
+ const LOAD_DEFERRED_IMAGES_RESET_ZOOM_LEVEL_EVENT = "single-file-load-deferred-images-keep-zoom-level-reset";
33
+ const LOAD_DEFERRED_IMAGES_RESET_EVENT = "single-file-load-deferred-images-reset";
34
+ const BLOCK_COOKIES_START_EVENT = "single-file-block-cookies-start";
35
+ const BLOCK_COOKIES_END_EVENT = "single-file-block-cookies-end";
36
+ const BLOCK_STORAGE_START_EVENT = "single-file-block-storage-start";
37
+ const BLOCK_STORAGE_END_EVENT = "single-file-block-storage-end";
38
+ const DISPATCH_SCROLL_START_EVENT = "single-file-dispatch-scroll-event-start";
39
+ const DISPATCH_SCROLL_END_EVENT = "single-file-dispatch-scroll-event-end";
40
+ const LAZY_LOAD_ATTRIBUTE = "single-file-lazy-load";
41
+ const LOAD_IMAGE_EVENT = "single-file-load-image";
42
+ const IMAGE_LOADED_EVENT = "single-file-image-loaded";
43
+ const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
44
+ const DELETE_FONT_EVENT = "single-file-delete-font";
45
+ const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
46
+ const FONT_STYLE_PROPERTIES = {
47
+ family: "font-family",
48
+ style: "font-style",
49
+ weight: "font-weight",
50
+ stretch: "font-stretch",
51
+ unicodeRange: "unicode-range",
52
+ variant: "font-variant",
53
+ featureSettings: "font-feature-settings"
54
+ };
55
+
56
+ const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
57
+ const dispatchEvent = event => { try { globalThis.dispatchEvent(event); } catch (error) { /* ignored */ } };
58
+ const CustomEvent = globalThis.CustomEvent;
59
+ const document = globalThis.document;
60
+ const screen = globalThis.screen;
61
+ const Element = globalThis.Element;
62
+ const UIEvent = globalThis.UIEvent;
63
+ const FileReader = globalThis.FileReader;
64
+ const Blob = globalThis.Blob;
65
+ const console = globalThis.console;
66
+ const warn = (console && console.warn && ((...args) => console.warn(...args))) || (() => { });
67
+
68
+ const observers = new Map();
69
+ const observedElements = new Map();
70
+
71
+ let dispatchScrollEvent;
72
+ addEventListener(LOAD_DEFERRED_IMAGES_START_EVENT, () => loadDeferredImagesStart());
73
+ addEventListener(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT, () => loadDeferredImagesStart(true));
74
+
75
+ function loadDeferredImagesStart(keepZoomLevel) {
76
+ const scrollingElement = document.scrollingElement || document.documentElement;
77
+ const clientHeight = scrollingElement.clientHeight;
78
+ const clientWidth = scrollingElement.clientWidth;
79
+ const scrollHeight = Math.max(scrollingElement.scrollHeight - clientHeight, clientHeight);
80
+ const scrollWidth = Math.max(scrollingElement.scrollWidth - clientWidth, clientWidth);
81
+ document.querySelectorAll("[loading=lazy]").forEach(element => {
82
+ element.loading = "eager";
83
+ element.setAttribute(LAZY_LOAD_ATTRIBUTE, "");
84
+ });
85
+ scrollingElement.__defineGetter__("clientHeight", () => scrollHeight);
86
+ scrollingElement.__defineGetter__("clientWidth", () => scrollWidth);
87
+ screen.__defineGetter__("height", () => scrollHeight);
88
+ screen.__defineGetter__("width", () => scrollWidth);
89
+ globalThis._singleFile_innerHeight = globalThis.innerHeight;
90
+ globalThis._singleFile_innerWidth = globalThis.innerWidth;
91
+ globalThis.__defineGetter__("innerHeight", () => scrollHeight);
92
+ globalThis.__defineGetter__("innerWidth", () => scrollWidth);
93
+ if (!keepZoomLevel) {
94
+ if (!globalThis._singleFile_getBoundingClientRect) {
95
+ globalThis._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
96
+ Element.prototype.getBoundingClientRect = function () {
97
+ const boundingRect = globalThis._singleFile_getBoundingClientRect.call(this);
98
+ if (this == scrollingElement) {
99
+ boundingRect.__defineGetter__("height", () => scrollHeight);
100
+ boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
101
+ boundingRect.__defineGetter__("width", () => scrollWidth);
102
+ boundingRect.__defineGetter__("right", () => scrollWidth + boundingRect.left);
103
+ }
104
+ return boundingRect;
105
+ };
106
+ }
107
+ }
108
+ if (!globalThis._singleFileImage) {
109
+ const Image = globalThis.Image;
110
+ globalThis._singleFileImage = globalThis.Image;
111
+ globalThis.__defineGetter__("Image", function () {
112
+ return function () {
113
+ const image = new Image(...arguments);
114
+ const result = new Image(...arguments);
115
+ result.__defineSetter__("src", function (value) {
116
+ image.src = value;
117
+ dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT, { detail: image.src }));
118
+ });
119
+ result.__defineGetter__("src", function () {
120
+ return image.src;
121
+ });
122
+ result.__defineSetter__("srcset", function (value) {
123
+ dispatchEvent(new CustomEvent(LOAD_IMAGE_EVENT));
124
+ image.srcset = value;
125
+ });
126
+ result.__defineGetter__("srcset", function () {
127
+ return image.srcset;
128
+ });
129
+ image.onload = image.onloadend = image.onerror = event => {
130
+ dispatchEvent(new CustomEvent(IMAGE_LOADED_EVENT, { detail: image.src }));
131
+ result.dispatchEvent(new UIEvent(event.type, event));
132
+ };
133
+ if (image.decode) {
134
+ result.decode = () => image.decode();
135
+ }
136
+ return result;
137
+ };
138
+ });
139
+ }
140
+ let zoomFactorX, zoomFactorY;
141
+ if (keepZoomLevel) {
142
+ zoomFactorX = clientHeight / scrollHeight;
143
+ zoomFactorY = clientWidth / scrollWidth;
144
+ } else {
145
+ zoomFactorX = (clientHeight + globalThis.scrollY) / scrollHeight;
146
+ zoomFactorY = (clientWidth + globalThis.scrollX) / scrollWidth;
147
+ }
148
+ const zoomFactor = Math.min(zoomFactorX, zoomFactorY);
149
+ if (zoomFactor < 1) {
150
+ const transform = document.documentElement.style.getPropertyValue("transform");
151
+ const transformPriority = document.documentElement.style.getPropertyPriority("transform");
152
+ const transformOrigin = document.documentElement.style.getPropertyValue("transform-origin");
153
+ const transformOriginPriority = document.documentElement.style.getPropertyPriority("transform-origin");
154
+ const minHeight = document.documentElement.style.getPropertyValue("min-height");
155
+ const minHeightPriority = document.documentElement.style.getPropertyPriority("min-height");
156
+ document.documentElement.style.setProperty("transform-origin", (zoomFactorX < 1 ? "50%" : "0") + " " + (zoomFactorY < 1 ? "50%" : "0") + " 0", "important");
157
+ document.documentElement.style.setProperty("transform", "scale3d(" + zoomFactor + ", " + zoomFactor + ", 1)", "important");
158
+ document.documentElement.style.setProperty("min-height", (100 / zoomFactor) + "vh", "important");
159
+ dispatchResizeEvent();
160
+ if (keepZoomLevel) {
161
+ document.documentElement.style.setProperty("-sf-transform", transform, transformPriority);
162
+ document.documentElement.style.setProperty("-sf-transform-origin", transformOrigin, transformOriginPriority);
163
+ document.documentElement.style.setProperty("-sf-min-height", minHeight, minHeightPriority);
164
+ } else {
165
+ document.documentElement.style.setProperty("transform", transform, transformPriority);
166
+ document.documentElement.style.setProperty("transform-origin", transformOrigin, transformOriginPriority);
167
+ document.documentElement.style.setProperty("min-height", minHeight, minHeightPriority);
168
+ }
169
+ }
170
+ if (!keepZoomLevel) {
171
+ dispatchResizeEvent();
172
+ const docBoundingRect = scrollingElement.getBoundingClientRect();
173
+ if (window == window.top) {
174
+ [...observers].forEach(([intersectionObserver, observer]) => {
175
+ const getBoundingClientRectDefined = observer.options && observer.options.root && observer.options.root.getBoundingClientRect;
176
+ const rootBoundingRect = getBoundingClientRectDefined && observer.options.root.getBoundingClientRect();
177
+ const targetElements = observedElements.get(intersectionObserver);
178
+ if (targetElements) {
179
+ const params = targetElements.map(target => {
180
+ const boundingClientRect = target.getBoundingClientRect();
181
+ const isIntersecting = true;
182
+ const intersectionRatio = 1;
183
+ const rootBounds = getBoundingClientRectDefined ? rootBoundingRect : docBoundingRect;
184
+ const time = 0;
185
+ return { target, intersectionRatio, boundingClientRect, intersectionRect: boundingClientRect, isIntersecting, rootBounds, time };
186
+ });
187
+ observer.callback(params, intersectionObserver);
188
+ }
189
+ });
190
+ }
191
+ }
192
+ }
193
+
194
+ addEventListener(LOAD_DEFERRED_IMAGES_END_EVENT, () => loadDeferredImagesEnd());
195
+ addEventListener(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT, () => loadDeferredImagesEnd(true));
196
+ addEventListener(LOAD_DEFERRED_IMAGES_RESET_EVENT, resetScreenSize);
197
+ addEventListener(LOAD_DEFERRED_IMAGES_RESET_ZOOM_LEVEL_EVENT, () => {
198
+ const transform = document.documentElement.style.getPropertyValue("-sf-transform");
199
+ const transformPriority = document.documentElement.style.getPropertyPriority("-sf-transform");
200
+ const transformOrigin = document.documentElement.style.getPropertyValue("-sf-transform-origin");
201
+ const transformOriginPriority = document.documentElement.style.getPropertyPriority("-sf-transform-origin");
202
+ const minHeight = document.documentElement.style.getPropertyValue("-sf-min-height");
203
+ const minHeightPriority = document.documentElement.style.getPropertyPriority("-sf-min-height");
204
+ document.documentElement.style.setProperty("transform", transform, transformPriority);
205
+ document.documentElement.style.setProperty("transform-origin", transformOrigin, transformOriginPriority);
206
+ document.documentElement.style.setProperty("min-height", minHeight, minHeightPriority);
207
+ document.documentElement.style.removeProperty("-sf-transform");
208
+ document.documentElement.style.removeProperty("-sf-transform-origin");
209
+ document.documentElement.style.removeProperty("-sf-min-height");
210
+ resetScreenSize();
211
+ });
212
+
213
+ function loadDeferredImagesEnd(keepZoomLevel) {
214
+ document.querySelectorAll("[" + LAZY_LOAD_ATTRIBUTE + "]").forEach(element => {
215
+ element.loading = "lazy";
216
+ element.removeAttribute(LAZY_LOAD_ATTRIBUTE);
217
+ });
218
+ if (!keepZoomLevel) {
219
+ if (globalThis._singleFile_getBoundingClientRect) {
220
+ Element.prototype.getBoundingClientRect = globalThis._singleFile_getBoundingClientRect;
221
+ delete globalThis._singleFile_getBoundingClientRect;
222
+ }
223
+ }
224
+ if (globalThis._singleFileImage) {
225
+ delete globalThis.Image;
226
+ globalThis.Image = globalThis._singleFileImage;
227
+ delete globalThis._singleFileImage;
228
+ }
229
+ if (!keepZoomLevel) {
230
+ dispatchResizeEvent();
231
+ }
232
+ }
233
+
234
+ function resetScreenSize() {
235
+ const scrollingElement = document.scrollingElement || document.documentElement;
236
+ if (globalThis._singleFile_innerHeight != null) {
237
+ delete globalThis.innerHeight;
238
+ globalThis.innerHeight = globalThis._singleFile_innerHeight;
239
+ delete globalThis._singleFile_innerHeight;
240
+ }
241
+ if (globalThis._singleFile_innerWidth != null) {
242
+ delete globalThis.innerWidth;
243
+ globalThis.innerWidth = globalThis._singleFile_innerWidth;
244
+ delete globalThis._singleFile_innerWidth;
245
+ }
246
+ delete scrollingElement.clientHeight;
247
+ delete scrollingElement.clientWidth;
248
+ delete screen.height;
249
+ delete screen.width;
250
+ }
251
+
252
+ addEventListener(DISPATCH_SCROLL_START_EVENT, () => {
253
+ dispatchScrollEvent = true;
254
+ });
255
+
256
+ addEventListener(DISPATCH_SCROLL_END_EVENT, () => {
257
+ dispatchScrollEvent = false;
258
+ });
259
+
260
+ addEventListener(BLOCK_COOKIES_START_EVENT, () => {
261
+ try {
262
+ document.__defineGetter__("cookie", () => { throw new Error("document.cookie temporary blocked by SingleFile"); });
263
+ } catch (error) {
264
+ // ignored
265
+ }
266
+ });
267
+
268
+ addEventListener(BLOCK_COOKIES_END_EVENT, () => {
269
+ delete document.cookie;
270
+ });
271
+
272
+ addEventListener(BLOCK_STORAGE_START_EVENT, () => {
273
+ if (!globalThis._singleFile_localStorage) {
274
+ globalThis._singleFile_localStorage = globalThis.localStorage;
275
+ globalThis.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
276
+ }
277
+ if (!globalThis._singleFile_indexedDB) {
278
+ globalThis._singleFile_indexedDB = globalThis.indexedDB;
279
+ globalThis.__defineGetter__("indexedDB", () => { throw new Error("indexedDB temporary blocked by SingleFile"); });
280
+ }
281
+ });
282
+
283
+ addEventListener(BLOCK_STORAGE_END_EVENT, () => {
284
+ if (globalThis._singleFile_localStorage) {
285
+ delete globalThis.localStorage;
286
+ globalThis.localStorage = globalThis._singleFile_localStorage;
287
+ delete globalThis._singleFile_localStorage;
288
+ }
289
+ if (!globalThis._singleFile_indexedDB) {
290
+ delete globalThis.indexedDB;
291
+ globalThis.indexedDB = globalThis._singleFile_indexedDB;
292
+ delete globalThis._singleFile_indexedDB;
293
+ }
294
+ });
295
+
296
+ if (globalThis.FontFace) {
297
+ const FontFace = globalThis.FontFace;
298
+ let warningFontFaceDisplayed;
299
+ globalThis.FontFace = function () {
300
+ if (!warningFontFaceDisplayed) {
301
+ warn("SingleFile is hooking the FontFace constructor, document.fonts.delete and document.fonts.clear to handle dynamically loaded fonts.");
302
+ warningFontFaceDisplayed = true;
303
+ }
304
+ getDetailObject(...arguments).then(detail => dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail })));
305
+ return new FontFace(...arguments);
306
+ };
307
+ globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
308
+ const deleteFont = document.fonts.delete;
309
+ document.fonts.delete = function (fontFace) {
310
+ getDetailObject(fontFace.family).then(detail => dispatchEvent(new CustomEvent(DELETE_FONT_EVENT, { detail })));
311
+ return deleteFont.call(document.fonts, fontFace);
312
+ };
313
+ document.fonts.delete.toString = function () { return "function delete() { [native code] }"; };
314
+ const clearFonts = document.fonts.clear;
315
+ document.fonts.clear = function () {
316
+ dispatchEvent(new CustomEvent(CLEAR_FONTS_EVENT));
317
+ return clearFonts.call(document.fonts);
318
+ };
319
+ document.fonts.clear.toString = function () { return "function clear() { [native code] }"; };
320
+ }
321
+
322
+ if (globalThis.IntersectionObserver) {
323
+ const IntersectionObserver = globalThis.IntersectionObserver;
324
+ let warningIntersectionObserverDisplayed;
325
+ globalThis.IntersectionObserver = function () {
326
+ if (!warningIntersectionObserverDisplayed) {
327
+ warn("SingleFile is hooking the IntersectionObserver API to detect and load deferred images.");
328
+ warningIntersectionObserverDisplayed = true;
329
+ }
330
+ const intersectionObserver = new IntersectionObserver(...arguments);
331
+ const observeIntersection = IntersectionObserver.prototype.observe || intersectionObserver.observe;
332
+ const unobserveIntersection = IntersectionObserver.prototype.unobserve || intersectionObserver.unobserve;
333
+ const callback = arguments[0];
334
+ const options = arguments[1];
335
+ if (observeIntersection) {
336
+ intersectionObserver.observe = function (targetElement) {
337
+ let targetElements = observedElements.get(intersectionObserver);
338
+ if (!targetElements) {
339
+ targetElements = [];
340
+ observedElements.set(intersectionObserver, targetElements);
341
+ }
342
+ targetElements.push(targetElement);
343
+ return observeIntersection.call(intersectionObserver, targetElement);
344
+ };
345
+ }
346
+ if (unobserveIntersection) {
347
+ intersectionObserver.unobserve = function (targetElement) {
348
+ let targetElements = observedElements.get(intersectionObserver);
349
+ if (targetElements) {
350
+ targetElements = targetElements.filter(element => element != targetElement);
351
+ if (targetElements.length) {
352
+ observedElements.set(intersectionObserver, targetElements);
353
+ } else {
354
+ observedElements.delete(intersectionObserver);
355
+ observers.delete(intersectionObserver);
356
+ }
357
+ }
358
+ return unobserveIntersection.call(intersectionObserver, targetElement);
359
+ };
360
+ }
361
+ observers.set(intersectionObserver, { callback, options });
362
+ return intersectionObserver;
363
+ };
364
+ globalThis.IntersectionObserver.prototype = IntersectionObserver.prototype;
365
+ globalThis.IntersectionObserver.toString = function () { return "function IntersectionObserver() { [native code] }"; };
366
+ }
367
+
368
+ async function getDetailObject(fontFamily, src, descriptors) {
369
+ const detail = {};
370
+ detail["font-family"] = fontFamily;
371
+ detail.src = src;
372
+ if (descriptors) {
373
+ Object.keys(descriptors).forEach(descriptor => {
374
+ if (FONT_STYLE_PROPERTIES[descriptor]) {
375
+ detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor];
376
+ }
377
+ });
378
+ }
379
+ return new Promise(resolve => {
380
+ if (detail.src instanceof ArrayBuffer) {
381
+ const reader = new FileReader();
382
+ reader.readAsDataURL(new Blob([detail.src]));
383
+ reader.addEventListener("load", () => {
384
+ detail.src = "url(" + reader.result + ")";
385
+ resolve(detail);
386
+ });
387
+ } else {
388
+ resolve(detail);
389
+ }
390
+ });
391
+ }
392
+
393
+ function dispatchResizeEvent() {
394
+ try {
395
+ dispatchEvent(new UIEvent("resize"));
396
+ if (dispatchScrollEvent) {
397
+ dispatchEvent(new UIEvent("scroll"));
398
+ }
399
+ } catch (error) {
400
+ // ignored
401
+ }
402
+ }
403
+
404
+ })(typeof globalThis == "object" ? globalThis : window);
@@ -0,0 +1,214 @@
1
+ /*
2
+ * Copyright 2010-2020 Gildas Lormeau
3
+ * contact : gildas.lormeau <at> gmail.com
4
+ *
5
+ * This file is part of SingleFile.
6
+ *
7
+ * The code in this file is free software: you can redistribute it and/or
8
+ * modify it under the terms of the GNU Affero General Public License
9
+ * (GNU AGPL) as published by the Free Software Foundation, either version 3
10
+ * of the License, or (at your option) any later version.
11
+ *
12
+ * The code in this file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
15
+ * General Public License for more details.
16
+ *
17
+ * As additional permission under GNU AGPL version 3 section 7, you may
18
+ * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
19
+ * AGPL normally required by section 4, provided you include this license
20
+ * notice and a URL through which recipients can access the Corresponding
21
+ * Source.
22
+ */
23
+
24
+ /* global globalThis, window */
25
+
26
+ const LOAD_DEFERRED_IMAGES_START_EVENT = "single-file-load-deferred-images-start";
27
+ const LOAD_DEFERRED_IMAGES_END_EVENT = "single-file-load-deferred-images-end";
28
+ const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT = "single-file-load-deferred-images-keep-zoom-level-start";
29
+ const LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT = "single-file-load-deferred-images-keep-zoom-level-end";
30
+ const LOAD_DEFERRED_IMAGES_RESET_ZOOM_LEVEL_EVENT = "single-file-load-deferred-images-keep-zoom-level-reset";
31
+ const LOAD_DEFERRED_IMAGES_RESET_EVENT = "single-file-load-deferred-images-reset";
32
+ const BLOCK_COOKIES_START_EVENT = "single-file-block-cookies-start";
33
+ const BLOCK_COOKIES_END_EVENT = "single-file-block-cookies-end";
34
+ const DISPATCH_SCROLL_START_EVENT = "single-file-dispatch-scroll-event-start";
35
+ const DISPATCH_SCROLL_END_EVENT = "single-file-dispatch-scroll-event-end";
36
+ const BLOCK_STORAGE_START_EVENT = "single-file-block-storage-start";
37
+ const BLOCK_STORAGE_END_EVENT = "single-file-block-storage-end";
38
+ const LOAD_IMAGE_EVENT = "single-file-load-image";
39
+ const IMAGE_LOADED_EVENT = "single-file-image-loaded";
40
+ const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
41
+ const DELETE_FONT_EVENT = "single-file-delete-font";
42
+ const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
43
+
44
+ const browser = globalThis.browser;
45
+ const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
46
+ const dispatchEvent = event => { try { globalThis.dispatchEvent(event); } catch (error) { /* ignored */ } };
47
+ const CustomEvent = globalThis.CustomEvent;
48
+ const document = globalThis.document;
49
+ const Document = globalThis.Document;
50
+
51
+ let fontFaces;
52
+ if (window._singleFile_fontFaces) {
53
+ fontFaces = window._singleFile_fontFaces;
54
+ } else {
55
+ fontFaces = window._singleFile_fontFaces = new Map();
56
+ }
57
+
58
+ if (document instanceof Document) {
59
+ if (browser && browser.runtime && browser.runtime.getURL) {
60
+ addEventListener(NEW_FONT_FACE_EVENT, event => {
61
+ const detail = event.detail;
62
+ const key = Object.assign({}, detail);
63
+ delete key.src;
64
+ fontFaces.set(JSON.stringify(key), detail);
65
+ });
66
+ addEventListener(DELETE_FONT_EVENT, event => {
67
+ const detail = event.detail;
68
+ const key = Object.assign({}, detail);
69
+ delete key.src;
70
+ fontFaces.delete(JSON.stringify(key));
71
+ });
72
+ addEventListener(CLEAR_FONTS_EVENT, () => fontFaces = new Map());
73
+ let scriptElement = document.createElement("script");
74
+ scriptElement.src = "data:," + "(" + injectedScript.toString() + ")()";
75
+ (document.documentElement || document).appendChild(scriptElement);
76
+ scriptElement.remove();
77
+ scriptElement = document.createElement("script");
78
+ scriptElement.src = browser.runtime.getURL("/lib/web/hooks/hooks-frames-web.js");
79
+ scriptElement.async = false;
80
+ (document.documentElement || document).appendChild(scriptElement);
81
+ scriptElement.remove();
82
+ }
83
+ }
84
+
85
+ export {
86
+ getFontsData,
87
+ loadDeferredImagesStart,
88
+ loadDeferredImagesEnd,
89
+ loadDeferredImagesResetZoomLevel,
90
+ LOAD_IMAGE_EVENT,
91
+ IMAGE_LOADED_EVENT
92
+ };
93
+
94
+ function getFontsData() {
95
+ return Array.from(fontFaces.values());
96
+ }
97
+
98
+ function loadDeferredImagesStart(options) {
99
+ if (options.loadDeferredImagesBlockCookies) {
100
+ dispatchEvent(new CustomEvent(BLOCK_COOKIES_START_EVENT));
101
+ }
102
+ if (options.loadDeferredImagesBlockStorage) {
103
+ dispatchEvent(new CustomEvent(BLOCK_STORAGE_START_EVENT));
104
+ }
105
+ if (options.loadDeferredImagesDispatchScrollEvent) {
106
+ dispatchEvent(new CustomEvent(DISPATCH_SCROLL_START_EVENT));
107
+ }
108
+ if (options.loadDeferredImagesKeepZoomLevel) {
109
+ dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_START_EVENT));
110
+ } else {
111
+ dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_START_EVENT));
112
+ }
113
+ }
114
+
115
+ function loadDeferredImagesEnd(options) {
116
+ if (options.loadDeferredImagesBlockCookies) {
117
+ dispatchEvent(new CustomEvent(BLOCK_COOKIES_END_EVENT));
118
+ }
119
+ if (options.loadDeferredImagesBlockStorage) {
120
+ dispatchEvent(new CustomEvent(BLOCK_STORAGE_END_EVENT));
121
+ }
122
+ if (options.loadDeferredImagesDispatchScrollEvent) {
123
+ dispatchEvent(new CustomEvent(DISPATCH_SCROLL_END_EVENT));
124
+ }
125
+ if (options.loadDeferredImagesKeepZoomLevel) {
126
+ dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_KEEP_ZOOM_LEVEL_END_EVENT));
127
+ } else {
128
+ dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_END_EVENT));
129
+ }
130
+ }
131
+
132
+ function loadDeferredImagesResetZoomLevel(options) {
133
+ if (options.loadDeferredImagesKeepZoomLevel) {
134
+ dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_RESET_ZOOM_LEVEL_EVENT));
135
+ } else {
136
+ dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_RESET_EVENT));
137
+ }
138
+ }
139
+
140
+ function injectedScript() {
141
+ if (typeof globalThis == "undefined") {
142
+ window.globalThis = window;
143
+ }
144
+ const document = globalThis.document;
145
+ const console = globalThis.console;
146
+ const dispatchEvent = event => globalThis.dispatchEvent(event);
147
+ const CustomEvent = globalThis.CustomEvent;
148
+ const FileReader = globalThis.FileReader;
149
+ const Blob = globalThis.Blob;
150
+ const warn = (console && console.warn && ((...args) => console.warn(...args))) || (() => { });
151
+ const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
152
+ const DELETE_FONT_EVENT = "single-file-delete-font";
153
+ const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
154
+ const FONT_STYLE_PROPERTIES = {
155
+ family: "font-family",
156
+ style: "font-style",
157
+ weight: "font-weight",
158
+ stretch: "font-stretch",
159
+ unicodeRange: "unicode-range",
160
+ variant: "font-variant",
161
+ featureSettings: "font-feature-settings"
162
+ };
163
+
164
+ if (globalThis.FontFace) {
165
+ const FontFace = globalThis.FontFace;
166
+ let warningFontFaceDisplayed;
167
+ globalThis.FontFace = function () {
168
+ if (!warningFontFaceDisplayed) {
169
+ warn("SingleFile is hooking the FontFace constructor, document.fonts.delete and document.fonts.clear to handle dynamically loaded fonts.");
170
+ warningFontFaceDisplayed = true;
171
+ }
172
+ getDetailObject(...arguments).then(detail => dispatchEvent(new CustomEvent(NEW_FONT_FACE_EVENT, { detail })));
173
+ return new FontFace(...arguments);
174
+ };
175
+ globalThis.FontFace.toString = function () { return "function FontFace() { [native code] }"; };
176
+ const deleteFont = document.fonts.delete;
177
+ document.fonts.delete = function (fontFace) {
178
+ getDetailObject(fontFace.family).then(detail => dispatchEvent(new CustomEvent(DELETE_FONT_EVENT, { detail })));
179
+ return deleteFont.call(document.fonts, fontFace);
180
+ };
181
+ document.fonts.delete.toString = function () { return "function delete() { [native code] }"; };
182
+ const clearFonts = document.fonts.clear;
183
+ document.fonts.clear = function () {
184
+ dispatchEvent(new CustomEvent(CLEAR_FONTS_EVENT));
185
+ return clearFonts.call(document.fonts);
186
+ };
187
+ document.fonts.clear.toString = function () { return "function clear() { [native code] }"; };
188
+ }
189
+
190
+ async function getDetailObject(fontFamily, src, descriptors) {
191
+ const detail = {};
192
+ detail["font-family"] = fontFamily;
193
+ detail.src = src;
194
+ if (descriptors) {
195
+ Object.keys(descriptors).forEach(descriptor => {
196
+ if (FONT_STYLE_PROPERTIES[descriptor]) {
197
+ detail[FONT_STYLE_PROPERTIES[descriptor]] = descriptors[descriptor];
198
+ }
199
+ });
200
+ }
201
+ return new Promise(resolve => {
202
+ if (detail.src instanceof ArrayBuffer) {
203
+ const reader = new FileReader();
204
+ reader.readAsDataURL(new Blob([detail.src]));
205
+ reader.addEventListener("load", () => {
206
+ detail.src = "url(" + reader.result + ")";
207
+ resolve(detail);
208
+ });
209
+ } else {
210
+ resolve(detail);
211
+ }
212
+ });
213
+ }
214
+ }
@@ -0,0 +1,48 @@
1
+ /*
2
+ * Copyright 2010-2020 Gildas Lormeau
3
+ * contact : gildas.lormeau <at> gmail.com
4
+ *
5
+ * This file is part of SingleFile.
6
+ *
7
+ * The code in this file is free software: you can redistribute it and/or
8
+ * modify it under the terms of the GNU Affero General Public License
9
+ * (GNU AGPL) as published by the Free Software Foundation, either version 3
10
+ * of the License, or (at your option) any later version.
11
+ *
12
+ * The code in this file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
15
+ * General Public License for more details.
16
+ *
17
+ * As additional permission under GNU AGPL version 3 section 7, you may
18
+ * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
19
+ * AGPL normally required by section 4, provided you include this license
20
+ * notice and a URL through which recipients can access the Corresponding
21
+ * Source.
22
+ */
23
+
24
+ /* global window, globalThis */
25
+
26
+ (globalThis => {
27
+
28
+ const FETCH_REQUEST_EVENT = "single-file-request-fetch";
29
+ const FETCH_RESPONSE_EVENT = "single-file-response-fetch";
30
+
31
+ const CustomEvent = globalThis.CustomEvent;
32
+ const fetch = globalThis.fetch;
33
+ const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
34
+ const dispatchEvent = event => { try { globalThis.dispatchEvent(event); } catch (error) { /* ignored */ } };
35
+
36
+ addEventListener(FETCH_REQUEST_EVENT, async event => {
37
+ const url = event.detail;
38
+ let detail;
39
+ try {
40
+ const response = await fetch(url, { cache: "force-cache" });
41
+ detail = { url, response: await response.arrayBuffer(), headers: [...response.headers], status: response.status };
42
+ } catch (error) {
43
+ detail = { url, error: error && error.toString() };
44
+ }
45
+ dispatchEvent(new CustomEvent(FETCH_RESPONSE_EVENT, { detail }));
46
+ });
47
+
48
+ })(typeof globalThis == "object" ? globalThis : window);