gpt4 book ai didi

javascript - 如何在 WebView 中单击按钮时获取值?

转载 作者:行者123 更新时间:2023-11-30 00:23:58 24 4
gpt4 key购买 nike

我有一个 native android 应用程序,它进入 WebView 以进行结帐过程。我正在实现 appsflyer 以通过应用程序跟踪收入。如何检测点击了页面上的哪个按钮,以及商品的收入价格是多少?

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.appsflyer.AFInAppEventParameterName;
import com.appsflyer.AFInAppEventType;
import com.appsflyer.AppsFlyerLib;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class EventActivity extends AppCompatActivity {

ProgressDialog pDialog;
Boolean redirecting = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);

String appsFlyerUID = AppCore.getInstance(getApplicationContext()).getAppsFlyerUID();
String idfa = AppCore.getInstance(getApplicationContext()).getMixpanelAdvertisingIdentifier();

Intent intent = getIntent();

try {
JSONObject event = new JSONObject(intent.getStringExtra("event"));

String eventId = event.getString("id");
String title = event.getString("title");
String startsAt = event.getString("starts_at");
String venue = event.getString("venue");
String city = event.getString("city");
String state = event.getString("state");

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
Date newDate = format.parse(startsAt);

format = new SimpleDateFormat("EEEE, MMMM d @ h:mm a");
String date = format.format(newDate);

((TextView)findViewById(R.id.event_title)).setText(title);
((TextView)findViewById(R.id.event_date)).setText(date);
((TextView)findViewById(R.id.event_venue)).setText(venue + " - " + city + ", " + state);

String url = "http://tlapi.ticketliquidator.com/event_map/" + eventId + "?utm_campaign=" + idfa + "&utm_term=" + appsFlyerUID;


Map<String, Object> eventValue = new HashMap<String, Object>();
AppsFlyerLib.getInstance().trackEvent(getApplicationContext(), "View Event " + eventId,eventValue);


final WebView wv = (WebView) findViewById(R.id.event_webview);
wv.getSettings().setJavaScriptEnabled(true);
final Activity self = this;

wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
redirecting = true;
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if(redirecting){
pDialog.dismiss();
}
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(redirecting){
pDialog = new ProgressDialog(self);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage(" Loading...");
pDialog.setCancelable(true);
pDialog.show();
}

}
});


pDialog = new ProgressDialog(this);
pDialog.setMessage("Getting event...");
pDialog.show();

wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {

if(progress == 100)
pDialog.dismiss();
}
});

wv.loadUrl(url);
} catch (Exception e) {
e.printStackTrace();
}


}
}

这是我的 WebView android 类。我正在尝试从此网页中提取信息...

Api WebView

我只需要知道在 WebView 中单击了哪个按钮,并获取该按钮旁边的价格。然后我会将其发送回 appsflyer 进行跟踪。

最佳答案

你必须用java脚本来欺骗它,因为在加载页面之后你必须像这样将你的javascript注入(inject)到加载的网页中

@Override
public void onPageFinished(WebView view, String url){
// your javascript as string which works fine in scratchpad
String javaScript ="javascript:(function() {alert();})()";
webview.loadUrl(javaScript);
}

这样您就可以注入(inject) javascript 并根据您的要求找到相应的 DOM,并在此基础上调用 java 方法,如 this .

关于javascript - 如何在 WebView 中单击按钮时获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com