Browse Source

Remove document.write for script path and allow external config

1. Use document.getElementsByTag name to find the script's path.
   When used async, this fails by providing the wrong path instead
   of failing catastrophically.
2. Detect if the script was loaded asynchronously by attaching a load
   event to the body and seeing if it fires.
3. Evaluate the correct script path when it is requested. This gives
   the user time to manually configuring it using the Papa.SCRIPT_PATH
   global. If this isn't done and the script was loaded async, throw
   a helpful error explaining how to fix.
pull/120/head
Ed Sanders 10 years ago
parent
commit
65388f3d50
  1. 28
      papaparse.js

28
papaparse.js

@ -7,7 +7,7 @@ @@ -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 @@ @@ -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 @@ @@ -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 @@ @@ -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 @@ @@ -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('<script id="'+id+'"></script>');
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;

Loading…
Cancel
Save