Improve downloadPDFViaBrowser handlers and logging
This commit is contained in:
parent
6af3b0276a
commit
5095543bef
1 changed files with 28 additions and 15 deletions
|
@ -1149,10 +1149,10 @@ Zotero.Attachments = new function(){
|
||||||
let channelBrowser, hiddenBrowser;
|
let channelBrowser, hiddenBrowser;
|
||||||
let hiddenBrowserPDFFoundDeferred = Zotero.Promise.defer();
|
let hiddenBrowserPDFFoundDeferred = Zotero.Promise.defer();
|
||||||
|
|
||||||
|
let isOurPDF = false;
|
||||||
var pdfMIMETypeHandler = async (blob, name, _, channel) => {
|
var pdfMIMETypeHandler = async (blob, name, _, channel) => {
|
||||||
Zotero.debug(`downloadPDFViaBrowser: Sniffing a PDF loaded at ${name}`);
|
Zotero.debug(`downloadPDFViaBrowser: Sniffing a PDF loaded at ${name}`);
|
||||||
|
|
||||||
let isOurPDF = false;
|
|
||||||
// try the browser
|
// try the browser
|
||||||
try {
|
try {
|
||||||
channelBrowser = channel.notificationCallbacks.getInterface(Ci.nsIWebNavigation)
|
channelBrowser = channel.notificationCallbacks.getInterface(Ci.nsIWebNavigation)
|
||||||
|
@ -1187,23 +1187,36 @@ Zotero.Attachments = new function(){
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
Zotero.MIMETypeHandler.addHandler("application/pdf", pdfMIMETypeHandler, true);
|
Zotero.MIMETypeHandler.addHandler("application/pdf", pdfMIMETypeHandler, true);
|
||||||
let noop = () => 0;
|
function noop() {};
|
||||||
hiddenBrowser = Zotero.HTTP.loadDocuments([url], noop, noop, noop, true, options.cookieSandbox);
|
hiddenBrowser = Zotero.HTTP.loadDocuments([url], noop, noop, noop, true, options.cookieSandbox);
|
||||||
let onLoadTimeoutDeferred = Zotero.Promise.defer();
|
let onLoadTimeoutDeferred = Zotero.Promise.defer();
|
||||||
hiddenBrowser.addEventListener("load", async function onLoad() {
|
let currentUrl = "";
|
||||||
|
hiddenBrowser.addProgressListener({
|
||||||
|
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIWebProgressListener,
|
||||||
|
Components.interfaces.nsISupportsWeakReference]),
|
||||||
|
onProgressChange: noop,
|
||||||
|
onStateChange: noop,
|
||||||
|
onStatusChange: noop,
|
||||||
|
onSecurityChange: noop,
|
||||||
|
async onLocationChange() {
|
||||||
|
let url = hiddenBrowser.contentDocument.location.href
|
||||||
|
if (currentUrl) {
|
||||||
|
Zotero.debug(`downloadPDFViaBrowser: A JS redirect occurred to ${hiddenBrowser.contentDocument.location.href}`);
|
||||||
|
}
|
||||||
|
currentUrl = url;
|
||||||
Zotero.debug(`downloadPDFViaBrowser: Page with potential JS redirect loaded, giving it ${onLoadTimeout}ms to process`);
|
Zotero.debug(`downloadPDFViaBrowser: Page with potential JS redirect loaded, giving it ${onLoadTimeout}ms to process`);
|
||||||
hiddenBrowser.addEventListener("unload", () => {
|
|
||||||
Zotero.debug(`downloadPDFViaBrowser: A JS redirect occurred, short timeout cancelled`);
|
|
||||||
hiddenBrowser.removeEventListener('load', onLoad);
|
|
||||||
});
|
|
||||||
await Zotero.Promise.delay(onLoadTimeout);
|
await Zotero.Promise.delay(onLoadTimeout);
|
||||||
onLoadTimeoutDeferred.reject(`Loading PDF via browser timed out on the JS challenge page after ${onLoadTimeout}ms`);
|
// If URL changed that means we got redirected and the onLoadTimeout needs to restart
|
||||||
}, true);
|
if (currentUrl === url && !isOurPDF) {
|
||||||
|
onLoadTimeoutDeferred.reject(new Error(`downloadPDFViaBrowser: Loading PDF via browser timed out on the JS challenge page after ${onLoadTimeout}ms`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
await Zotero.Promise.race([
|
await Zotero.Promise.race([
|
||||||
onLoadTimeoutDeferred.promise,
|
onLoadTimeoutDeferred.promise,
|
||||||
Zotero.Promise.delay(downloadTimeout).then(() => {
|
Zotero.Promise.delay(downloadTimeout).then(() => {
|
||||||
if (!hiddenBrowserPDFFoundDeferred.promise.isResolved()) {
|
if (!isOurPDF) {
|
||||||
throw new Error(`Loading PDF via browser timed out after ${downloadTimeout}ms`);
|
throw new Error(`downloadPDFViaBrowser: Loading PDF via browser timed out after ${downloadTimeout}ms`);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
hiddenBrowserPDFFoundDeferred.promise
|
hiddenBrowserPDFFoundDeferred.promise
|
||||||
|
@ -1214,7 +1227,7 @@ Zotero.Attachments = new function(){
|
||||||
await OS.File.remove(path, { ignoreAbsent: true });
|
await OS.File.remove(path, { ignoreAbsent: true });
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
Zotero.logError(e, 1);
|
Zotero.logError(e);
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue