I am creating a javascript api for my application and I want to use namespaces in my javascript code. However I am unable to get it to work nor find any information on the issue.
Desired Functionality:
HTML:
<script>
Android.typeOne.methodName();
Android.typeTwo.methodName();
</script>
Java Code:
webView.addJavascriptInterface(new TypeOneInterface(context), "Android.typeOne");
webView.addJavascriptInterface(new TypeTwoInterface(context), "Android.typeTwo");
However this never works, if I remove .typeOne and have: Android.methodName then that works fine.
I'm looking at the documentation of addJavascriptInterface. It says that The Java object's fields are not accessible. Since typeOne would have to be a property on the exported Java Object, it seems you would need to arrange the "namespace" by hand. That is, export TypeOne and put it in the global JavaScript Android object.
So I'm guessing you need to create empty objects, and put stuff in them as needed.
<script>
// after stuff has been "injected into the JS context of the main frame"
Android = {};
Android.typeOne = window.TypeOne;
... and
webView.addJavascriptInterface(new TypeOneInterface(context), "TypeOne");
This answer is a guess, I have never used JavaScript in a WebView.
I think that javascript understand that you are calling methodeName on object typeOne that is a child of object Android. But you don't have any object named Android nor typeOne.
The dot in javascript is used for hierarchy betweens parents and child.
You should try to use name without dots or, call your object differently (perhaps window["Android.typeOne"].methodName();
Related
I am new in Kotlin, and while working on a WebView app. I found
val webSettings = mWebView.settings
webSettings.javaScriptEnabled = true
2.mWebView.getSettings().setJavaScriptEnabled(true)
1 and 2 are Kotlin statements
What is the difference between these? which one is better to use?
There is no detail explanation about these.
There is no difference between both.
Kotlin supports property access syntax, which means if you have declared one variable in Kotlin, it'll have it's getters & setters by default (No need to declare explicitly).
You can also access getters & setters by accessing variable directly,
So, acessing setter method in kotlin :
mWebView.getSettings().setJavaScriptEnabled(true)
be like this if using property access syntax,
webSettings.javaScriptEnabled = true
More from here
Conclusion:
Simply,
if you assign variable in kotlin -> it will treated as setter
for that (in your case)
if you access variable in kotlin -> it will treated as getter
for that
First it will not different between writing a statement in android with java or kotlin .. both will make the same effect.
For using webSetting there is no different between both ..
but most of tutorials use the first one to get the Websetting object one then modify at as they want to enable or disable any webview setting instead of calling getSetting object every time they need it.
You can read more about Webiew in android from this article.
https://www.oreilly.com/library/view/building-hybrid-android/9781449361907/ch04.html
I write an app for android. There is a Webview and I want to put Text into the focussed TextArea on the website after clicking a button at my app. I haven't found any solution at google.
It is important to put in the text, it is no login view, where data can be transmitted by the POST methode, the user should be able to see the input.
I hope you can help me.
Thanks!
public void addJavascriptInterface (Object object, String name)
Added in API level 1
Injects the supplied Java object into this WebView. The object is injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object's methods to be accessed from JavaScript. For applications targeted to API level JELLY_BEAN_MR1 and above, only public methods that are annotated with JavascriptInterface can be accessed from JavaScript. For applications targeted to API level JELLY_BEAN or below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.
Note that injected objects will not appear in JavaScript until the page is next (re)loaded. For example:
class JsObject {
#JavascriptInterface
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
IMPORTANT:
This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk for applications targeted to API level JELLY_BEAN or below, because JavaScript could use reflection to access an injected object's public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content.
JavaScript interacts with Java object on a private, background thread of this WebView. Care is therefore required to maintain thread safety.
The Java object's fields are not accessible.
Parameters
object the Java object to inject into this WebView's JavaScript context. Null values are ignored.
name the name used to expose the object in JavaScript
I am using phone, sorry for formating. This is quote from offical page android.
So:
If is your page:
Set ID of Input, for example text (for example: <input id="text" ...)
Add this code of end of document:
<script>function a(b){document.getElementById("text (here is yur ID)").value=b;};function b(a){document.getElementById("text (here is your ID)").value+=a;}</script>
To change text, use this code:
String str="any string";
webview.loadUrl("javascript:a(\""+str+"\")");
To add text at the end:
String str="any string";
webview.loadUrl("javascript:b(\""+str+"\")");
I would like to know how I can get a value from a webview.
For example, to load a value into the webview I use:
myWebView.loadUrl("www.example.com");
myWebView.loadUrl("javascript:document.getElementById('username').value='"+ user + "';");
Now I would like to read some text from a certain ID from a webpage.
Something like:
myWebView.loadUrl("www.example.com");
myWebView.getUrl("javascript:document.getElementById('username').toString(user);");
Is this possible?
Edit: Since KitKat (API 19), the WebView class provides the method evaluateJavascript that allows to execute javascript and get the return values.
For olders API, you can still bind javascript to your Android Java code (look here)
So, you will implement a Javascript interface class and add it to your WebView and then call something like
myWebView.loadUrl("javascript:MyJavascriptInterface.getValue(document.getElementById('username').value);");
This will call the method getValue (this is a custom function, it is up to you to rename it as you want) in your class that is used as a Javascript interface, and change MyJavascriptInterface with the name you specified in addJavascriptInterface.
Yes, you can. getElementById('username').Value
I have a webview in a alertbox, in this webview I have a <textarea>
Anyone have an idea how to get the texts typed in this field and be used in the android app. as normal Edittext fields
Happy Coding..!
Its not possible as you are saying. An alternative can be helpful. You should add an interface to your WebView which contains a method which can get value from web page. You have create and to call a javascript function which can pass the value of your textarea to your interface function by using that Interface. I hope you understand.
For example: Add an interface to your Webview like:
mainView.addJavascriptInterface(MyOwnCreatedClassName, "AndroidInterface");
Here MyOwnCreatedClassName is the name of the class which will contain that method and AndroidInterface is the name that the Javascript will use to call that method.
for complete example follow:
Using webView.AddJavascriptInterface with MonoDroid
I am displaying a html in a custom activity with a webview. I am overriding
public boolean shouldOverrideUrlLoading(WebView view, String url) {
to intercept certain urls. Now I would like to be able to figure out some more details about the html around that url.
In my case I would like to get the html element that contains the url. E.g. if the url comes from
Manfred Moser
I would like to be able to somehow retrieve the value "Manfred Moser" from within the anchor tag. Is there a way to do that. I found that you do not have access to the DOM from Java.
One way I can think of would be to download the page separately and parse it all before even loading it in webview. However that is an ugly hack at best. Is there a better way?
There is a dirty hack:
Bind some Java object so that it can be called from Javascript with WebView:
addJavascriptInterface(javaObjectExposed, "JSname")
Force execute javascript within an existing page by
WebView.loadUrl("javascript:window.JSname.passData("some data");");
Described here: http://lexandera.com/2009/01/extracting-html-from-a-webview/
Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.
In addition to #Peter Knego's approach, there is the inverse:
Use addJavascriptInterface(), per his step #1
For your link, use an onClick attribute on your <a> tag to call out some method on the Java object from step #1, where the Java object turns around and loads the URL into the WebView, and supplies whatever sort of identifying information you want.
Suffice it to say, there's no way to get the information you want from shouldOverrideUrlLoading().