How to run a JavaScript file - V8 - c++

I have embedded v8 into my c++ application. Referring https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc I am able to run a javascript. Tested and works fine.
I access the links from my c++ application, download html data, download javascript. Some embedded scripts in the html call functions in external script files. How do I ensure that the external scripts are available for the embedded ones?
The downloaded JavaScript files (one or more) may be of large size. In such a context, how do I execute the JavaScript api present in HTML using v8? Code to run a JavaScript in v8 is below,
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
v8::NewStringType::kNormal)
.ToLocalChecked();
// Compile the source code.
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
// Run the script to get the result.
v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
Assuming downloaded javascript is 200KB, how can I feed such a large buffer to v8::Script::Compile api. And when I have more than one file present, how can feed I them to v8 ?

How do I ensure that the external scripts are available for the embedded ones?
You load the external scripts first.
How do I execute the JavaScript API present in HTML using v8?
Do you mean the DOM? window, document and the like? The DOM is not part of ECMAScript, so V8 knows nothing about it; it is provided by the embedder (i.e. usually Chrome). In your own embedding you need to provide all those objects yourself, using V8's API. Needless to say, this is a huge amount of work. If what you're after is a way to render websites, then I recommend that you use some existing component/library for that, for example the Chromium Embedded Framework, or your favorite GUI toolkit's WebView (or whatever it is called).
Assuming downloaded JavaScript is 200KB, how can I feed such a large buffer to v8::Script::Compile API?
Just like you feed a small script to V8: put it into a v8::Local<v8::String>, then call v8::Script::Compile and v8::Script::Run.
And when I have more than one file present, how can feed I them to v8 ?
Call v8::Script::Compile and v8::Script::Run repeatedly, possibly using a loop. For an example, see V8's shell sample, specifically the function RunMain.
As I receive partial JavaScript in HTTP packets (chunks), can I pass the partial JavaScript to V8?
Yes, V8 has a script streaming interface. See the API documentation for v8::ScriptCompiler::ExternalSourceStream. For examples on how to use it, you can study the tests. Streaming may or may not be worth it for scripts as small as 200KB; it is definitely not required.

Related

write output to json from c++ webassembly code using emscripten

I would like to write my program output i.e., "some-key:some-value" to JSON file while running my web assembly c++ code through Emscripten.
I found a way to read the data from JSON file through the same way, As described here.
Is there any possibility to update the data in browser view which dynamically changes every second?
It looks like the json library you reference supported writing json: https://github.com/nlohmann/json. There are examples of using the .dumps() method to output json.
I assume you want to update the DOM based on code running WebAssembly? The only want to effect the DOM from WebAssembly is call out to JS, or have JS call into wasm. For example you could have a setTimeout in JS that calls a WebAssembly function every second to retrieve some new state, and then use that to update the DOM.

how to read PAC file using C++

I am using libcurl for HTTP requests.
My application should be able to understand the proxy settings if the user has any
So it can be
Proxy by proxy server or
Proxy by Auto proxy Configuration
I see support for PAC isnt available in libcurl
Since my application is in C++, are there any extension / parser engine available?
Thanks for reading the post
PAC (Proxy Auto Config) file is simply Javascript that has function FindProxyForURL returning proxy config string.
Technically, this function can use anything that Javascript can do, so you must bundle some Javascript engine to interpret it.
tiny-js (simple single-file javascript interpreter written in C++) is the library that should fit the bill for this task.
UPDATE: pacparser library is pretty much ready-to-use engine designed specifically to parse pac files. Its downside that it bundles whole SpiderMonkey Javascript engine, which makes it rather heavy solution - it would add 1MB+ to your project binaries just to parse pac files.
If you can hack pacparser to use tiny-js instead of SpiderMonkey, that would be really nice solution.

Converting HTML file to PDF using Win32/MFC

As part of my application, my client has requested that I include an automated e-mailing system. As part of this system, I generate HTML code and use automation to send it via. Outlook.
However, they also require a PDF copy of the HTML document to be sent as an attachment. My initial attempts involved using libHaru, which proved difficult to use efficiently, as I was required to create the PDF document from scratch, which required computation of the position of each of the lines in a table, and positioning of all the text, etc.
I was wondering if there would be a way to programmatically convert HTML code (or an HTML file if need be) into a PDF document either by using Win32/MFC itself or an external library.
Thanks in advance!
EDIT: Just to clarify, I am looking for solutions which minimize external dependencies.
You should evaluate this utility wkhtmltopdf:
http://code.google.com/p/wkhtmltopdf/
You can call it from the command line without the need to run a setup.
I use it generating my output documents as html then cal a ShellExecute(...) to convert it to PDF. It's great!
Inside uses webkit + qt. So compability with modern HTML is OK.
Hope it helps.
I'd take a look at PDF Creator, which can be used as a COM object (that acts pretty much like a printer). I haven't used it to print HTML, so I'm not sure, but my guess is that you'll probably end up having to instantiate a web browser control to render the HTML, and then feed it from there to the PDF control.
Some possible answers are in this thread:
C++ Library to Convert HTML to PDF?
Not sure if they will satisfy your particular requirements, but these might at least get you started.
Edit:
Some other possible options here.
Not MFC but you can try QtWebKit. It can render and export HTML to PDF, PNG, JPEG

How to use V8's built in functions

I'm new in both javascript and V8. According to Google's Embedder's Guide, I saw something in the context section talking about built-in utility javascript functions. And I also found some .js files(e.g. math.js) in the downloaded source code, so I tried to write a simple program to call functions in these files, but I failed.
Does a context created by Persistent<Context> context = Context::New() have any built-in js functions? How can I access them?
Is there a way to first import existing js files as a library(something like src="xxx" type="text/javascript" in HTML page) and then run my own execute script?
Can I call google maps api through the embedded V8 library in app? How?
3. Google Maps needs a full browser DOM (or at least XMLHttpRequest I guess), you can't use it from just a Javascript library.
I think v8 gives you the Math.* functions for free.
You need to implement everything else yourself though, like loading other javascript files. shell.cc has some of the functions you might be looking for.
As for the maps API, I believe you would need a full blown rendering engine/javascript engine combo for that. You might be better off taking a look at Webkit or something that you can use to embed Webkit for what you're looking to do, I can't really say.
You can use for example the --allow_natives_syntax or --expose_natives_as option.
Here are examples with MathLog picked at random in src/math.js:
First compile a shell with
$ scons d8 -j8
Then use --expose_natives_as:
$ ./d8 --expose_natives_as nat
V8 version 3.12.7 (candidate) [console: dumb]
d8> nat.MathLog(100)
4.605170185988092
or use --allow_natives_syntax with the '%' prefix:
$ ./d8 --allow_natives_syntax
V8 version 3.12.7 (candidate) [console: dumb]
d8> %MathLog(100)
4.605170185988092

C++ writing HTML onto each of two already opened Firefox tabs from within extension

I'm seeking C++ help in writing HTML code to a new tab in Firefox within an extension.
Our C++ code has been partially wrapped by an XPCOM wrapper and embedded within a Firefox extension thanks to the work of a consultant we have lost contact with, and still partially implemented by calling out to a standalone executable.
To get our output displayed from the standalone executable, the C++ code writes the output to a file and simply calls system(firefox file.html) which then comes up with a file:-based URI.
This no longer works in all situations, based on a report from a user running Vista. So it seems to be time to do it right, and navigate the DOM, likely integrating the rest of the C++ code into the XPCOM-wrapped part. Perhaps there's a right way to do it from the standalone executable using the DOM model?
The "current working directory" seems to no longer match the directory in which the extension installed the standalone executable, with a "VirtualStore" path element.
We also generate parallel output in a different MIME type, VRML to be specific.
Any suggestions or examples for how to properly generate output into a Firefox browser pane under C++ programmatic control would be very much appreciated.
You could call Firefox with a fully specified file:/// URL, not a relative URL (file.html).
Or you if you want to dump a separate executable, you could implement a protocol handler or a simpler about module (where ios.newChannel would be replaced by your own channel implementation that generates the data).
I'd say keeping the file-generation solution is OK and doesn't seem very bad, so I'd go with (1), perhaps changing the generated file location to a temporary folder and specifying it fully both for the executable that generates it and for Firefox.