Morning all!
I'm looking at trying to make JS calls from within Processing. I've managed to get the Javascript library that comes with Processing working, but I've only been able to get alert calls to work, while I'd ideally like to be able to call entire functions.
If I place the function verbatim inside the eval(), it breaks as soon as it encounters a new line. If I place the entire function onto a single line by eliminating the line breaks, it crashes upon encountering a colon.
If anyone could give me some pointers, that would be great. Alternatively, if anyone has a way of reading cookies from a Processing applet, that would be great too! (I could only find reference to a single cookie library for Processing, but it looks like it's long since been taken offline)
Thanks all!
This is the test code I'm working with:
This is the JS function I'd like to call upon mousePressed:
I'm looking at trying to make JS calls from within Processing. I've managed to get the Javascript library that comes with Processing working, but I've only been able to get alert calls to work, while I'd ideally like to be able to call entire functions.
If I place the function verbatim inside the eval(), it breaks as soon as it encounters a new line. If I place the entire function onto a single line by eliminating the line breaks, it crashes upon encountering a colon.
If anyone could give me some pointers, that would be great. Alternatively, if anyone has a way of reading cookies from a Processing applet, that would be great too! (I could only find reference to a single cookie library for Processing, but it looks like it's long since been taken offline)
Thanks all!
This is the test code I'm working with:
- import netscape.javascript.*;
- void setup() {}
- void draw() {}
- void mousePressed() {
- try {
- JSObject.getWindow(this).eval( "alert(\"This is a JS test.\")" );
- }
- catch (JSException e) {
- e.printStackTrace();
- }
- }
This is the JS function I'd like to call upon mousePressed:
- function checkCookie() {
- if (document.cookie.length>0) {
- c_start=document.cookie.indexOf("cookieID=");
- if (c_start!=-1) {
- c_start=c_start + 8;
- c_end=document.cookie.indexOf(";",c_start);
- if (c_end==-1) c_end=document.cookie.length;
- cookieID = unescape(document.cookie.substring(c_start,c_end));
- }
- }
- else {
- cookieID = "";
- }
- if (cookieID!=null && cookieID!="") {
- alert('You have a cookie!');
- }
- else {
- alert('No cookies for you =(');
- }
- }
1