Ver código fonte

fixed "remove hidden elements" feature

Gildas 7 anos atrás
pai
commit
0a7f96e8bc

+ 26 - 4
extension/core/content/content.js

@@ -18,13 +18,14 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global SingleFile, singlefile, FrameTree, document, Blob, MouseEvent, getSelection */
+/* global SingleFile, singlefile, FrameTree, document, Blob, MouseEvent, getSelection, getComputedStyle */
 
 (() => {
 
 	const browser = this.browser || this.chrome;
 
 	const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
+	const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
 	const PROGRESS_LOADED_COEFFICIENT = 2;
 
 	let processing = false;
@@ -39,20 +40,28 @@
 					if (!options.removeFrames) {
 						hideHeadFrames();
 					}
+					if (options.removeHiddenElements) {
+						selectRemovedElements();
+					}
 					return SingleFile.initialize(options);
 				})
 				.then(process => {
-					if (message.options.shadowEnabled) {
+					const options = message.options;
+					if (options.removeHiddenElements) {
+						unselectRemovedElements();
+					}
+					if (options.shadowEnabled) {
 						singlefile.ui.init();
 					}
 					return process();
 				})
 				.then(page => {
+					const options = message.options;
 					const date = new Date();
-					page.filename = page.title + (message.options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
+					page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
 					page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
 					downloadPage(page);
-					if (message.options.shadowEnabled) {
+					if (options.shadowEnabled) {
 						singlefile.ui.end();
 					}
 					processing = false;
@@ -73,6 +82,19 @@
 		document.head.querySelectorAll("iframe, frame, object[type=\"text/html\"][data]").forEach(element => element.hidden = true);
 	}
 
+	function selectRemovedElements() {
+		document.querySelectorAll("html > body *:not(style):not(script):not(link)").forEach(element => {
+			const style = getComputedStyle(element);
+			if (element.hidden || style.display == "none" || ((style.opacity === 0 || style.visibility == "hidden") && !element.clientWidth && !element.clientHeight)) {
+				element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
+			}
+		});
+	}
+
+	function unselectRemovedElements() {
+		document.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
+	}
+
 	async function getOptions(options) {
 		options.url = document.location.href;
 		if (options.selected) {

+ 2 - 2
extension/ui/pages/options.html

@@ -51,8 +51,8 @@
 				<input type="checkbox" id="removeScriptsInput">
 			</div>
 			<div class="option">
-				<label for="removeHiddenInput">remove hidden elements</label>
-				<input type="checkbox" id="removeHiddenInput">
+				<label for="removeHiddenElementsInput">remove hidden elements</label>
+				<input type="checkbox" id="removeHiddenElementsInput">
 			</div>
 			<div class="option">
 				<label for="removeUnusedCSSRulesInput">remove unused CSS rules</label>

+ 1 - 2
lib/single-file/single-file-browser.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global SingleFileCore, base64, DOMParser, getComputedStyle, TextDecoder, fetch, superFetch, parseSrcset, uglifycss */
+/* global SingleFileCore, base64, DOMParser, TextDecoder, fetch, superFetch, parseSrcset, uglifycss */
 
 this.SingleFile = (() => {
 
@@ -91,7 +91,6 @@ this.SingleFile = (() => {
 			}
 			return {
 				DOMParser,
-				getComputedStyle,
 				document: doc,
 				serialize: () => getDoctype(doc) + doc.documentElement.outerHTML,
 				parseSrcset: srcset => parseSrcset(srcset),

+ 2 - 9
lib/single-file/single-file-core.js

@@ -21,6 +21,7 @@
 const SingleFileCore = (() => {
 
 	const SELECTED_CONTENT_ATTRIBUTE_NAME = "data-single-file-selected-content";
+	const REMOVED_CONTENT_ATTRIBUTE_NAME = "data-single-file-removed-content";
 
 	let Download, DOM, URL;
 
@@ -226,7 +227,6 @@ const SingleFileCore = (() => {
 			}
 			this.dom = DOM.create(pageContent, this.baseURI);
 			this.DOMParser = this.dom.DOMParser;
-			this.getComputedStyle = this.dom.getComputedStyle;
 			this.doc = this.dom.document;
 			if (!pageContent && this.doc.querySelector("meta[name=fragment][content=\"!\"]") && !this.baseURI.endsWith("?" + ESCAPED_FRAGMENT) && !this.baseURI.endsWith("&" + ESCAPED_FRAGMENT)) {
 				await DOMProcessor.loadEscapedFragmentPage();
@@ -404,14 +404,7 @@ const SingleFileCore = (() => {
 		}
 
 		removeHiddenElements() {
-			this.doc.querySelectorAll("html > body *:not(style):not(script):not(link)").forEach(element => {
-				if (this.getComputedStyle) {
-					const style = this.getComputedStyle(element);
-					if (element.hidden || style.visibility == "hidden" || style.display == "none" || style.opacity == 0) {
-						element.remove();
-					}
-				}
-			});
+			this.doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.remove());
 		}
 
 		compressHTML() {