B4J Question Run a JavaScript Silently in a non-ui Program

BillMeyer

Well-Known Member
Licensed User
Longtime User
Good Day,

I am very new at this type of methodology and before I pursue something that is actually not possible, I'd like to ask if one could run the following code in a non-ui B4J program to retrieve the co-ordinates of the current location of the laptop that it is running on and save them to a database (The database part is no problem).

Code is from w3schools.com (Link)

B4X:
<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

Actually all I need is to run the <script> </script> section and pass that back to 2x variables so that I can save it in a database.

If this can be done Then
Great
Please Help
Else
Back to the drawing board
Pass Ideas please
End

Have a Lekka Day !!
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
Thank you guys - I appreciate and I will try !!.


Bill
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I have managed to run your javascript in chrome and get the values back into a non-ui b4j program.

Unfortunately, the chrome window pops up for about a second whilst it runs the javascript, so not silent.

The code I used ( needs Selenium jars )
B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
 #CommandLineArgs:
 #MergeLibraries: True 
 #AdditionalJar: d:/selenium/client-combined-3.141.59.jar
 #AdditionalJar: d:/selenium/libs/guava-25.0-jre.jar
 #AdditionalJar: d:/selenium/libs/okio-1.14.0.jar
 #AdditionalJar: d:/selenium/libs/okhttp-3.11.0.jar
 #AdditionalJar: d:/selenium/libs/commons-exec-1.3.jar
 #AdditionalJar: d:/selenium/libs/byte-buddy-1.8.15.jar
#End Region
Sub Process_Globals
End Sub
Sub AppStart (Args() As String)
 Log("Location is : " & asJO(Me).RunMethod("doit",Null))
 'StartMessageLoop
End Sub
Sub asJO(o As JavaObject) As JavaObject
 Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
#if java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.lang.Thread;
public static String doit() {
  System.setProperty("webdriver.chrome.driver", "d:/selenium/libs/chromedriver.exe");
  WebDriver driver = new ChromeDriver();
        driver.get("file://c:/temp/geo.html");
  WebElement ele = driver.findElement(By.name("b1"));
  ele.click();
  try{
   Thread.sleep(4000);
  }catch (InterruptedException e){
   System.out.println(e.getMessage());
  }
  Object loc = driver.findElement(By.id("demo")).getText();
  //Close the browser
  driver.quit();
  return (String)loc;
}
#End If
 
Upvote 0
Top