| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- /*
- * Copyright 2018 Gildas Lormeau
- * contact : gildas.lormeau <at> gmail.com
- *
- * This file is part of SingleFile.
- *
- * SingleFile is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * SingleFile is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
- */
- /* global browser, SingleFile, singlefile, frameTree, document, Blob, MouseEvent, getSelection, prompt, addEventListener, Node, window, timeout, MutationObserver */
- this.singlefile.top = this.singlefile.top || (() => {
- const MESSAGE_PREFIX = "__SingleFile__::";
- const TIMEOUT_LAZY_LOADING = 500;
- const SingleFileClass = SingleFile.getClass();
- let processing = false;
- browser.runtime.onMessage.addListener(message => {
- if (message.savePage) {
- savePage(message);
- }
- });
- addEventListener("message", event => {
- if (typeof event.data == "string" && event.data.startsWith(MESSAGE_PREFIX)) {
- const message = JSON.parse(event.data.substring(MESSAGE_PREFIX.length));
- if (message.savePage) {
- savePage(message);
- }
- }
- });
- return true;
- async function savePage(message) {
- const options = message.options;
- if (!processing && !options.frameId) {
- let selectionFound;
- if (options.selected) {
- selectionFound = await markSelection();
- }
- if (!options.selected || selectionFound) {
- processing = true;
- try {
- const page = await processPage(options);
- await downloadPage(page, options);
- } catch (error) {
- console.error(error); // eslint-disable-line no-console
- browser.runtime.sendMessage({ processError: true, error, options: { autoSave: false } });
- }
- } else {
- browser.runtime.sendMessage({ processCancelled: true, options: { autoSave: false } });
- }
- processing = false;
- }
- }
- async function markSelection() {
- let selectionFound = markSelectedContent();
- if (selectionFound) {
- return selectionFound;
- } else {
- const selectedArea = await singlefile.ui.getSelectedArea();
- if (selectedArea) {
- markSelectedArea(selectedArea);
- selectionFound = true;
- }
- return selectionFound;
- }
- }
- async function processPage(options) {
- singlefile.ui.init();
- const processor = new SingleFileClass(options);
- options.insertSingleFileComment = true;
- options.insertFaviconLink = true;
- if (!options.removeFrames && this.frameTree) {
- options.framesData = await frameTree.getAsync(options);
- }
- options.doc = document;
- options.win = window;
- let index = 0, maxIndex = 0;
- options.onprogress = event => {
- if (event.type == event.RESOURCES_INITIALIZED) {
- maxIndex = event.details.max;
- }
- if (event.type == event.RESOURCES_INITIALIZED || event.type == event.RESOURCE_LOADED) {
- if (event.type == event.RESOURCE_LOADED) {
- index++;
- }
- browser.runtime.sendMessage({ processProgress: true, index, maxIndex, options: { autoSave: false } });
- if (options.shadowEnabled) {
- singlefile.ui.onprogress(index, maxIndex);
- }
- } else if (event.type == event.PAGE_ENDED) {
- browser.runtime.sendMessage({ processEnd: true, options: { autoSave: false } });
- }
- };
- if (options.lazyLoadImages) {
- await lazyLoadResources();
- }
- await processor.initialize();
- await processor.preparePageData();
- const page = await processor.getPageData();
- if (options.selected) {
- unmarkSelectedContent();
- }
- page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
- if (options.shadowEnabled) {
- singlefile.ui.end();
- }
- if (options.displayStats) {
- console.log("SingleFile stats"); // eslint-disable-line no-console
- console.table(page.stats); // eslint-disable-line no-console
- }
- return page;
- }
- async function lazyLoadResources() {
- const scriptURL = browser.runtime.getURL("lib/single-file/lazy-loader-before.js");
- const scriptBeforeElement = document.createElement("script");
- scriptBeforeElement.src = scriptURL;
- document.body.appendChild(scriptBeforeElement);
- let timeoutId;
- const promise = new Promise(resolve => {
- scriptBeforeElement.onload = () => scriptBeforeElement.remove();
- var observer = new MutationObserver(mutationsList => {
- mutationsList.forEach(mutation => {
- if (mutation.attributeName == "src" || mutation.attributeName == "srcset") {
- timeoutId = deferLazyLoadEnd(timeoutId, resolve);
- }
- });
- });
- observer.observe(document, { attributes: true, childList: true, subtree: true });
- timeoutId = deferLazyLoadEnd(resolve);
- });
- return promise;
- }
- function deferLazyLoadEnd(timeoutId, resolve) {
- timeout.clear(timeoutId);
- return timeout.set(() => {
- resolve();
- const scriptURL = browser.runtime.getURL("lib/single-file/lazy-loader-after.js");
- const scriptAfterElement = document.createElement("script");
- scriptAfterElement.src = scriptURL;
- document.body.appendChild(scriptAfterElement);
- scriptAfterElement.onload = () => scriptAfterElement.remove();
- }, TIMEOUT_LAZY_LOADING);
- }
- function markSelectedContent() {
- const selection = getSelection();
- const range = selection.rangeCount ? selection.getRangeAt(0) : null;
- let selectionFound = false;
- if (range && range.commonAncestorContainer) {
- const treeWalker = document.createTreeWalker(range.commonAncestorContainer);
- const ancestorElement = range.commonAncestorContainer != Node.ELEMENT_NODE ? range.commonAncestorContainer.parentElement : range.commonAncestorContainer;
- while (treeWalker.nextNode() && treeWalker.currentNode != range.endContainer) {
- if (treeWalker.currentNode == range.startContainer) {
- selectionFound = true;
- }
- if (selectionFound) {
- const element = treeWalker.currentNode.nodeType == Node.ELEMENT_NODE ? treeWalker.currentNode : treeWalker.currentNode.parentElement;
- element.setAttribute(SingleFileClass.SELECTED_CONTENT_ATTRIBUTE_NAME, "");
- }
- }
- if (selectionFound) {
- ancestorElement.setAttribute(SingleFileClass.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME, "");
- }
- }
- return selectionFound;
- }
- function markSelectedArea(selectedAreaElement) {
- selectedAreaElement.setAttribute(SingleFileClass.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME, "");
- selectedAreaElement.querySelectorAll("*").forEach(element => element.setAttribute(SingleFileClass.SELECTED_CONTENT_ATTRIBUTE_NAME, ""));
- }
- function unmarkSelectedContent() {
- document.querySelectorAll("[" + SingleFileClass.SELECTED_CONTENT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SingleFileClass.SELECTED_CONTENT_ATTRIBUTE_NAME));
- document.querySelectorAll("[" + SingleFileClass.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME + "]").forEach(selectedContent => selectedContent.removeAttribute(SingleFileClass.SELECTED_CONTENT_ROOT_ATTRIBUTE_NAME));
- }
- async function downloadPage(page, options) {
- if (options.backgroundSave) {
- const response = await browser.runtime.sendMessage({ download: true, url: page.url, confirmFilename: options.confirmFilename, filename: page.filename });
- if (response.notSupported) {
- const response = await browser.runtime.sendMessage({ download: true, content: page.content, confirmFilename: options.confirmFilename, filename: page.filename });
- if (response.notSupported) {
- downloadPageFallback(page, options);
- }
- } else {
- URL.revokeObjectURL(page.url);
- }
- } else {
- downloadPageFallback(page, options);
- }
- }
- function downloadPageFallback(page, options) {
- if (options.confirmFilename) {
- page.filename = prompt("File name", page.filename);
- }
- if (page.filename && page.filename.length) {
- const link = document.createElement("a");
- document.body.appendChild(link);
- link.download = page.filename;
- link.href = page.url;
- link.dispatchEvent(new MouseEvent("click"));
- link.remove();
- URL.revokeObjectURL(page.url);
- }
- }
- })();
|