Android WebView Compile a Form and submit with Javascript

2024/2/27 7:53:27

I'm trying to complete this form :

I was able to insert value to the two textbox but the button doesn't work.

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home_page);final String user = getIntent().getStringExtra("username");final String psw = getIntent().getStringExtra("password");MyWebView view = new MyWebView(this);view.getSettings().setJavaScriptEnabled(true);view.getSettings().setDomStorageEnabled(true);view.loadUrl("");view.setWebViewClient(new WebViewClient() {@Overridepublic boolean shouldOverrideUrlLoading(WebView v, String url) {v.loadUrl(url);return true;}@Overridepublic void onPageFinished(WebView v, String url) {v.loadUrl("javascript:" +"var y = document.getElementsByName('login')[0].value='"+user+"';" +"var x = document.getElementsByName('password')[0].value='"+psw+"';");
}class MyWebView extends WebView {Context context;public MyWebView(Context context) {super(context);this.context = context;}

The following code doesn't work :
"var k = document.getElementByTagName('form')[0].submit();"
"var k = document.getElementByName('newlogin')[0].submit();"

 @Overridepublic void onPageFinished(WebView view, String url) {super.onPageFinished(view, url);if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {view.evaluateJavascript("javascript:document.getElementById('username').value ='" + strUsername + "';javascript:document.getElementById('password').value = '" + strPassword + "';javascript:document.getElementById('loginButton').click();", null);} else {view.loadUrl("javascript:document.getElementById('username').value = '" + strUsername + "';javascript:document.getElementById('password').value = '" + strPassword + "';javascript:document.getElementById('loginButton').click();");}}

Related Q&A

Is it safe to call $(document).ready() from inside a function?

If I use the $(document).ready() handler from within a function, will it still guarantee that the code inside it will only be run if the document is ready, even if the document ready event has occurred…

array.push(element) vs array[array.length] = element [duplicate]

This question already has answers here:How to append something to an array?(30 answers)Why is array.push sometimes faster than array[n] = value?(5 answers)Closed 10 years ago.I was wondering if there…

Passing local variable with name of a global variable isnt possible in JS?

foo = "foobar"; var bar = function(){var foo = foo || "";return foo; } bar();`This code gives a result empty string. Why cannot JS reassign a local variable with same name as a glob…

Convert Node crypto aes-256-cbc to CryptoJS

How to convert the following Nodes built-in crypto module encryption to CryptoJS? const crypto = require(crypto);const pass = some,password:)with>spec(chars* const cipher1 = crypto.createCipher(aes…

Can grpc and express server run by same nodejs server, or grpc has to be different server

I am trying to create a REST server which will be based on Node/Express. How to add a GRPC server in the same REST server, or it has to be completely different NodeJS server which will host only the GR…

How to trim content of element and put ... if the characters go over a certain limit?

I would like to trim a part of the <td> if it is too long. This will make sure the table doesnt get messed up. All the data in the following table is retrieved from a database. On the "Subje…

Gremlin, javascript: where is the function valueMap() imported from?

I am using es6 on nodejs, and am trying to execute the project() step in a gremlin query.As part of the projection, I want to extract the properties.Using gremlin console I would use valueMap() to get …

How do I disable scripts in my iframe? [duplicate]

This question already has answers here:Disable JavaScript in iframe/div(4 answers)Closed 10 years ago.I have an iframe that loads a page that I would like to disable the javascripts loading in the ifra…

Attribute selectors, JavaScript and IE8

Im attempting to use attribute selectors and CSS for formatting elements.The HTML looks like:<div id="user" highlight="false">User Name</div>The CSS is:[highlight=true] …

How to set, change or block timezone(systemtime) with Puppeteer?

OS: Windows 10 Pro x64-based PC node -v: v8.12.0 npm list puppeteer: `-- [email protected]I tried this:puppeteer.launch({env: {TZ: Australia/Melbourne,...process.env} });Dont work for me. see…