viewer.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Copyright 2016 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /* globals chrome */
  16. 'use strict';
  17. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME')) {
  18. var defaultUrl; // eslint-disable-line no-var
  19. (function rewriteUrlClosure() {
  20. // Run this code outside DOMContentLoaded to make sure that the URL
  21. // is rewritten as soon as possible.
  22. let queryString = document.location.search.slice(1);
  23. let m = /(^|&)file=([^&]*)/.exec(queryString);
  24. defaultUrl = m ? decodeURIComponent(m[2]) : '';
  25. // Example: chrome-extension://.../http://example.com/file.pdf
  26. let humanReadableUrl = '/' + defaultUrl + location.hash;
  27. history.replaceState(history.state, '', humanReadableUrl);
  28. if (top === window) {
  29. chrome.runtime.sendMessage('showPageAction');
  30. }
  31. })();
  32. }
  33. let pdfjsWebApp, pdfjsWebAppOptions;
  34. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('PRODUCTION')) {
  35. pdfjsWebApp = require('./app.js');
  36. pdfjsWebAppOptions = require('./app_options.js');
  37. }
  38. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('FIREFOX || MOZCENTRAL')) {
  39. require('./firefoxcom.js');
  40. require('./firefox_print_service.js');
  41. }
  42. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
  43. require('./genericcom.js');
  44. }
  45. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME')) {
  46. require('./chromecom.js');
  47. }
  48. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME || GENERIC')) {
  49. require('./pdf_print_service.js');
  50. }
  51. function getViewerConfiguration() {
  52. return {
  53. appContainer: document.body,
  54. mainContainer: document.getElementById('viewerContainer'),
  55. viewerContainer: document.getElementById('viewer'),
  56. eventBus: null, // Using global event bus with (optional) DOM events.
  57. toolbar: {
  58. container: document.getElementById('toolbarViewer'),
  59. numPages: document.getElementById('numPages'),
  60. pageNumber: document.getElementById('pageNumber'),
  61. scaleSelectContainer: document.getElementById('scaleSelectContainer'),
  62. scaleSelect: document.getElementById('scaleSelect'),
  63. customScaleOption: document.getElementById('customScaleOption'),
  64. previous: document.getElementById('previous'),
  65. next: document.getElementById('next'),
  66. zoomIn: document.getElementById('zoomIn'),
  67. zoomOut: document.getElementById('zoomOut'),
  68. viewFind: document.getElementById('viewFind'),
  69. openFile: document.getElementById('openFile'),
  70. print: document.getElementById('print'),
  71. presentationModeButton: document.getElementById('presentationMode'),
  72. download: document.getElementById('download'),
  73. viewBookmark: document.getElementById('viewBookmark'),
  74. },
  75. secondaryToolbar: {
  76. toolbar: document.getElementById('secondaryToolbar'),
  77. toggleButton: document.getElementById('secondaryToolbarToggle'),
  78. toolbarButtonContainer:
  79. document.getElementById('secondaryToolbarButtonContainer'),
  80. presentationModeButton:
  81. document.getElementById('secondaryPresentationMode'),
  82. openFileButton: document.getElementById('secondaryOpenFile'),
  83. printButton: document.getElementById('secondaryPrint'),
  84. downloadButton: document.getElementById('secondaryDownload'),
  85. viewBookmarkButton: document.getElementById('secondaryViewBookmark'),
  86. firstPageButton: document.getElementById('firstPage'),
  87. lastPageButton: document.getElementById('lastPage'),
  88. pageRotateCwButton: document.getElementById('pageRotateCw'),
  89. pageRotateCcwButton: document.getElementById('pageRotateCcw'),
  90. cursorSelectToolButton: document.getElementById('cursorSelectTool'),
  91. cursorHandToolButton: document.getElementById('cursorHandTool'),
  92. scrollVerticalButton: document.getElementById('scrollVertical'),
  93. scrollHorizontalButton: document.getElementById('scrollHorizontal'),
  94. scrollWrappedButton: document.getElementById('scrollWrapped'),
  95. spreadNoneButton: document.getElementById('spreadNone'),
  96. spreadOddButton: document.getElementById('spreadOdd'),
  97. spreadEvenButton: document.getElementById('spreadEven'),
  98. documentPropertiesButton: document.getElementById('documentProperties'),
  99. },
  100. fullscreen: {
  101. contextFirstPage: document.getElementById('contextFirstPage'),
  102. contextLastPage: document.getElementById('contextLastPage'),
  103. contextPageRotateCw: document.getElementById('contextPageRotateCw'),
  104. contextPageRotateCcw: document.getElementById('contextPageRotateCcw'),
  105. },
  106. sidebar: {
  107. // Divs (and sidebar button)
  108. outerContainer: document.getElementById('outerContainer'),
  109. viewerContainer: document.getElementById('viewerContainer'),
  110. toggleButton: document.getElementById('sidebarToggle'),
  111. // Buttons
  112. thumbnailButton: document.getElementById('viewThumbnail'),
  113. outlineButton: document.getElementById('viewOutline'),
  114. attachmentsButton: document.getElementById('viewAttachments'),
  115. // Views
  116. thumbnailView: document.getElementById('thumbnailView'),
  117. outlineView: document.getElementById('outlineView'),
  118. attachmentsView: document.getElementById('attachmentsView'),
  119. },
  120. sidebarResizer: {
  121. outerContainer: document.getElementById('outerContainer'),
  122. resizer: document.getElementById('sidebarResizer'),
  123. },
  124. findBar: {
  125. bar: document.getElementById('findbar'),
  126. toggleButton: document.getElementById('viewFind'),
  127. findField: document.getElementById('findInput'),
  128. highlightAllCheckbox: document.getElementById('findHighlightAll'),
  129. caseSensitiveCheckbox: document.getElementById('findMatchCase'),
  130. entireWordCheckbox: document.getElementById('findEntireWord'),
  131. findMsg: document.getElementById('findMsg'),
  132. findResultsCount: document.getElementById('findResultsCount'),
  133. findPreviousButton: document.getElementById('findPrevious'),
  134. findNextButton: document.getElementById('findNext'),
  135. },
  136. passwordOverlay: {
  137. overlayName: 'passwordOverlay',
  138. container: document.getElementById('passwordOverlay'),
  139. label: document.getElementById('passwordText'),
  140. input: document.getElementById('password'),
  141. submitButton: document.getElementById('passwordSubmit'),
  142. cancelButton: document.getElementById('passwordCancel'),
  143. },
  144. documentProperties: {
  145. overlayName: 'documentPropertiesOverlay',
  146. container: document.getElementById('documentPropertiesOverlay'),
  147. closeButton: document.getElementById('documentPropertiesClose'),
  148. fields: {
  149. 'fileName': document.getElementById('fileNameField'),
  150. 'fileSize': document.getElementById('fileSizeField'),
  151. 'title': document.getElementById('titleField'),
  152. 'author': document.getElementById('authorField'),
  153. 'subject': document.getElementById('subjectField'),
  154. 'keywords': document.getElementById('keywordsField'),
  155. 'creationDate': document.getElementById('creationDateField'),
  156. 'modificationDate': document.getElementById('modificationDateField'),
  157. 'creator': document.getElementById('creatorField'),
  158. 'producer': document.getElementById('producerField'),
  159. 'version': document.getElementById('versionField'),
  160. 'pageCount': document.getElementById('pageCountField'),
  161. 'pageSize': document.getElementById('pageSizeField'),
  162. 'linearized': document.getElementById('linearizedField'),
  163. },
  164. },
  165. errorWrapper: {
  166. container: document.getElementById('errorWrapper'),
  167. errorMessage: document.getElementById('errorMessage'),
  168. closeButton: document.getElementById('errorClose'),
  169. errorMoreInfo: document.getElementById('errorMoreInfo'),
  170. moreInfoButton: document.getElementById('errorShowMore'),
  171. lessInfoButton: document.getElementById('errorShowLess'),
  172. },
  173. printContainer: document.getElementById('printContainer'),
  174. openFileInputName: 'fileInput',
  175. debuggerScriptPath: './debugger.js',
  176. };
  177. }
  178. function webViewerLoad() {
  179. let config = getViewerConfiguration();
  180. if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
  181. Promise.all([
  182. SystemJS.import('pdfjs-web/app'),
  183. SystemJS.import('pdfjs-web/app_options'),
  184. SystemJS.import('pdfjs-web/genericcom'),
  185. SystemJS.import('pdfjs-web/pdf_print_service'),
  186. ]).then(function([app, appOptions, ...otherModules]) {
  187. window.PDFViewerApplication = app.PDFViewerApplication;
  188. window.PDFViewerApplicationOptions = appOptions.AppOptions;
  189. app.PDFViewerApplication.run(config);
  190. });
  191. } else {
  192. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME')) {
  193. pdfjsWebAppOptions.AppOptions.set('defaultUrl', defaultUrl);
  194. }
  195. window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;
  196. window.PDFViewerApplicationOptions = pdfjsWebAppOptions.AppOptions;
  197. if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
  198. // Give custom implementations of the default viewer a simpler way to
  199. // set various `AppOptions`, by dispatching an event once all viewer
  200. // files are loaded but *before* the viewer initialization has run.
  201. const event = document.createEvent('CustomEvent');
  202. event.initCustomEvent('webviewerloaded', true, true, {});
  203. document.dispatchEvent(event);
  204. }
  205. pdfjsWebApp.PDFViewerApplication.run(config);
  206. }
  207. }
  208. if (document.readyState === 'interactive' ||
  209. document.readyState === 'complete') {
  210. webViewerLoad();
  211. } else {
  212. document.addEventListener('DOMContentLoaded', webViewerLoad, true);
  213. }