diff --git a/papaparse.js b/papaparse.js index 230be43..7832d24 100644 --- a/papaparse.js +++ b/papaparse.js @@ -7,7 +7,7 @@ { "use strict"; - var IS_WORKER = !global.document, SCRIPT_PATH; + var IS_WORKER = !global.document, IS_SYNC = false, AUTO_SCRIPT_PATH; var workers = {}, workerIdCounter = 0; // A configuration object from which to draw default settings @@ -39,6 +39,8 @@ global.Papa.BYTE_ORDER_MARK = "\ufeff"; global.Papa.BAD_DELIMITERS = ["\r", "\n", "\"", global.Papa.BYTE_ORDER_MARK]; global.Papa.WORKERS_SUPPORTED = !!global.Worker; + // Must be set externally if using workers and Papa Parse is loaded asynchronously + global.Papa.SCRIPT_PATH = null; // Configurable chunk sizes for local and remote files, respectively global.Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB @@ -147,9 +149,17 @@ if (IS_WORKER) + { global.onmessage = workerThreadReceivedMessage; + } else if (Papa.WORKERS_SUPPORTED) - SCRIPT_PATH = getScriptPath(); + { + AUTO_SCRIPT_PATH = getScriptPath(); + // Check if the script was loaded synchronously + document.body.addEventListener('load', function () { + IS_SYNC = true; + }, true); + } @@ -157,7 +167,7 @@ function CsvToJson(_input, _config) { var config = IS_WORKER ? _config : copyAndValidateConfig(_config); - var useWorker = config.worker && Papa.WORKERS_SUPPORTED && SCRIPT_PATH; + var useWorker = config.worker && Papa.WORKERS_SUPPORTED && (Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH); if (useWorker) { @@ -1349,16 +1359,20 @@ // the script path here. See: https://github.com/mholt/PapaParse/issues/87#issuecomment-57885358 function getScriptPath() { - var id = "worker" + String(Math.random()).substr(2); - document.write(''); - return document.getElementById(id).previousSibling.src; + var scripts = document.getElementsByTagName('script'); + return scripts.length ? scripts[scripts.length - 1].src : ''; } function newWorker() { if (!Papa.WORKERS_SUPPORTED) return false; - var w = new global.Worker(SCRIPT_PATH); + if (!IS_SYNC && Papa.SCRIPT_PATH === null) + throw new Error( + 'Script path cannot be determined automatically when Papa Parse is loaded asynchronously. ' + + 'You need to set Papa.SCRIPT_PATH manually.' + ); + var w = new global.Worker(Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH); w.onmessage = mainThreadReceivedMessage; w.id = workerIdCounter++; workers[w.id] = w;