Inserting javascript code from an external .js file
Hi there,
I'm trying to work out a method to get around the fact that OSWeb does allow loops to use a file as a source.
The initial part of the post relates to HTML but bear with me, I then do get on to my questions about OSweb (and JATOS)...
I'm currently exploring the possibility of using PapaParse (https://www.papaparse.com/) to parse a csv (from a URL) into an array. I've started exploring it using HTML code to host a page on my server. So far I've managed to write javascript code to access a csv file through a URL (hosted on Github to get around the cross-origin resource sharing (CORS) issue), and I've uploaded papaparse.min.js onto my server so that it can be called in using "<script src=... </script>". I've got some way to go still, but I do get the content of the csv into an array.
Here' what the method looks like in HTML:
You can try it out here: https://neurocog.uib.es/testcsv/test4.html (use the console to see the results).
I get this... The content of my csv file (2 headers on first row, then 4 rows with two pieces of string on each row). Sweet...
So far so good (or at least not too bad). One limitation is that Github allows access to csv files but apparently not to .js files (or at least I get a CORS warning message when I tried to specify the source of the .js file as a complete URL to a raw.github address). So, for now I got around this issue by copying the .js file to my server in the same folder as my HTML document.
Now comes the point where I get stuck... implementing it into OS... I'm trying to get OS to load up the papaparse.min.js file but I can't work out the correct path to use... (nor am I totally sure of the method I'm using to call the js file). This is what I'm trying:
I've also tried specifying the script.scr as "papaparse.min.js" (no "./" prefix) and I added papaparse.min.js to the pool file in OS. But to no avail. When running the task in the browser, the program can't find the .js file.
I tried loading up the task to JATOS on my own server (after installing papaparser through npm), but the same thing happens the task can't get to the .js file.
So, in a nutshell, I'm a little lost as how to specify paths and where to store the .js file for my task to find it when running it from my browser (especially through JATOS, as my aim is to use this for running an experiment online).
When trying to run it through JATOS, I get the following error message:
I can't work out where the publix/49/49 folder is located on my server, and I was wondering whether there is a way to have .js resources centrally located and set a path to that location instead of having to copy the .js resource inside every individual experiment folder (which I'm not sure how to do anyway).
Stuck at that stage, I can't really tell yet whether the next bit of the task will run smoothly:
I can't help but feel that calling on papaparse.min.js may be a possible solution to populate loops with data from a csv file, but as you can see, I'm having some issues pointing OS/JATOS to the .js resource. If that could be solved, I think I might then be able to get the content on the csv into an array and then use a modified version of the solution I posted in another thread to use that array as a substitute for the file source method of the loop in OS.
Sorry for the rather long description; just trying to be complete. In a nutshell, my problem is to get OS to read an external .js file (either hosted locally or from a URL, though I suspect the latter is doomed due to CORS). If anybody out there could help me with that bit, that would be awesome!
Best,
Fabrice.
Comments
Update and solution
Since my initial post, @sebastiaan pointed out that OSWeb 1.4 (part of the latest prerelease of OpenSesame 3.3.10) bundles up some Javascript libraries, including csv-parse, which should do the job I'm trying to do (see https://forum.cogsci.nl/discussion/comment/23997#Comment_23997 for related post and solution; see https://osdoc.cogsci.nl/3.3/manual/javascript/about/ for documentation).
In fact, in the latest prerelease of OpenSesame 3.3.10, files are supported as a source for loops, so it's not even necessary to parse csv files manually! Awesome!
As for importing an external Javascript (.js) library, I understand that it is not possible in Javascript (see https://forum.cogsci.nl/discussion/comment/23997#Comment_23997).
Good to hear the new functionality is finding an immediate use!
Check out SigmundAI.eu for our OpenSesame AI assistant!