From 3a5526e24749f5a773b842237cbdd51e8ad607ea Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 17 Aug 2017 17:30:28 +0200 Subject: [PATCH] infer pdfjs document title from iframe url --- www/common/pdfjs/web/viewer.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/www/common/pdfjs/web/viewer.js b/www/common/pdfjs/web/viewer.js index 676a92f40..1cb7efcb5 100644 --- a/www/common/pdfjs/web/viewer.js +++ b/www/common/pdfjs/web/viewer.js @@ -341,25 +341,14 @@ function isDataSchema(url) { return url.substr(i, 5).toLowerCase() === 'data:'; } function getPDFFileNameFromURL(url) { - var defaultFilename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'document.pdf'; - - if (isDataSchema(url)) { - console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.'); - return defaultFilename; - } - var reURI = /^(?:(?:[^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/; - var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i; - var splitURI = reURI.exec(url); - var suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]); - if (suggestedFilename) { - suggestedFilename = suggestedFilename[0]; - if (suggestedFilename.indexOf('%') !== -1) { - try { - suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0]; - } catch (e) {} - } + var query; + var title; + if (/\?/.test(url)) { + url.replace(/\?(.*)$/, function (all, t) { + title = t; + }); } - return suggestedFilename || defaultFilename; + return title || 'document.pdf'; } function normalizeWheelEventDelta(evt) { var delta = Math.sqrt(evt.deltaX * evt.deltaX + evt.deltaY * evt.deltaY); @@ -1192,11 +1181,13 @@ var PDFViewerApplication = { setTitleUsingUrl: function pdfViewSetTitleUsingUrl(url) { this.url = url; this.baseUrl = url.split('#')[0]; - var title = (0, _ui_utils.getPDFFileNameFromURL)(url, ''); + + var title = _ui_utils.getPDFFileNameFromURL(url); if (!title) { try { title = decodeURIComponent((0, _pdfjsLib.getFilenameFromUrl)(url)) || url; } catch (e) { + console.error(e) title = url; } }