2222 * 
2323 */ 
2424
25- import  canDownload  from  './utils/canDownload ' 
25+ import  logger  from  './services/logger ' 
2626import  redirectIfNotIframe  from  './utils/redirectIfNotIframe' 
2727
28- /** 
29-  * Checks if the page is displayed in an iframe. If not redirect to /. 
30-  **/ 
28+ // Checks if the page is displayed in an iframe. If not redirect to /. 
3129redirectIfNotIframe ( ) 
3230
3331// When "PDFViewerApplication.webViewerInitialized" is executed (once 
@@ -38,7 +36,6 @@ redirectIfNotIframe()
3836// "PDFViewerApplication" and "PDFViewerApplicationOptions" are globally set and 
3937// before "PDFViewerApplication.initialize" is executed. 
4038function  initializeCustomPDFViewerApplication ( )  { 
41- 
4239	// Preferences override options, so they must be disabled for 
4340	// "externalLinkTarget" to take effect. 
4441	PDFViewerApplicationOptions . set ( 'disablePreferences' ,  true ) 
@@ -48,86 +45,7 @@ function initializeCustomPDFViewerApplication() {
4845	PDFViewerApplicationOptions . set ( 'cMapUrl' ,  document . getElementsByTagName ( 'head' ) [ 0 ] . getAttribute ( 'data-cmapurl' ) ) 
4946	PDFViewerApplicationOptions . set ( 'enablePermissions' ,  true ) 
5047
51- 	console . debug ( 'Initialized files_pdfviewer' ,  PDFViewerApplicationOptions . getAll ( ) ) 
52- 
53- 	// The download has to be forced to use the URL of the file; by default 
54- 	// "PDFViewerApplication.download" uses a blob, but this causes a CSP error 
55- 	// (at least, in Firefox) when trying to download it. 
56- 	PDFViewerApplication . download  =  function ( )  { 
57- 		// "isDataSchema()" and "getPDFFileNameFromURL()" are copied from 
58- 		// "vendor/pdfjs/web/viewer.js", as the functions defined in that file 
59- 		// can not be accessed from the outside. 
60- 		function  isDataSchema ( url )  { 
61- 			let  i  =  0 
62- 			const  ii  =  url . length 
63- 			while  ( i  <  ii  &&  url [ i ] . trim ( )  ===  '' )  { 
64- 				i ++ 
65- 			} 
66- 			return  url . substr ( i ,  5 ) . toLowerCase ( )  ===  'data:' 
67- 		} 
68- 
69- 		function  getPDFFileNameFromURL ( url )  { 
70- 			const  defaultFilename  =  arguments . length  >  1  &&  arguments [ 1 ]  !==  undefined  ? arguments [ 1 ]  : 'document.pdf' 
71- 
72- 			if  ( isDataSchema ( url ) )  { 
73- 				console . warn ( 'getPDFFileNameFromURL: '  +  'ignoring "data:" URL for performance reasons.' ) 
74- 				return  defaultFilename 
75- 			} 
76- 			const  reURI  =  / ^ (?: (?: [ ^ : ] + : ) ? \/ \/ [ ^ / ] + ) ? ( [ ^ ? # ] * ) ( \? [ ^ # ] * ) ? ( # .* ) ? $ / 
77- 			const  reFilename  =  / [ ^ / ? # = ] + \. p d f \b (? ! .* \. p d f \b ) / i
78- 			const  splitURI  =  reURI . exec ( url ) 
79- 			let  suggestedFilename  =  reFilename . exec ( splitURI [ 1 ] )  ||  reFilename . exec ( splitURI [ 2 ] )  ||  reFilename . exec ( splitURI [ 3 ] ) 
80- 			if  ( suggestedFilename )  { 
81- 				suggestedFilename  =  suggestedFilename [ 0 ] 
82- 				if  ( suggestedFilename . indexOf ( '%' )  !==  - 1 )  { 
83- 					try  { 
84- 						suggestedFilename  =  reFilename . exec ( decodeURIComponent ( suggestedFilename ) ) [ 0 ] 
85- 					}  catch  ( e )  { 
86- 						console . debug ( e ) 
87- 					} 
88- 				} 
89- 			} 
90- 			return  suggestedFilename  ||  defaultFilename 
91- 		} 
92- 
93- 		const  url  =  decodeURIComponent ( window . location . search . substr ( 6 ) ) 
94- 
95- 		this . downloadManager . downloadUrl ( url ,  getPDFFileNameFromURL ( url ) ) 
96- 	} 
97- 
98- 	if  ( ! canDownload ( ) )  { 
99- 		// Disable download function when downloads are hidden, as even if the 
100- 		// buttons in the UI are hidden the download could still be triggered 
101- 		// with Ctrl|Meta+S. 
102- 		PDFViewerApplication . download  =  function ( )  { 
103- 		} 
104- 		const  downloadButton  =  document . getElementById ( 'toolbarViewerRight' ) . querySelector ( 'button.download' ) 
105- 		if  ( downloadButton )  { 
106- 			downloadButton . style . display  =  'none' 
107- 		} 
108- 
109- 		// Disable printing service when downloads are hidden, as even if the 
110- 		// buttons in the UI are hidden the printing could still be triggered 
111- 		// with Ctrl|Meta+P. 
112- 		// Abuse the "supportsPrinting" parameter, which signals that the 
113- 		// browser does not fully support printing, to make PDFViewer disable 
114- 		// the printing service. 
115- 		// "supportsPrinting" is a getter function, so it needs to be deleted 
116- 		// before replacing it with a simple value. 
117- 		delete  PDFViewerApplication . supportsPrinting 
118- 		PDFViewerApplication . supportsPrinting  =  false 
119- 
120- 		// When printing is not supported a warning is shown by the default 
121- 		// "beforePrint" function when trying to print. That function needs to 
122- 		// be replaced with an empty one to prevent that warning to be shown. 
123- 		PDFViewerApplication . beforePrint  =  function ( )  { 
124- 		} 
125- 
126- 		// For css properties 
127- 		document . getElementById ( 'viewer' ) . classList . add ( 'disabledTextSelection' ) 
128- 
129- 		console . debug ( 'Files_PDFViewer, download and print disabled' ) 
130- 	} 
48+ 	logger . debug ( 'Initialized files_pdfviewer' ,  PDFViewerApplicationOptions . getAll ( ) ) 
13149} 
13250
13351document . addEventListener ( 'DOMContentLoaded' ,  initializeCustomPDFViewerApplication ,  true ) 
0 commit comments