@@ -186,65 +186,88 @@ We need to check if the viewer is initialized, before handling the event.
186
186
+ // Ignore mousewheel event if pdfViewer isn't loaded
187
187
+ if (!PDFViewerApplication.pdfViewer) return;
188
188
189
- #### Load code for worker using AJAX
189
+ #### Load code for worker using AJAX if needed
190
190
191
191
A [ Web Worker] ( https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers ) can't use code from
192
- a same-origin domain. The CORS headers don't apply (they do work in some browsers, but not in all).
193
-
194
- To get around this, the source code of the worker is fetched using AJAX and presented as blob.
195
-
196
- var WorkerTransport = (function WorkerTransportClosure() {
197
- function WorkerTransport(workerInitializedCapability, pdfDataRangeTransport) {
198
- + /**
199
- + * Needed because workers cannot load scripts outside of the current origin (as of firefox v45).
200
- + * This patch does require the worker script to be served with a (Access-Control-Allow-Origin: *) header
201
- + * @patch
202
- + */
203
- + globalScope.cachedJSDfd = null;
204
- + this.loadWorkerXHR = function(url){
205
- + if (globalScope.cachedJSDfd) { return globalScope.cachedJSDfd; }
206
- + globalScope.cachedJSDfd = PDFJS.createPromiseCapability();
207
- +
208
- + var xmlhttp;
209
- + xmlhttp = new XMLHttpRequest();
210
- +
211
- + xmlhttp.onreadystatechange = function(){
212
- + if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
213
- + var workerJSBlob = new Blob([xmlhttp.responseText], { type: 'text/javascript' });
214
- + globalScope.cachedJSDfd.resolve(window.URL.createObjectURL(workerJSBlob));
215
- + }
216
- + };
217
- +
218
- + xmlhttp.open('GET', url, true);
219
- + xmlhttp.send();
220
- + return globalScope.cachedJSDfd.promise;
221
- + }
222
-
223
- Instead of simply starting the worker, we need to get the code and than start it.
224
-
225
- + this.loadWorkerXHR(workerSrc).then(function(blob) {
226
- + workerSrc = PDFJS.workerSrc = blob;
227
- +
228
- try {
229
- // Some versions of FF can't create a worker on localhost, see:
230
- // https://bugzilla.mozilla.org/show_bug.cgi?id=683280
231
- @@ -3589,11 +3618,16 @@
232
- return;
233
- } catch (e) {
234
- info('The worker has been disabled.');
235
- + this.setupFakeWorker();
192
+ a same-origin domain. The CORS headers don't apply.
193
+
194
+ he patch will cause pdf.js to first try to create the Worker the regular way, with a URL to the JavaScript source. If
195
+ this fails, the source if fetched using AJAX and used to create an
196
+ [ object url] ( https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL ) . If this also fails, pdf.js will go
197
+ onto it's last resort by calling ` setupFakeWorker() ` .
198
+
199
+ + /**
200
+ + * Needed because workers cannot load scripts outside of the current origin (as of firefox v45).
201
+ + * This patch does require the worker script to be served with a (Access-Control-Allow-Origin: *) header
202
+ + * @patch
203
+ + */
204
+ + var loadWorkerXHR = function(){
205
+ + var url = PDFJS.workerSrc;
206
+ + var jsdfd = PDFJS.createPromiseCapability();
207
+ +
208
+ + if (url.match(/^blob:/) || typeof URL.createObjectURL === 'undefined') {
209
+ + jsdfd.reject(); // Failed loading using blob
210
+ + }
211
+ +
212
+ + var xmlhttp;
213
+ + xmlhttp = new XMLHttpRequest();
214
+ +
215
+ + xmlhttp.onreadystatechange = function(){
216
+ + if (xmlhttp.readyState != 4) return;
217
+ +
218
+ + if (xmlhttp.status == 200) {
219
+ + info('Loaded worker source through XHR.');
220
+ + var workerJSBlob = new Blob([xmlhttp.responseText], { type: 'text/javascript' });
221
+ + jsdfd.resolve(window.URL.createObjectURL(workerJSBlob));
222
+ + } else {
223
+ + jsdfd.reject();
224
+ + }
225
+ + };
226
+ +
227
+ + xmlhttp.open('GET', url, true);
228
+ + xmlhttp.send();
229
+ + return jsdfd.promise;
230
+ + }
231
+ +
232
+ + var workerError = function() {
233
+ + loadWorkerXHR().then(function(blob) {
234
+ + PDFJS.workerSrc = blob;
235
+ + loadWorker();
236
+ + }, function() {
237
+ + this.setupFakeWorker();
238
+ + }.bind(this));
239
+ + }.bind(this);
240
+ +
241
+
242
+ - if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
243
+ + var loadWorker = function() {
244
+ var workerSrc = PDFJS.workerSrc;
245
+ if (!workerSrc) {
246
+ error('No PDFJS.workerSrc specified');
247
+ @@ -3559,6 +3603,8 @@
248
+ // Some versions of FF can't create a worker on localhost, see:
249
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=683280
250
+ var worker = new Worker(workerSrc);
251
+ + worker.onerror = workerError;
252
+ +
253
+ var messageHandler = new MessageHandler('main', worker);
254
+ this.messageHandler = messageHandler;
255
+
256
+ @@ -3589,11 +3635,16 @@
257
+ return;
258
+ } catch (e) {
259
+ info('The worker has been disabled.');
260
+ + workerError();
261
+ }
262
+ - }
263
+ + }.bind(this);
264
+ // Either workers are disabled, not supported or have thrown an exception.
265
+ // Thus, we fallback to a faked worker.
266
+ - this.setupFakeWorker();
267
+ + if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
268
+ + loadWorker();
269
+ + } else {
270
+ + this.setupFakeWorker();
271
+ + }
236
272
}
237
- +
238
- + }.bind(this));
239
- }
240
-
241
- As the worker is started async, we only start the worker on an error, when the browser doesn't support web workers or
242
- when it's explicitly set that we don't want to use the web worker api.
243
-
244
- // Either workers are disabled, not supported or have thrown an exception.
245
- // Thus, we fallback to a faked worker.
246
- - this.setupFakeWorker();
247
- + if (globalScope.PDFJS.disableWorker || typeof Worker === 'undefined') {
248
- + this.setupFakeWorker();
249
- + }
250
273
0 commit comments