Jelajahi Sumber

retrieve canvas data and empty stylesheet rules when saving a page before unload

Gildas 7 tahun lalu
induk
melakukan
cdd2dba846

+ 6 - 4
extension/core/bg/bg.js

@@ -55,7 +55,7 @@ singlefile.core = (() => {
 			}
 		}
 		if (request.processContent) {
-			processBackgroundTab(sender.tab.id, request.content, request.url);
+			processBackgroundTab(sender.tab.id, request);
 		}
 	});
 
@@ -77,10 +77,12 @@ singlefile.core = (() => {
 		}
 	};
 
-	async function processBackgroundTab(tabId, content, url) {
+	async function processBackgroundTab(tabId, message) {
 		const options = await singlefile.config.get();
-		options.content = content;
-		options.url = url;
+		options.content = message.content;
+		options.url = message.url;
+		options.canvasData = message.canvasData;
+		options.emptyStyleRulesText = message.emptyStyleRulesText;
 		options.insertSingleFileComment = true;
 		options.insertFaviconLink = true;
 		options.removeFrames = true;

+ 9 - 8
extension/core/content/content-autosave-unload.js

@@ -18,15 +18,16 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, addEventListener, document, location, docHelper */
+/* global browser, window, addEventListener, document, location, docHelper */
 
-this.singlefile.autoSave = this.singlefile.autoSave || (() => {
+this.singlefile.autoSave = this.singlefile.autoSave || (async () => {
 
-	browser.runtime.sendMessage({ isAutoSaveUnloadEnabled: true }).then(isAutoSaveUnloadEnabled => {
-		if (isAutoSaveUnloadEnabled) {
-			addEventListener("unload", () => browser.runtime.sendMessage({ processContent: true, content: docHelper.serialize(document), url: location.href }));
-		}
-	});
-	return true;
+	const [isAutoSaveUnloadEnabled, options] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveUnloadEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
+	if (isAutoSaveUnloadEnabled) {
+		addEventListener("unload", () => {
+			const docData = docHelper.preProcessDoc(document, window, options);
+			browser.runtime.sendMessage({ processContent: true, content: docHelper.serialize(document), canvasData: docData.canvasData, emptyStyleRulesText: docData.emptyStyleRulesText, url: location.href });
+		});
+	}
 
 })();