Просмотр исходного кода

use XHR to fetch resources when autosave is active

Former-commit-id: f2439d7871f40bfdb8d493ad9c75f2635ac183a0
Gildas 5 лет назад
Родитель
Сommit
d72884f907
2 измененных файлов с 25 добавлено и 3 удалено
  1. 24 2
      extension/core/bg/autosave.js
  2. 1 1
      extension/lib/single-file/index.js

+ 24 - 2
extension/core/bg/autosave.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global singlefile, URL, Blob, woleet */
+/* global singlefile, URL, Blob, XMLHttpRequest, woleet */
 
 singlefile.extension.core.bg.autosave = (() => {
 
@@ -118,7 +118,7 @@ singlefile.extension.core.bg.autosave = (() => {
 		options.tabIndex = tab.index;
 		let pageData;
 		try {
-			pageData = await singlefile.extension.getPageData(options, null, null);
+			pageData = await singlefile.extension.getPageData(options, null, null, { fetch });
 			if (options.includeInfobar) {
 				await singlefile.common.ui.content.infobar.includeScript(pageData);
 			}
@@ -140,4 +140,26 @@ singlefile.extension.core.bg.autosave = (() => {
 		}
 	}
 
+	function fetch(url) {
+		return new Promise((resolve, reject) => {
+			const xhrRequest = new XMLHttpRequest();
+			xhrRequest.withCredentials = true;
+			xhrRequest.responseType = "arraybuffer";
+			xhrRequest.onerror = event => reject(new Error(event.detail));
+			xhrRequest.onreadystatechange = () => {
+				if (xhrRequest.readyState == XMLHttpRequest.DONE) {
+					resolve({
+						status: xhrRequest.status,
+						headers: {
+							get: name => xhrRequest.getResponseHeader(name)
+						},
+						arrayBuffer: async () => xhrRequest.response
+					});
+				}
+			};
+			xhrRequest.open("GET", url, true);
+			xhrRequest.send();
+		});
+	}
+
 })();

+ 1 - 1
extension/lib/single-file/index.js

@@ -38,5 +38,5 @@ this.singlefile.extension = this.singlefile.extension || {
 		}
 	},
 	injectScript: (tabId, options) => this.singlefile.extension.lib.core.bg.scripts.inject(tabId, options),
-	getPageData: (options, doc, win) => this.singlefile.lib.getPageData(options, { fetch: this.singlefile.extension.lib.fetch.content.resources.fetch }, doc, win)
+	getPageData: (options, doc, win, initOptions = { fetch: this.singlefile.extension.lib.fetch.content.resources.fetch }) => this.singlefile.lib.getPageData(options, initOptions, doc, win)
 };