c++ tiny-js execute javascript function from external file - javascript

I have a C++ application which has to execute a Javascript-function from a dynamic file. (I need to read a proxy.pac file).
E.g. I have a C++ application running which reads the following file: proxy.pac (which contains the javascript function FindProxyForURL(url, host), which I have to call with the two parameters.
However, I have no idea how to start off and I can't find anything on the internet so far.
So basically my question is: how doe I load dynamic Javascript from a file and execute a function within that code(/file)

I made application server using TinyJS.
see this.
https://github.com/pochi0701/wizdlive

Related

Photoshop 8800 Error only after python creates the file

I have a bit of a weird situation that I will try to explain the best I can.
I am using Python to launch photoshop and run a javascript file. But my goal is for python to generate the javascript first, then run it in photoshop.
In order to do that I have python copying the javascript file, then replacing a single line of code and running this new copy.
When I run the original javascript file it works as intended with no problems.
When I run the copied javascript file it works as intended with no problems.
When I run the copied javascript file that has the line replaced, it gives me an 8800 error.
At this point, even if I manually type the replaced line to match the original javascript file. I will still get an 8800 error.
Does python somehow write files differently?
Here is the code I am using to replace the copy and replace the javascript contents:
from shutil import copyfile
jsx_file = r'E:\PS\_javascript_constructor_template.jsx'
jsx_file_new = r'E:\PS\_javascript_constructor_template_new.jsx'
copyfile(jsx_file, jsx_file_new)
with open(jsx_file_new, "r") as fin:
data = fin.read()
with open(jsx_file_new, "w") as fout:
fout.write(data.replace("!REPLACEME!",'"E:\PS\MockVar.csv"'))
Any ideas?
SIDE NOTE: I am only doing this because I have no idea how to pass an argument from python into the javascript file I am subprocess calling.
I would much rather send an argument to the javascript file than build new files enitrely.
If you'd rather send an argument to the Photoshop script, I'd recommend using interprocess communication. You can use the socket module in Python and the Socket object in Extendscript to send messages back and forth. Check out External Communications Tools in the Adobe Tools Guide for more information.

Load js dynamically in an Node.js received from mongoDB (hosted #Azure)

I have a Node.js server hosted in Azure
Developers should be able to write JavaScript satisfying an API and upload it to my MongoDB hosted somewhere else.
My Node.js server now gets triggered and picks the right logic from the MongoDB.
How can I execute this Code dynamically the best way?
Use Eval? Or can I load the file in MainMemory and execute it?
The loaded code must be able to access other methods running on the Server (the ones documented in the API for the developer)
Basically, you are right, we can leverage eval() to execute the javascript code from string, if you need to access to the local scope. You can refer to https://nodejs.org/api/vm.html#vm_vm_runinthiscontext_code_options for the detailed explanation.
Here is my test file in MongoDB:
{
"id": "123",
"code": "someFuc()"
}
And test code snippet in Node.js:
function someFuc(){
console.log("this is consoled from node.js application");
}
...
// file results query from db
...
var code = results[0].code;
console.log(code);
eval(code);
I found a pretty neat solution:
transform the JavaScript syntax to "Node.js" syntax
write the module with the fs-library to a file
"require" the created file to a variable
execute the logic of the variable
delete the file afterwards
--> you can now debug the code unlike with using "eval"

Passing Value from C++ to Javascript

i have a c++ file which reads values from a sensor and I want to display those values on a website dynamically. So Im looking for a way to pass these values(integers) from my cpp file to an javascript which displays them on the site.
My first, simple try was to write the values into a js file as variables every second from my cpp script. The Js then uses this file as a source and displays its variables on the site:
cpp:
fprintf(file, "var mx=%d, my=%d, mz=%d, ax=%d, ay=%d, az=%d, gx=%d, gy=%d, gz=%d;\n",
imu.raw_m[0], imu.raw_m[1], imu.raw_m[2], // M = Magnetometer
imu.raw_a[0], imu.raw_a[1], imu.raw_a[2], // A = Accelerometer
imu.raw_g[0], imu.raw_g[1], imu.raw_g[2] // G = Gyroscope
);
html/js:
<script src="./imu.js" type="text/javascript"></script>
The Problem now is of course, that I need to refresh the page all the time, because the imu.js file is cached by the website.
I'd rather have a way to directly pass to integers from the cpp file to the js script. I read something about json or Googles V8 script. But I'd like to hear your suggestions first.
By the way, Im running this on a raspi, if this is important.
Thanks for your help
EDIT:
I'm goning to try it with a mysql database, in which my cpp file writes the data from the sensor with Connector/c++ from http://dev.mysql.com/doc/connector-cpp/en/ and my website reads them.
You could compile your C++ code into a Node.js plugin, you can then register a JavaScript function with your plugin which the C++ calls when it updates the value. That way you can pass values directly from C++ into Javascript in a managed and controlled way.
Node.js has the added benefit of being able to host your webpage and do all the Websocket and HTTP stuff that can be a pain in C++.
You do not have to refresh if your script is smart about how to access the data file! In case you do have a webserver at hand: Take care that your data file is accessible by your webserver and then let your script request the file via ajax (link to w3schools)
I'm doing something similar on a BeagleBone Black. With websocketd you can turn pretty much any program into a websocket endpoint and then send data via stdin and stdout commands. This would be a particularly good solution for you since websockets are designed to handle information that's constantly changing.

How do I use javascript variable in LuCI Lua markup?

I am creating an HTML file for use with OpenWrt LuCI web interface. As discussed here: http://luci.subsignal.org/trac/wiki/Documentation/Templates I am using the Lua Markup language to run a Lua function called runDiag and I need to pass the javascript variable option to the runDiag function. I can't figure out how to make this work. I have tried various modifications to the markup inside the displayDiag function without success.
Can anyone help?
Example:
<%-
function runDiag(option)
return option
end
-%>
<script>
function displayDiag() {
var option = document.getElementById('iface').value;
document.getElementById('diag_text').innerHTML = '<%- write (runDiag(option)) -%>';
}
</script>
You can't do this. The Lua template is ran on the server, and the JavaScript code is ran on the client (i.e. web browser). They can't communicate.
The Lua code simply generates an HTML file to send to the client. It doesn't know about JavaScript; it's just some text that it's giving to the client. Here, option refers to a nonexistant Lua variable, which has the value of nil.
Conversely, the JavaScript code has no knowledge of the server-side Lua code. It just gets whatever the server generated. Thus, it only sees this line:
document.getElementById('diag_text').innerHTML = 'nil';
To communicate with the web server, you will need use AJAX or some other protocol.

Can a Flash SWF access Javascript when the files are on different servers? SwfAddress.js not working.

I had a somewhat weird setup for a site here, which goes as follows:
Server A holds a SWF
Server B holds a HTML document with the SWF Embedded, along with SwfAddress.js
I can't seem to get any Flash-to-javascript to work here through External Interface calls. SwfAddress fails and I see this error in Firebugs Console: "Error calling method on NPObject!"
This also occurs with another JS function I'm trying to call with the MouseWheel.
I've set up my crossdomains and deployed all Security.allowDomain("")'s where applicable.
Has anyone embedded an external SWF and had issues with AS-JS communication before??
take a look at this thread? similar issue http://www.actionscript.org/forums/showthread.php3?t=137138

Categories

Resources