Requiring a .js file in Windows - javascript

I'm just starting to pick up javascript and casperjs for a project that I'm doing. I'm trying to create a project that uses babyparse in it.
Currently I have babyparse.js located at C:\js\babyparse.js.
I'm having trouble including babyparse, and I've tried a couple different ways of doing it.
1) eval('C:\js\babyparse.js') - I come up with a syntax error "Invalid Character \u0008". I found this on StackOverflow, but I was unable to find the unicode character in babyparase.js.
2) require('C:\js\babyparse.js') - this just doesn't even find the right path in windows. I'm assuming that's because of the backslash pathing of windows as opposed to the regular unix forwardslash.
3) require('C:\\js\\babyparse.js') - this gets the correct path in windows, but I get the error that CasperJS couldn't find the module C:\js\babyparse.js
I feel like I've looked a lot through stackoverflow, but maybe I'm just missing something obvious. Thanks in advance for the help!

Currently I have babyparse.js located in C:\js\babyparse
require('C:\js\babyparse.js')
You've got the path wrong it seems. The correct path would be
require('C:\\js\\babyparse\\babyparse.js')
Beware that babyparse is a node.js module. It may use some of node.js native modules wich are not present in PhantomJS javascript interpreter in which case your script will break.
If you want to use csv parsing while scraping, it's definitely better to use PapaParse which is specifically designed as general-flavour javascript without dependencies, "not even jQuery".

I managed to solve the problem!
The following snippet worked for me:
var fs = require('fs');
eval(fs.read('papaparse.js'));

Related

Error while calling js library using $var in a Python app

In my python code using web.py, I am trying to call js library in main layout.
Here is the code snippet -
$var js: static/js/jquery-3.4.0.min.js static/js/bootstrap.min.js static/js/material.min.js
However, when I execute the code it gives me this error:
127.0.0.1:51225 - - [04/May/2019 07:05:29] "HTTP/1.1 GET /âModels/js1/bootstrap.min.js"" - 404 Not Found
I have also added screenshot of the code and folder structure as attachment. What I can see is that during the call to library, it adds the special characters â and then instead of single quotes there are double quotes at the end. I suspect this is the problem for the library not found.
I was thinking that this could be because of UTF-8 issues. Hence i went and also updated the encoding standards to UTF-8 both at server and client level. But even this is not solving the problem. Can someone please help with suggestions here. I have been struggling this from last 4-5 days and trying to research everything that could help but not of much use.
I'm using PyCharm for development.
The js library path is correct and it should have been called. If it calls then my js code will be executed. Please suggest if I'm doing something wrong here.

DebugKit CakePHP 3.x ReferenceError: __debug_kit_id is not defined

I recently upgraded php from 5.6 to 7 and I think it broke CakePHP's DebugKit. On every page I navigate to within my app, the debug_kit javascript file produces an error, specifically when setting the currentRequest. The error is:
ReferenceError: __debug_kit_id is not defined
At first I didn't install sqlite, so I thought that was the issue, but after installing the sqlite, mbstring, and intl extensions the error is still there. I can't seem to find an answer anywhere, most of the problems people encounter with debugkit seem to be solved by installing sqlite.
The code snippet from the debug_kit.js file:
$(document).ready(function() {
toolbar = new Toolbar({
button: $('#toolbar'),
content: $('#panel-content-container'),
panelButtons: $('.panel'),
panelClose: $('#panel-close'),
keyboardScope : $(document),
currentRequest: __debug_kit_id,
originalRequest: __debug_kit_id,
baseUrl: __debug_kit_base_url
});
toolbar.initialize();
Thanks everyone!
This not appear to be a PHP error, but rather a Javascript one because the variable __debug_kit_id is not defined anywhere in your Javascript code. Define it to be something like
var __debug_kit_id = 0;
or whatever value it should have and that will solve your error.
I was able to solve my own problem by removing all remaining php5 packages. I followed bento's answer from this askubuntu question: https://askubuntu.com/questions/59886/how-to-compelety-remove-php and going one by one through the old php5 packages (leaving php7 packages intact). Thanks all

Socket.io failed to load websocket.js

I tried to use socket.io in an simple app.
When it is calling the following js file:
node_modules\socket.io\lib\transports\websocket.js
The following line failed to load (It didn't really fail. It actually returned some empty result and failed some later codes using protocolVersions):
var protocolVersions = require('./websocket');
I debugged a little bit and it seems that the intention of this line is to load the following js file:
node_modules\socket.io\lib\transports\websocket\index.js
However because there is also a "websocket.js" (the file being run) under the "transports" folder, the require functions always tries to load "websocket.js" instead, and return some unexpected results.
I changed the folder name to "websockets" and call "require('./websockets')" instead, and it fixed the problem. However this is a shared library I am not feeling comfortable messing up its codes.
I am trying to understand why this problem happened this way and what should be the best way to work around it. I am sure this piece of codes has been well tested before released.
You probably already noticed that I am using backslash '\' to separate the path. Yes I am running it on Windows! Will that create any difference?
Thanks!

After Effects applyPreset : bad path

So I'm totally new to ExtendScript, and already amazed. I've been trying to automatize the creation of standard videos, and to do so I want to create presets and apply them to footage. So I use the applyPreset(File) method of the layers.
The new File("path") seems to open fine, but then applyPreset is unhappy because it tells me my path is wrong. Namely, After Effects says "Unable to call applyPreset because of parameter 1 : incorrect path "/Users/Charles/Documents/Slide-In-1.ffx"". Yet my path, I can assure you, is absolutely correct. Being on Mac, I used "~/Documents/Slide-In-1.ffx", which got replaced by "/Users/Charles/Documents/Slide-In-1.ffx" according to the error output (this replacement is correct, clearly). So where's the problem ? How can I check that a file has indeed been found and opened by JavaScript ? Thanks a lot in advance.
Charles
Seems my path was in fact bad. Something fishy, but I had to change my file organisation anyway to have a clean centralized git repo, and it started working.
you can test if a file exists like this:
if( File(YOURPATH).exists )
If true, this object refers to a file or file-system alias that actually exists in the file system.
http://jongware.mit.edu/Js/pc_File.html

Envjs and Rhino in a Java application - Where to find env.rhino.js?

I'm trying to run envjs and Rhino in a java application to render SVGs with D3.js.
So far, I can evaluate smaller functions using Rhino but when it comes to setting up envjs, the problems begin. The most important one is that the only tutorial for envjs talks about a file called env.rhino.js. But I have no idea where to find it.
Can anybody help me out?
(Yes, google shows some results but they are not officially belonging to Rhino or envjs)
I know this answer is very late. But I want to do the same and had the same problems - maybe this will help the next one. It took a while to figure out which one of the hundrets of github forks on env-js will do the job. I found out that this combination will work for a simple test:
git clone https://github.com/thatcher/envjs-site.git
#note the different fork!
wget https://raw.github.com/thatcher/env-js/master/src/dom/sizzle.js
wget http://d3js.org/d3.v3.min.js
java -jar dist/env-js-1.1.jar
load("lib/env.rhino.js");
load("sizzle.js");
load("d3.v3.min.js");
d3.select("body").append("svg").selectAll("line").data([1,2]).enter().append("line").attr("x1", function(d){return d;});
document.innerHTML;
<html><head/><body><line/><line/><svg xmlns="http://www.w3.org/2000/svg"><line x1="1"/><line x1="2"/></svg></body><line/><line/></html>
j
First, download env.rhino.js.
Then, use this Java code to start a Rhino instance and load Env.js:
import org.mozilla.javascript.Context;
import org.mozilla.javascript.tools.shell.Global;
import org.mozilla.javascript.tools.shell.Main;
Context cx = Context.enter();
Global scope = new Global(cx);
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_5);
Now you can load and run a JavaScript file (using its absolute file-system path)
Main.processFile(cx, scope, ABSOLUTE_PATH_TO_SOME_JAVASCRIPT_FILE);
And/or evaluate JavaScript code and get its String result
(String)cx.evaluateString(scope, "alert('Its WORKING!')", "js", 1, null);
I did attempt this but couldn't go very far. I also wanted to generate SVGs on server side, with requests being initiated from java(Glassfish in my case). The only way you can do this is with jsdom & Node.js. I am able to do this successfully. Sadly, other than Node.js + jsdom, there seems no other way to do this.
Once you get it working, there are bigger problems lurking if you try to heavily load Node.js with SVG generation requests.

Categories

Resources