RunScript

From WxWiki
Jump to: navigation, search

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: Menu RunScript.png

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:

String.png Integer.png Double.png Bool.png Object.png Array.png Date.png Null.png Undefined.png Dom.png

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

Limits

  • On GTK and MSW there aren't known limits. However, on OSX there are some limits:
  1. JavaScript allocations greater than 10MB are not allowed
  2. 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