From ba5cb7d5c604f38c656173a69d0acf3e0bf351c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= <adomas.ven@gmail.com>
Date: Wed, 20 Jul 2022 14:23:57 +0300
Subject: [PATCH] Fixes some assets not being saved with SingleFile snapshots

Part of zotero/zotero-connectors#394

When saving an item (e.g. on arxiv) with a site translator, the
translator may add a custom snapshot attachment URL which does not match
the translation URL (the URL in the browser where translation was
initiated) in which case Zotero handles snapshot saving. Zotero loads
the page in a background browser. By default images are not loaded in
this browser. SingleFile due to CORS restrictions has to refetch all
resources that are not from the same domain or subdomain, but uses
pre-loaded resources (images) from the same domain. Before this change
any images that were from the same domain as the saved website were not
saved in the snapshot. This commit fixes that.
---
 chrome/content/zotero/xpcom/attachments.js | 3 ++-
 chrome/content/zotero/xpcom/http.js        | 6 +++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js
index 0d456abf95..efb36742dc 100644
--- a/chrome/content/zotero/xpcom/attachments.js
+++ b/chrome/content/zotero/xpcom/attachments.js
@@ -563,7 +563,8 @@ Zotero.Attachments = new function(){
 						reject(e);
 					},
 					true,
-					cookieSandbox
+					cookieSandbox,
+					{ allowImages: true }
 				);
 			});
 		};
diff --git a/chrome/content/zotero/xpcom/http.js b/chrome/content/zotero/xpcom/http.js
index 92fb00241b..a8a91f10dd 100644
--- a/chrome/content/zotero/xpcom/http.js
+++ b/chrome/content/zotero/xpcom/http.js
@@ -1217,9 +1217,10 @@ Zotero.HTTP = new function() {
 	 * @param {Boolean} dontDelete Don't delete the hidden browser upon completion; calling function
 	 *                             must call deleteHiddenBrowser itself.
 	 * @param {Zotero.CookieSandbox} [cookieSandbox] Cookie sandbox object
+	 * @param {Object} [docShellPrefs] See Zotero.Browser.createHiddenBrowser
 	 * @return {browser} Hidden browser used for loading
 	 */
-	this.loadDocuments = function (urls, processor, onDone, onError, dontDelete, cookieSandbox) {
+	this.loadDocuments = function (urls, processor, onDone, onError, dontDelete, cookieSandbox, docShellPrefs={}) {
 		// (Approximately) how many seconds to wait if the document is left in the loading state and
 		// pageshow is called before we call pageshow with an incomplete document
 		const LOADING_STATE_TIMEOUT = 120;
@@ -1342,6 +1343,9 @@ Zotero.HTTP = new function() {
 			currentURL = 0;
 		for(var i=0; i<urls.length; i++) {
 			let hiddenBrowser = Zotero.Browser.createHiddenBrowser();
+			for (let pref in docShellPrefs) {
+				hiddenBrowser.docShell[pref] = docShellPrefs[pref];
+			}
 			if (cookieSandbox) {
 				cookieSandbox.attachToBrowser(hiddenBrowser);
 			}