diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index c97b53c66..45e61eb6f 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -7,7 +7,7 @@ 'use strict'; -getPdf('helloworld.pdf', function getPdfHelloWorld(data) { +PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) { // // Instantiate PDFDoc with PDF data // diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html index 0fa711fe4..a48e3705b 100644 --- a/examples/helloworld/index.html +++ b/examples/helloworld/index.html @@ -3,6 +3,7 @@ + @@ -23,9 +24,9 @@ diff --git a/src/core.js b/src/core.js index 8d3d6d434..48b1d0591 100644 --- a/src/core.js +++ b/src/core.js @@ -471,26 +471,18 @@ var PDFDoc = (function() { this.pageCache = []; if (useWorker) { - var worker; - if (typeof PDFJS_WORKER_DIR !== 'undefined') { - // If `PDFJS_WORKER_DIR` is specified, we assume the pdf.js files - // located all in that directory. Create a new worker and tell him - // the directory, such that he can load the scripts from there. - worker = new Worker(PDFJS_WORKER_DIR + 'worker_loader.js'); - console.log('main: post dir'); - - worker.postMessage(PDFJS_WORKER_DIR); - } else if (typeof PDFJS_WORKER_FILE !== 'undefined') { - // If we build the worker using a worker file, then we assume, that - // everything the worker needs is already included in that file. - // Therefore the worker doesn't have to call `importScripts` to load - // all the single files and therefore it's not necessary to tell the - // worker a directory to laod the js files from. - // (Which is different from the PDFJS_WORKER_DIR case above.) - worker = new Worker(PDFJS_WORKER_FILE); - } else { - throw 'No worker file or directory specified.'; + var workerSrc = PDFJS.workerSrc; + if (typeof workerSrc === 'undefined') { + throw 'No PDFJS.workerSrc specified'; } + + var worker = new Worker(workerSrc); + + // Tell the worker the file it was created from. + worker.postMessage({ + action: 'workerSrc', + data: workerSrc + }); } else { // If we don't use a worker, just post/sendMessage to the main thread. var worker = { diff --git a/src/pdf.js b/src/pdf.js index 2ecd2fddc..51f606548 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -10,5 +10,5 @@ var PDFJS = {}; // Files are inserted below - see Makefile /* PDFJSSCRIPT_INCLUDE_ALL */ -})(); +}).call((typeof window === 'undefined') ? this : window); diff --git a/src/worker.js b/src/worker.js index 5545fc459..d46364f60 100644 --- a/src/worker.js +++ b/src/worker.js @@ -47,6 +47,13 @@ var WorkerProcessorHandler = { setup: function(handler) { var pdfDoc = null; + handler.on('workerSrc', function(data) { + // In development, the `workerSrc` message is handled in the + // `worker_loader.js` file. In production the workerProcessHandler is + // called for this. This servers as a dummy to prevent calling an + // undefined action `workerSrc`. + }); + handler.on('doc', function(data) { // Create only the model of the PDFDoc, which is enough for // processing the content of the pdf. @@ -174,7 +181,7 @@ var workerConsole = { // Worker thread? if (typeof window === 'undefined') { - this.console = workerConsole; + globalScope.console = workerConsole; var handler = new MessageHandler('worker_processor', this); WorkerProcessorHandler.setup(handler); diff --git a/src/worker_loader.js b/src/worker_loader.js index 69f7d55ba..8eb1da7b2 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -7,11 +7,19 @@ this.onmessage = function(evt) { // Reset the `onmessage` function as it was only set to call // this function the first time a message is passed to the worker // but shouldn't get called anytime afterwards. - delete this.onmessage; + this.onmessage = null; - // Directory the include files are contained is send as the - // first message to the worker. - var dir = evt.data; + if (evt.data.action !== 'workerSrc') { + throw 'Worker expects first message to be `workerSrc`'; + } + + // Content of `PDFJS.workerSrc` as defined on the main thread. + var workerSrc = evt.data.data; + + // Extract the directory that contains the source files to load. + // Assuming the source files have the same relative possition as the + // `workerSrc` file. + var dir = workerSrc.substring(0, workerSrc.lastIndexOf('/') + 1); // List of files to include; var files = [ diff --git a/test/test_slave.html b/test/test_slave.html index 7ac886769..91852d5a5 100644 --- a/test/test_slave.html +++ b/test/test_slave.html @@ -3,25 +3,29 @@ pdf.js test slave - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + diff --git a/web/viewer-snippet.html b/web/viewer-snippet.html index 4d2a4f7b7..6b0c8821b 100644 --- a/web/viewer-snippet.html +++ b/web/viewer-snippet.html @@ -1,7 +1,6 @@ diff --git a/web/viewer.html b/web/viewer.html index 4505331d6..7dd81e0af 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -5,7 +5,6 @@ - @@ -25,6 +24,8 @@ + +