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

Get help using Construct 2

Post » Fri Feb 24, 2017 6:21 pm

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.
B
9
S
3
Posts: 14
Reputation: 703

Post » Fri Feb 24, 2017 7:21 pm

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.
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Fri Feb 24, 2017 8:06 pm

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.
Image ImageImage
B
169
S
50
G
173
Posts: 8,314
Reputation: 110,272

Post » Fri Feb 24, 2017 9:09 pm

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.
Code: Select all
"$.getScript('NoSleep.js', function(){
document.noSleep = new NoSleep();

function enableNoSleep() {
  document.noSleep.enable();
  document.removeEventListener('touchstart', enableNoSleep, false);
}

document.addEventListener('touchstart', enableNoSleep, false);
});"
B
92
S
32
G
109
Posts: 5,290
Reputation: 70,991

Post » Wed Jun 28, 2017 5:48 pm

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?
B
7
Posts: 2
Reputation: 291


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, ItsAstro, kellehk and 29 guests