single-file-core 1.0.14 → 1.0.17
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.
- package/package.json +1 -1
- package/processors/hooks/content/content-hooks-frames.js +0 -88
- package/processors/index.js +0 -2
- package/single-file-core.js +33 -32
- package/{processors/hooks/content/content-hooks.js → single-file-frames.js} +1 -16
- package/{processors/hooks/content/content-hooks-web.js → single-file-hooks-frames.js} +1 -25
- package/single-file-infobar.js +24 -0
- /package/{index.js → single-file.js} +0 -0
package/package.json
CHANGED
|
@@ -41,7 +41,6 @@ const NEW_FONT_FACE_EVENT = "single-file-new-font-face";
|
|
|
41
41
|
const DELETE_FONT_EVENT = "single-file-delete-font";
|
|
42
42
|
const CLEAR_FONTS_EVENT = "single-file-clear-fonts";
|
|
43
43
|
|
|
44
|
-
const browser = globalThis.browser;
|
|
45
44
|
const addEventListener = (type, listener, options) => globalThis.addEventListener(type, listener, options);
|
|
46
45
|
const dispatchEvent = event => { try { globalThis.dispatchEvent(event); } catch (error) { /* ignored */ } };
|
|
47
46
|
const CustomEvent = globalThis.CustomEvent;
|
|
@@ -69,17 +68,6 @@ if (document instanceof Document) {
|
|
|
69
68
|
fontFaces.delete(JSON.stringify(key));
|
|
70
69
|
});
|
|
71
70
|
addEventListener(CLEAR_FONTS_EVENT, () => fontFaces = new Map());
|
|
72
|
-
let scriptElement = document.createElement("script");
|
|
73
|
-
scriptElement.src = "data:," + "(" + injectedScript.toString() + ")()";
|
|
74
|
-
(document.documentElement || document).appendChild(scriptElement);
|
|
75
|
-
scriptElement.remove();
|
|
76
|
-
if (browser && browser.runtime && browser.runtime.getURL) {
|
|
77
|
-
scriptElement = document.createElement("script");
|
|
78
|
-
scriptElement.src = browser.runtime.getURL("/lib/single-file-hooks-frames.js");
|
|
79
|
-
scriptElement.async = false;
|
|
80
|
-
(document.documentElement || document).appendChild(scriptElement);
|
|
81
|
-
scriptElement.remove();
|
|
82
|
-
}
|
|
83
71
|
}
|
|
84
72
|
|
|
85
73
|
export {
|
|
@@ -135,80 +123,4 @@ function loadDeferredImagesResetZoomLevel(options) {
|
|
|
135
123
|
} else {
|
|
136
124
|
dispatchEvent(new CustomEvent(LOAD_DEFERRED_IMAGES_RESET_EVENT));
|
|
137
125
|
}
|
|
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
126
|
}
|
package/processors/index.js
CHANGED
|
@@ -22,13 +22,11 @@
|
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
24
|
import * as frameTree from "./frame-tree/content/content-frame-tree.js";
|
|
25
|
-
import * as hooks from "./hooks/content/content-hooks.js";
|
|
26
25
|
import * as hooksFrames from "./hooks/content/content-hooks-frames.js";
|
|
27
26
|
import * as lazy from "./lazy/content/content-lazy-loader.js";
|
|
28
27
|
|
|
29
28
|
export {
|
|
30
29
|
frameTree,
|
|
31
|
-
hooks,
|
|
32
30
|
hooksFrames,
|
|
33
31
|
lazy
|
|
34
32
|
};
|
package/single-file-core.js
CHANGED
|
@@ -533,7 +533,7 @@ class Processor {
|
|
|
533
533
|
if (!this.options.backgroundSave) {
|
|
534
534
|
filename = filename.replace(/\//g, replacementCharacter);
|
|
535
535
|
}
|
|
536
|
-
if (!this.options.saveToGDrive && !this.options.saveToGitHub && !this.options.saveWithCompanion &&
|
|
536
|
+
if (!this.options.saveToGDrive && !this.options.saveToGitHub && !this.options.saveWithCompanion && !this.options.saveWithWebDAV &&
|
|
537
537
|
((this.options.filenameMaxLengthUnit == "bytes" && util.getContentSize(filename) > this.options.filenameMaxLength) || (filename.length > this.options.filenameMaxLength))) {
|
|
538
538
|
const extensionMatch = filename.match(/(\.[^.]{3,4})$/);
|
|
539
539
|
const extension = extensionMatch && extensionMatch[0] && extensionMatch[0].length > 1 ? extensionMatch[0] : "";
|
|
@@ -1016,10 +1016,14 @@ class Processor {
|
|
|
1016
1016
|
|
|
1017
1017
|
resolveStyleAttributeURLs() {
|
|
1018
1018
|
this.doc.querySelectorAll("[style]").forEach(element => {
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1019
|
+
if (this.options.blockStylesheets) {
|
|
1020
|
+
element.removeAttribute("style");
|
|
1021
|
+
} else {
|
|
1022
|
+
const styleContent = element.getAttribute("style");
|
|
1023
|
+
const declarationList = cssTree.parse(styleContent, { context: "declarationList", parseCustomProperty: true });
|
|
1024
|
+
ProcessorHelper.resolveStylesheetURLs(declarationList, this.baseURI, this.workStyleElement);
|
|
1025
|
+
this.styles.set(element, declarationList);
|
|
1026
|
+
}
|
|
1023
1027
|
});
|
|
1024
1028
|
}
|
|
1025
1029
|
|
|
@@ -1030,10 +1034,11 @@ class Processor {
|
|
|
1030
1034
|
if (element.media) {
|
|
1031
1035
|
mediaText = element.media.toLowerCase();
|
|
1032
1036
|
}
|
|
1033
|
-
const
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
+
const scoped = Boolean(element.closest("[" + SHADOWROOT_ATTRIBUTE_NAME + "]"));
|
|
1038
|
+
const stylesheetInfo = {
|
|
1039
|
+
mediaText,
|
|
1040
|
+
scoped
|
|
1041
|
+
};
|
|
1037
1042
|
if (element.tagName == "LINK" && element.charset) {
|
|
1038
1043
|
options.charset = element.charset;
|
|
1039
1044
|
}
|
|
@@ -1043,32 +1048,19 @@ class Processor {
|
|
|
1043
1048
|
const newResources = Object.keys(this.options.updatedResources).filter(url => this.options.updatedResources[url].type == "stylesheet" && !this.options.updatedResources[url].retrieved).map(url => this.options.updatedResources[url]);
|
|
1044
1049
|
await Promise.all(newResources.map(async resource => {
|
|
1045
1050
|
resource.retrieved = true;
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
+
if (!this.options.blockStylesheets) {
|
|
1052
|
+
const stylesheetInfo = {};
|
|
1053
|
+
const element = this.doc.createElement("style");
|
|
1054
|
+
this.doc.body.appendChild(element);
|
|
1055
|
+
element.textContent = resource.content;
|
|
1056
|
+
await processElement(element, stylesheetInfo, this.stylesheets, this.baseURI, this.options, this.workStyleElement);
|
|
1057
|
+
}
|
|
1051
1058
|
}));
|
|
1052
1059
|
}
|
|
1053
1060
|
|
|
1054
1061
|
async function processElement(element, stylesheetInfo, stylesheets, baseURI, options, workStyleElement) {
|
|
1055
1062
|
let stylesheet;
|
|
1056
1063
|
stylesheets.set(element, stylesheetInfo);
|
|
1057
|
-
if (!options.blockStylesheets) {
|
|
1058
|
-
stylesheet = await getStylesheet(element, baseURI, options, workStyleElement);
|
|
1059
|
-
}
|
|
1060
|
-
if (stylesheet && stylesheet.children) {
|
|
1061
|
-
if (options.compressCSS) {
|
|
1062
|
-
ProcessorHelper.removeSingleLineCssComments(stylesheet);
|
|
1063
|
-
}
|
|
1064
|
-
stylesheetInfo.stylesheet = stylesheet;
|
|
1065
|
-
} else {
|
|
1066
|
-
stylesheets.delete(element);
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
|
-
async function getStylesheet(element, baseURI, options, workStyleElement) {
|
|
1071
|
-
let stylesheet;
|
|
1072
1064
|
if (!options.blockStylesheets) {
|
|
1073
1065
|
if (element.tagName == "LINK") {
|
|
1074
1066
|
stylesheet = await ProcessorHelper.resolveLinkStylesheetURLs(element.href, baseURI, options, workStyleElement);
|
|
@@ -1080,7 +1072,14 @@ class Processor {
|
|
|
1080
1072
|
}
|
|
1081
1073
|
}
|
|
1082
1074
|
}
|
|
1083
|
-
|
|
1075
|
+
if (stylesheet && stylesheet.children) {
|
|
1076
|
+
if (options.compressCSS) {
|
|
1077
|
+
ProcessorHelper.removeSingleLineCssComments(stylesheet);
|
|
1078
|
+
}
|
|
1079
|
+
stylesheetInfo.stylesheet = stylesheet;
|
|
1080
|
+
} else {
|
|
1081
|
+
stylesheets.delete(element);
|
|
1082
|
+
}
|
|
1084
1083
|
}
|
|
1085
1084
|
}
|
|
1086
1085
|
|
|
@@ -1809,7 +1808,8 @@ class ProcessorHelper {
|
|
|
1809
1808
|
let { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL,
|
|
1810
1809
|
{ asBinary: true, expectedType: "image", groupDuplicates: options.groupDuplicateImages });
|
|
1811
1810
|
if (!originalResourceURL.startsWith("#")) {
|
|
1812
|
-
|
|
1811
|
+
const maxSizeDuplicateImages = options.maxSizeDuplicateImages || SINGLE_FILE_VARIABLE_MAX_SIZE;
|
|
1812
|
+
if (duplicate && options.groupDuplicateImages && util.getContentSize(content) < maxSizeDuplicateImages) {
|
|
1813
1813
|
const varNode = cssTree.parse("var(" + SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource + ")", { context: "value" });
|
|
1814
1814
|
for (let keyName of Object.keys(varNode.children.head.data)) {
|
|
1815
1815
|
urlNode[keyName] = varNode.children.head.data[keyName];
|
|
@@ -1882,7 +1882,8 @@ class ProcessorHelper {
|
|
|
1882
1882
|
const forbiddenPrefixFound = PREFIXES_FORBIDDEN_DATA_URI.filter(prefixDataURI => content.startsWith(prefixDataURI)).length;
|
|
1883
1883
|
if (!forbiddenPrefixFound) {
|
|
1884
1884
|
const isSVG = content.startsWith(PREFIX_DATA_URI_IMAGE_SVG);
|
|
1885
|
-
|
|
1885
|
+
const maxSizeDuplicateImages = options.maxSizeDuplicateImages || SINGLE_FILE_VARIABLE_MAX_SIZE;
|
|
1886
|
+
if (expectedType == "image" && processDuplicates && duplicate && !isSVG && util.getContentSize(content) < maxSizeDuplicateImages) {
|
|
1886
1887
|
if (ProcessorHelper.replaceImageSource(resourceElement, SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource, options)) {
|
|
1887
1888
|
cssVariables.set(indexResource, { content, url: originURL });
|
|
1888
1889
|
const declarationList = cssTree.parse(resourceElement.getAttribute("style"), { context: "declarationList", parseCustomProperty: true });
|
|
@@ -21,19 +21,4 @@
|
|
|
21
21
|
* Source.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const browser = globalThis.browser;
|
|
27
|
-
const document = globalThis.document;
|
|
28
|
-
const Document = globalThis.Document;
|
|
29
|
-
|
|
30
|
-
if (document instanceof Document) {
|
|
31
|
-
const scriptElement = document.createElement("script");
|
|
32
|
-
scriptElement.async = false;
|
|
33
|
-
if (browser && browser.runtime && browser.runtime.getURL) {
|
|
34
|
-
scriptElement.src = browser.runtime.getURL("/lib/single-file-hooks.js");
|
|
35
|
-
scriptElement.async = false;
|
|
36
|
-
}
|
|
37
|
-
(document.documentElement || document).appendChild(scriptElement);
|
|
38
|
-
scriptElement.remove();
|
|
39
|
-
}
|
|
24
|
+
export * from "./processors/frame-tree/content/content-frame-tree.js";
|
|
@@ -21,28 +21,4 @@
|
|
|
21
21
|
* Source.
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
|
|
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);
|
|
24
|
+
import "./processors/hooks/content/content-hooks-frames-web.js";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2010-2022 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
|
+
import "./common/content-infobar-web.js";
|
|
File without changes
|