RunScript
This is a project done on GSOC 2017.
Getting started
Prerequisites
You need to clone the project:
git clone https://github.com/joseeloren/wxWidgets.git
cd wxWidgets
Building the Library
- On GTK, using Ubuntu 16.04:
Firstly, install the dependencies required:
sudo apt-get install libgtk-3-dev
sudo apt-get install libwebkit2gtk-4.0-dev
sudo apt-get install libcppunit-dev
Secondly, Compile wxWidgets:
mkdir gtk-build
cd gtk-build
../configure --with-gtk=3
make
sudo make install
- On OSX, using macOS Sierra:
Firstly, install the dependencies required:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install cppunit
Secondly, compile wxWidgets:
mkdir osx-build
cd osx-build
../configure --with-macosx-version-min=10.9 --with-macosx-sdk=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk --enable-cxx11
make
sudo make install
Note: cxx11 is enable because cppunit from homebrew package manager requieres it, as well as osx 10.9 (to move from libstdc++ to libc++).
- On MSW, using Windows 10:
On Visual Studio Community 2017, using SDK 10: Firstly, open build\msw\wx_vc15.sln and replace on every project SDK version to 10. Secondly, right click on the solution and "Compile solution".
Building Sample
After a successful build, you can go to the samples sub-directory, compile and run them, for example:
- On GTK and OSX:
cd samples/webiew
make
./webview
- On MSW:
Open samples/webview/webview_vc9.vcproj, change SDK version to 10 and "Compile solution". Then, open a command prompt and go to samples/webview/vc_mswu and run webview.exe.
Then, on the top Menu, there is a list of RunScript samples, like "Return integer" and "Return string". Note: On MSW, there are three more samples, that uses MSWSetModernEmulationLevel. There is an image of RunScript menu:
Unit Tests
After a successful build, you can go to the tests sub-directory, compile and run them, for example:
- On GTK and OSX:
cd tests
make
./test_gui WebTestCase
- On MSW:
Open tests/test_gui_vc15.sln, change SDK version to 10 and "Compile solution". Then, go to tests/vc_mswu and ./test_gui WebTestCase.
Note: All tests made are passing on all platforms.
Features
Summary of features running:
- Return strings, integers, floating point numbers, booleans,
- Can modify DOM.
- Return objects as JSON string
Sample Images
Some images of webview sample running RunScript samples:
Samples source code
In order to illustrate some code examples of how to use RunScript, I will add some of them.
An example of how manage a JavaScript code that retuns a String:
wxString result = "";
if ( m_web_view->RunScript("function f(a){return a;}f('Hello World!');", &result) )
wxLogMessage(_("RunScript result: %s"), result);
else
wxLogWarning(_("RunScript returned false. There is an error on JavaScript."));
Another example returning an integer:
wxString result = "";
if ( m_web_view->RunScript("function f(a){return a;}f(123);", &result) )
wxLogMessage(_("RunScript result: %s"), result);
else
wxLogWarning(_("RunScript returned false. There is an error on JavaScript."));
And returning a JSON object:
wxString result = "";
if ( m_web_view->RunScript("function f(){var person = new Object();person.name = 'Foo'; \
person.lastName = 'Bar';return person;}f();", &result) )
wxLogMessage(_("RunScript result: %s"), result);
else
wxLogWarning(_("RunScript returned false. There is an error on JavaScript."));
An example of some code that modifies DOM (notice there isn't a return value):
if ( m_web_view->RunScript("document.write(\"Hello World!\");") )
wxLogMessage(_("RunScript runs properly."));
else
wxLogWarning(_("RunScript returned false. There is an error on JavaScript."));
Also, as it was commented before, on MSW there is the possibility to run the code with emulation level (IE8; please see Know issues section for more information):
wxWebViewIE::MSWSetModernEmulationLevel();
wxString result = "";
if ( m_web_view->RunScript("function f(){var person = new Object();person.name = 'Foo'; \
person.lastName = 'Bar';return person;}f();", &result) )
wxLogMessage(_("RunScript result: %s"), result);
else
wxLogWarning(_("RunScript returned false. There is an error on JavaScript."));
// If you don't want to continue use emulation level, this clear the registry value that changes it.
wxWebViewIE::MSWSetModernEmulationLevel(false);
Known issues and future work
- On MSW, some dates are not working. For example, new Date("2017-10-08T21:30:40Z") doesn't work. It is because we maintain compatibility with IE8 (using MSWSetModernEmulationLevel). More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
- On OSX, there is a workaround to get objects, nulls and undefines values. Also, it never returns nil in case of JS failing code. More information: https://stackoverflow.com/questions/37108379/how-does-stringbyevaluatingjavascriptfromstring-return-nil. In the future it will be an interesting idea move from this deprecated method to the async method: https://developer.apple.com/documentation/uikit/uiwebview/1617963-stringbyevaluatingjavascriptfrom?language=objc
- Doing it asynchronously. In the future, using WKWebView on OSX instead of UIWebView and on GTK asynchronous method is available, the main challenge is doing an async method on MSW. The possible solution is moving to Chromium, Edge or another library. Synchounly methods stuck the main loop until they finish running the JS code. More information is on the Google Document below.
Limits
- On GTK and MSW there aren't known limits. However, on OSX there are some limits:
- JavaScript allocations greater than 10MB are not allowed
- JavaScript that takes longer than 10 seconds to execute is not allowed
More information: https://stackoverflow.com/questions/7388743/stringbyevaluatingjavascriptfromstring-doesnt-always-seem-to-work
Developers
Developers can be contacted on the developer mailing list:
- Jose Lorenzo (GSoC 2017 developer)
More information about the project could be found on this Google Document: https://docs.google.com/document/d/1oA-lPdjwPWgWehhgSTNcZR1fL_z2ds8zgGY9QjLEwu4/edit?usp=sharing