diff options
author | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2020-03-18 02:49:33 +0300 |
---|---|---|
committer | backportbot[bot] <backportbot[bot]@users.noreply.github.com> | 2020-03-18 17:17:41 +0300 |
commit | 8818adad0ec06e867d2299155e47a528fac1da20 (patch) | |
tree | f0237fc9cecef1bb2db0689eff03f3c5942de72a | |
parent | fc333643ab39016333bdf52fbb741a4b68386696 (diff) |
Update custom PDFViewerApplication initialization to PDF.js 2.1.266
Since v2.1.266 an event is triggered before the PDFViewerApplication is
initialized, so the hack that relied on the asynchronous calls during
the initialization to hook in is no longer needed.
However, as the event is triggered before the initialization the options
are now set before the preferences are read. As the preferences override
the options the preferences need to be disabled to be able to set
"externalLinkTarget". Disabling the preferences has no effect for the
rest of them, as they already have the same value as the default
options.
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
-rw-r--r-- | js/workersrc.js | 26 |
1 files changed, 8 insertions, 18 deletions
diff --git a/js/workersrc.js b/js/workersrc.js index a8cd485..1c9b4f0 100644 --- a/js/workersrc.js +++ b/js/workersrc.js @@ -15,20 +15,14 @@ redirectIfNotDisplayedInFrame(); // When "PDFViewerApplication.webViewerInitialized" is executed (once // "PDFViewerApplication.initialize" is done) it opens the PDF file via URL, // which requires the PDFViewerApplication to be properly configured, so the -// custom initialization has to be executed before that. As -// "PDFViewerApplication" does not provide any hookable point for custom -// initialization in its initialization routine a dirty hack has to be used. -// -// When "vendor/pdfjs/web/viewer.js" is parsed it calls -// "PDFViewerApplication.initialize" (either directly or as a "DOMContentLoaded" -// event handler), and "PDFViewerApplication.initialize" does some asynchronous -// calls, which causes the execution flow to continue with the next script or -// the next "DOMContentLoaded" handler. Thanks to this -// "initializeCustomPDFViewerApplication" can be executed at that point -// by parsing "js/workersrc.js" after "vendor/pdfjs/web/viewer.js" and either -// calling it directly or adding it as a "DOMContentLoaded" event handler (using -// the same conditions as in "vendor/pdfjs/web/viewer.js"). +// custom initialization has to be executed before that. This can be done by +// listening to the "webviewerloaded" event, which is emitted after +// "PDFViewerApplication" and "PDFViewerApplicationOptions" are globally set and +// before "PDFViewerApplication.initialize" is executed. function initializeCustomPDFViewerApplication() { + // Preferences override options, so they must be disabled for + // "externalLinkTarget" to take effect. + PDFViewerApplicationOptions.set('disablePreferences', true); PDFViewerApplicationOptions.set('externalLinkTarget', pdfjsLib.LinkTarget.BLANK); PDFViewerApplicationOptions.set('isEvalSupported', false); PDFViewerApplicationOptions.set('workerSrc', document.getElementsByTagName('head')[0].getAttribute('data-workersrc')); @@ -78,8 +72,4 @@ function initializeCustomPDFViewerApplication() { }; } -if (document.readyState === 'interactive' || document.readyState === 'complete') { - initializeCustomPDFViewerApplication(); -} else { - document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true); -} +document.addEventListener('webviewerloaded', initializeCustomPDFViewerApplication, true); |