如何注入到字符串的Android的WebView [英] How to inject a String into Android WebView
问题描述
我加载一个简单的网页,名为AddSerialNum文本字段
I am loading a simple webpage, with a Text Field named "AddSerialNum"
<form>
Serial Number: <input type="text" name="AddSerialNum">
</form>
然后我试图填充该领域,以字符串我从一个酒吧code扫描Android中得到的结果:
Then I am trying to populate that field, with a string I get from a barcode scan result in Android:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String contents = null;
//Call Clipboard and assign service to var.
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
//Set contents = to scan result
contents = data.getStringExtra("SCAN_RESULT");
//Format scan result
@SuppressWarnings("unused")
String format = data.getStringExtra("SCAN_RESULT_FORMAT");
//Copy Data to clipboard
ClipData clip = ClipData.newPlainText("serial", contents);
clipboard.setPrimaryClip(clip);
//Toast - message states copy was successful with serial number
Toast.makeText(this, "Copied " +contents , Toast.LENGTH_LONG).show();
// Handled a successful scan
//Put the fucking results in the box!
browser.loadUrl("javascript:document.getElementByID('AddSerialNum').value = '"+contents+"';");
} else if (resultCode == RESULT_CANCELED) {
// Cancellation action
}
}
}
在Toast通知来了,酒吧code的序列号复制到剪贴板作为主要素材,但为什么不是在HTML文件中的表格填写?
The toast notification comes up, and the serial number of the barcode is copied to the clipboard as a primary clip, But why is it not filling in the form on the html file?
我觉得我失去了一些东西,但我有疲惫的眼睛,这是令人沮丧的我。任何人都知道怎么帮?
I feel like I am missing something, but I have tired eyes, and this is frustrating me. Anyone know how to help?
在此先感谢!
推荐答案
找到自己的答案。
以下code应该使用。
The following code should be used.
browser.loadUrl("javascript:document.forms[0].AddSerialNum.value = '"+contents+"';");
看来你必须通过节点导航到该领域,而不仅仅是最后一个字段。
It seems that you have to navigate to that field by nodes, rather than just the final field.
这篇关于如何注入到字符串的Android的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!