Alright, so I'm working on an app that will fill out forms. I have no idea how to do this, other than the fact that I need to use Javascript for this. This code loads the web page, but does not input any values to the form.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
WebView myWebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
WebView myWebview = (WebView) findViewById(R.id.webview);
//hide loading image
findViewById(R.id.progress).setVisibility(View.GONE);
//show WebView
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
});
myWebview.loadUrl("https://www.panerabread.com/en-us/mypanera/registration-page.html");
myWebview.loadUrl("javascript:document.getElementById(\"join_first_name\").value=\"Hello World\";");
}
Below is my second class. Not sure what this does, I got it from the documentation though.
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
}
Try putting the loadUrl("javascript..." into the onPageFinished listener. You're calling it before the page is loaded.
Related
I tried to view http://artikelweb.com in a webview. The web page appears nicely. But, whenever I go to any author link from "Popular Authors" section, the web page appears, but, after loading, the quotes aren't showing.
In Google Chrome Browser(mobile) the quotes appear after loading,
but, in my app, the quotes are not showing in web view.
Code Snippet:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.artikelweb.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
In your particular case you must enable the DOM Storage API
webSettings.setDomStorageEnabled(true);
So your code must become:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
myWebView.loadUrl("http://www.artikelweb.com");
myWebView.setWebViewClient(new WebViewClient());
}
That becouse the javascript that use the website you are accessing needs it in its javascript.
Of course you could take advantage of a third party library, but again you won't know why this exact case works in the third party one and not in the default webview.
The library you are using has this initial settings:
https://raw.githubusercontent.com/delight-im/Android-AdvancedWebView/master/Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java
final WebSettings webSettings = getSettings();
webSettings.setAllowFileAccess(false);
setAllowAccessFromFileUrls(webSettings, false);
webSettings.setBuiltInZoomControls(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT < 18) {
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
}
webSettings.setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < 19) {
webSettings.setDatabasePath(databaseDir);
}
setMixedContentAllowed(webSettings, true);
setThirdPartyCookiesEnabled(true);
Enabling by default many options that the default webview has disable for security reason.
Set UserAgent to your Webview settings and try
webSettings.setUserAgentString("Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>");
I am developing an Android app and want to fill a form in a WebView automatically using JavaScript.
final WebView webView = (WebView) findViewById(R.id.MyWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:document.getElementById('tbBenutzer').value = 'myUser';");
webView.loadUrl("javascript:document.getElementById('tbKennwort').value = 'myPwd';");
webView.loadUrl("javascript:document.getElementById('form1').submit();");
}
});
webView.loadUrl(my_url);
The website loads fine and I can see it for a short time, but instead of filling the Input, it replaces the whole content of the WebView with my username. There is nothing else left.
I don't know why, but after some research I found out, that if I put all of my JavaScript code into an function it works.
final WebView webView = (WebView) findViewById(R.id.MyWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:(function() {document.getElementById('tbBenutzer').value='myUser';}) ();");
webView.loadUrl("javascript:(function() {document.getElementById('tbKennwort').value='myPwd';}) ();");
webView.loadUrl("javascript:(function() {document.getElementById('form1').submit();}) ();");
}
});
webView.loadUrl(my_url);
I am using simple webview.Where i have given link to login page of my web application.
The webview work fine until i try custom webviewClient. By adding webviewClient it stops processing of javascript.I am able to see only html part.
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new MyBrowser());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("my web application login page with jscript");
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
I am able to login successfully.There is javascript in login page,so i think it works.But after logging in it redirects to home page where onpage load javascript will append some data to html table.The data which is appended by jscript is visible in other browser,but webview not showing that data.
Try the below that enables javascript
public class WebViewDemo extends Activity {
private static final String URL_TO_LOAD = "http://google.com";
private static final String LOCAL_RESOURCE = "file:///android_asset/html/HelloWorld.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.browser_demo);
WebView view= (WebView) findViewById(R.id.browser1);
loadResource(view, LOCAL_RESOURCE);
}
private void loadResource(WebView view, String resource) { wv.loadUrl(resource);
view.getSettings().setJavaScriptEnabled(true);
view.setWebChromeClient(new CustomChromeclient());
view.setWebViewClient(new CustomWebViewclient(this));
view.addJavascriptInterface(new JavaScriptInterface(view), "JSI");
} //
I have a WebView in Android, and most things render fine. My Youtube videos render fine, but the ads on them do not show up. I checked and JavaScript is enabled. So I am not sure why the ads do not render.
Here is the code that I use:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
WebSettings webSettings = webview.getSettings();
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webview.setWebChromeClient(new WebChromeClient(){});
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
webview.loadUrl("http://javatester.org/javascript.html");
}
Would anyone know why the AdSense ads do not render, and how I can make it render?
Thanks!
Suppose I load a 3rd party URL through webview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new MyWebChromeClient());
webview.loadUrl("http://ebay.com");
}
Is it possible for me to inject something into this WebView to replace the ebay logo with my own?
To expand on CommonsWare's correct answer:
WebView webview = new WebView();
webview.setWebViewClient(new WebClient());
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("stackoverflow.com");
then in WebClient:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// Obvious next step is: document.forms[0].submit()
view.loadUrl("javascript:document.forms[0].q.value='[android]'");
}
}
In a nutshell, you wait for the page to load. Then you loadUrl("javascript:[your javascript here]").
Not directly. You can invoke Javascript code in the context of the current Web page, via loadUrl(), much like a bookmarklet does. However, you do not have direct access to the DOM from Java code.
Since API level 19 there is a handy function evaluateJavascript (String script, ValueCallback<String> resultCallback) that to me makes more sense to use (but I guess the result will be the same). If you just want to run som javascript in the WebView (to modify the DOM for example) the resultCallback can be left empty.
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
webView.evaluateJavascript(
"const images = document.getElementsByTagName(\"img\");\n" +
"for(var i=0; i<images.length; i++){\n" +
" if(images[i].alt == \"eBay Logo\"){\n" +
" images[i].src = \"my_logo.png\";\n" +
" }\n" +
"}", null);
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://ebay.com");
WebView webview = new WebView();
//Maybe need to set to enabled javascript?
webView.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebClient());
webview.loadUrl("stackoverflow.com");