Browse Source

Merge pull request #120 from edg2s/script-path-fix

Remove document.write for script path and allow external config
pull/125/head
Matt Holt 10 years ago
parent
commit
ec99cc82f6
  1. 33
      papaparse.js

33
papaparse.js

@ -7,7 +7,7 @@
{ {
"use strict"; "use strict";
var IS_WORKER = !global.document, SCRIPT_PATH; var IS_WORKER = !global.document, LOADED_SYNC = false, AUTO_SCRIPT_PATH;
var workers = {}, workerIdCounter = 0; var workers = {}, workerIdCounter = 0;
// A configuration object from which to draw default settings // A configuration object from which to draw default settings
@ -39,6 +39,8 @@
global.Papa.BYTE_ORDER_MARK = "\ufeff"; global.Papa.BYTE_ORDER_MARK = "\ufeff";
global.Papa.BAD_DELIMITERS = ["\r", "\n", "\"", global.Papa.BYTE_ORDER_MARK]; global.Papa.BAD_DELIMITERS = ["\r", "\n", "\"", global.Papa.BYTE_ORDER_MARK];
global.Papa.WORKERS_SUPPORTED = !!global.Worker; 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 // Configurable chunk sizes for local and remote files, respectively
global.Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB global.Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB
@ -147,9 +149,22 @@
if (IS_WORKER) if (IS_WORKER)
{
global.onmessage = workerThreadReceivedMessage; global.onmessage = workerThreadReceivedMessage;
}
else if (Papa.WORKERS_SUPPORTED) else if (Papa.WORKERS_SUPPORTED)
SCRIPT_PATH = getScriptPath(); {
AUTO_SCRIPT_PATH = getScriptPath();
// Check if the script was loaded synchronously
if ( !document.body ) {
// Body doesn't exist yet, must be synchronous
LOADED_SYNC = true;
} else {
document.addEventListener('DOMContentLoaded', function () {
LOADED_SYNC = true;
}, true);
}
}
@ -157,7 +172,7 @@
function CsvToJson(_input, _config) function CsvToJson(_input, _config)
{ {
var config = IS_WORKER ? _config : copyAndValidateConfig(_config); var config = IS_WORKER ? _config : copyAndValidateConfig(_config);
var useWorker = config.worker && Papa.WORKERS_SUPPORTED && SCRIPT_PATH; var useWorker = config.worker && Papa.WORKERS_SUPPORTED;
if (useWorker) if (useWorker)
{ {
@ -1331,16 +1346,20 @@
// the script path here. See: https://github.com/mholt/PapaParse/issues/87#issuecomment-57885358 // the script path here. See: https://github.com/mholt/PapaParse/issues/87#issuecomment-57885358
function getScriptPath() function getScriptPath()
{ {
var id = "worker" + String(Math.random()).substr(2); var scripts = document.getElementsByTagName('script');
document.write('<script id="'+id+'"></script>'); return scripts.length ? scripts[scripts.length - 1].src : '';
return document.getElementById(id).previousSibling.src;
} }
function newWorker() function newWorker()
{ {
if (!Papa.WORKERS_SUPPORTED) if (!Papa.WORKERS_SUPPORTED)
return false; return false;
var w = new global.Worker(SCRIPT_PATH); if (!LOADED_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.onmessage = mainThreadReceivedMessage;
w.id = workerIdCounter++; w.id = workerIdCounter++;
workers[w.id] = w; workers[w.id] = w;

Loading…
Cancel
Save