How do I use the Browser's "Execute Javascript" action?

0 favourites
  • 5 posts
From the Asset Store
Alot of High Quality Items sound effects for your game!
  • I'm having trouble understanding how exactly to get this to work. I have a script called NoSleep.js, which I've imported into my construct project files and is supposed to prevent display sleep on any Android or iOS web browser. But I have no idea what exactly I'm supposed to enter into the Execute Javascript action field. The name of the script? The script itself? Or what? There isn't really much documentation on this, and I can't seem to find any .capx examples of it anywhere.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The action does exactly what it says, it executes some snippet of JavaScript. It's mostly useful for simple JavaScript stuff.

    It's not quite sufficient to easily use some JavaScript library. First, importing the library into the files folder isn't enough to make it usable. for that it needs to be loaded in one of three ways:

    1. Edit the exported HTML file and add the library with the other js files.

    2. Load it after the fact with the jquery.getScript function.

    3. Make a plugin and put the library in it's dependencies section of the edittime.js.

    One is a bit akward to use for testing, but it is the normal way to include JavaScript files in HTML.

    Two is slightly tricky since loading a library is asynchronous so the library can take time to load an may not be usable right away. The syntax is $.getScript(filename, callback) and callback is a function to call when the library is done loading. If you want examples of such a thing search my posts for JavaScript.

    Three is the reccomended way by making a plugin. Doing it with a plugin may make some things more straightforward than the other two methods.

    So then after you get the library loaded how you use the library depends on the library.

  • I think you can load a project js file using the script tag.

    Getting that into the editor is not easy.

    You can use a variable to get past the editors formatting limits.

    Then again, why bother with that when you can put the script into the variable.

    If it's not to big that is.

  • In the case of that library here's the text to run that will load the library and run the example code from the libraries webpage.

    "$.getScript('NoSleep.js', function(){
    document.noSleep = new NoSleep();
    
    function enableNoSleep() {
      document.noSleep.enable();
      document.removeEventListener('touchstart', enableNoSleep, false);
    }
    
    document.addEventListener('touchstart', enableNoSleep, false);
    });"[/code:3ch80xxw]
  • I've trouble to exec a java method by the following.

    I use the Browser.executeJavaScript from Construct2... As Function name i said: startGame();

    In Android Studio I created a Class like:

    public class GameEventsPlugin extends CordovaPlugin {

    private Context context;

    override

    public void initialize(CordovaInterface cordova, CordovaWebView webView) {

    this.context = cordova.getActivity().getApplicationContext();

    super.initialize(cordova, webView);

    }

    override

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    if (action.equals("startGame")) {

    Toast.makeText(this.context, "App gestartet", Toast.LENGTH_LONG).show();

    }

    return true;

    }

    }

    In my activfity class I put this before loadUrl(launchUrl):

    pluginEntries.add(new PluginEntry("QM-Plugins", new GameEventsPlugin()));

    The initialize method is called, but never the execute... What is wrong?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)