@@ -12,26 +12,21 @@ function redirectIfNotDisplayedInFrame () {
1212}
1313redirectIfNotDisplayedInFrame ( ) ;
1414
15- // When "PDFViewerApplication.initialize" is executed it overwrites the value of
16- // "PDFJS.workerSrc", so the custom initialization has to be executed after
17- // that. As "PDFViewerApplication" does not provide any hookable point for
18- // custom initialization in its initialization routine a dirty hack has to be
19- // used.
20- //
21- // When "vendor/pdfjs/web/viewer.js" is parsed at the end it calls
22- // "PDFViewerApplication.initialize" (either directly or as a "DOMContentLoaded"
23- // event handler), and "PDFViewerApplication.initialize" sends an asynchronous
24- // XHR request to initialize the locales, which causes the execution flow to
25- // continue with the next script or the next "DOMContentLoaded" handler. Thanks
26- // to this "initializeCustomPDFViewerApplication" can be executed at that point
27- // by parsing "js/workersrc.js" after "vendor/pdfjs/web/viewer.js" and either
28- // calling it directly or adding it as a "DOMContentLoaded" event handler (using
29- // the same conditions as in "vendor/pdfjs/web/viewer.js").
15+ // When "PDFViewerApplication.webViewerInitialized" is executed (once
16+ // "PDFViewerApplication.initialize" is done) it opens the PDF file via URL,
17+ // which requires the PDFViewerApplication to be properly configured, so the
18+ // custom initialization has to be executed before that. This can be done by
19+ // listening to the "webviewerloaded" event, which is emitted after
20+ // "PDFViewerApplication" and "PDFViewerApplicationOptions" are globally set and
21+ // before "PDFViewerApplication.initialize" is executed.
3022function initializeCustomPDFViewerApplication ( ) {
31- PDFJS . openExternalLinksInNewWindow = true ;
32- PDFJS . isEvalSupported = false ;
33- PDFJS . workerSrc = document . getElementsByTagName ( 'head' ) [ 0 ] . getAttribute ( 'data-workersrc' ) ;
34- PDFJS . cMapUrl = document . getElementsByTagName ( 'head' ) [ 0 ] . getAttribute ( 'data-cmapurl' ) ;
23+ // Preferences override options, so they must be disabled for
24+ // "externalLinkTarget" to take effect.
25+ PDFViewerApplicationOptions . set ( 'disablePreferences' , true ) ;
26+ PDFViewerApplicationOptions . set ( 'externalLinkTarget' , pdfjsLib . LinkTarget . BLANK ) ;
27+ PDFViewerApplicationOptions . set ( 'isEvalSupported' , false ) ;
28+ PDFViewerApplicationOptions . set ( 'workerSrc' , document . getElementsByTagName ( 'head' ) [ 0 ] . getAttribute ( 'data-workersrc' ) ) ;
29+ PDFViewerApplicationOptions . set ( 'cMapUrl' , document . getElementsByTagName ( 'head' ) [ 0 ] . getAttribute ( 'data-cmapurl' ) ) ;
3530
3631 // The download has to be forced to use the URL of the file; by default
3732 // "PDFViewerApplication.download" uses a blob, but this causes a CSP error
@@ -77,8 +72,4 @@ function initializeCustomPDFViewerApplication() {
7772 } ;
7873}
7974
80- if ( document . readyState === 'interactive' || document . readyState === 'complete' ) {
81- initializeCustomPDFViewerApplication ( ) ;
82- } else {
83- document . addEventListener ( 'DOMContentLoaded' , initializeCustomPDFViewerApplication , true ) ;
84- }
75+ document . addEventListener ( 'webviewerloaded' , initializeCustomPDFViewerApplication , true ) ;
0 commit comments