Android 上存在 stringByEvaluatingJavaScriptFromString

发布于 2024-11-16 18:17:43 字数 131 浏览 3 评论 0原文

Android 中是否存在像 Iphone 一样的 stringByEvaluatingJavaScriptFromString 方法? 不能使用像 javascript:wave() 这样的简单代码。但具有复杂的Java Script功能。 谢谢

Exist stringByEvaluatingJavaScriptFromString method in Android like Iphone?.
Not with a simple code like this javascript:wave(). But with a complex Java Script function.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-11-23 18:17:43

您可以使用loadUrl来代替,例如:

webView.loadUrl("javascript:PerformSimpleCalculation()");

它的效果类似于stringByEvaluatingJavaScriptFromString。API描述不清楚。

You can use loadUrl instead,eg:

webView.loadUrl("javascript:PerformSimpleCalculation()");

It's effect similar to stringByEvaluatingJavaScriptFromString.The API description is not clear.

双手揣兜 2024-11-23 18:17:43

stringByEvaluatingJavaScriptFromString 是 Android API 中的私有方法。但它确实很有用。

您可以通过 Java 反射检索此 API:

Method stringByEvaluatingJavaScriptFromString , sendMessageMethod;;
    Object webViewCore , browserFrame;
private boolean hasIntercepted = true;
    Object webViewObject = this;
    Class webViewClass = WebView.class;
    try {
        Field mp = webViewClass.getDeclaredField("mProvider");
        mp.setAccessible(true);
        webViewObject = mp.get(this);
        webViewClass = webViewObject.getClass();
        Field wc = webViewClass.getDeclaredField("mWebViewCore");
        wc.setAccessible(true);
        webViewCore = wc.get(webViewObject);
        if (webViewCore != null) {
            sendMessageMethod = webViewCore.getClass().getDeclaredMethod("sendMessage", Message.class);
            sendMessageMethod.setAccessible(true);

            Field bf= webViewCore.getClass().getDeclaredField("mBrowserFrame");
            bf.setAccessible(true);
            browserFrame = bf.get(webViewCore);
            stringByEvaluatingJavaScriptFromString = browserFrame.getClass().getDeclaredMethod("stringByEvaluatingJavaScriptFromString", String.class);
            stringByEvaluatingJavaScriptFromString.setAccessible(true);   

        }
        hasIntercepted = true;
    } catch (Throwable e) {
        hasIntercepted = false;
    }

然后使用此方法:

Object argument =(Object) ((String)"window.hitTest("+event.getX()+ ","+event.getY()+")");
    try {
        Object _tagName = stringByEvaluatingJavaScriptFromString.invoke(browserFrame, argument);
        String tagName =(String)_tagName;
    } catch (Exception e) {
        e.printStackTrace();
    }

stringByEvaluatingJavaScriptFromString is a private method in Android API. But it is really useful.

You could retrive this API via Java reflection:

Method stringByEvaluatingJavaScriptFromString , sendMessageMethod;;
    Object webViewCore , browserFrame;
private boolean hasIntercepted = true;
    Object webViewObject = this;
    Class webViewClass = WebView.class;
    try {
        Field mp = webViewClass.getDeclaredField("mProvider");
        mp.setAccessible(true);
        webViewObject = mp.get(this);
        webViewClass = webViewObject.getClass();
        Field wc = webViewClass.getDeclaredField("mWebViewCore");
        wc.setAccessible(true);
        webViewCore = wc.get(webViewObject);
        if (webViewCore != null) {
            sendMessageMethod = webViewCore.getClass().getDeclaredMethod("sendMessage", Message.class);
            sendMessageMethod.setAccessible(true);

            Field bf= webViewCore.getClass().getDeclaredField("mBrowserFrame");
            bf.setAccessible(true);
            browserFrame = bf.get(webViewCore);
            stringByEvaluatingJavaScriptFromString = browserFrame.getClass().getDeclaredMethod("stringByEvaluatingJavaScriptFromString", String.class);
            stringByEvaluatingJavaScriptFromString.setAccessible(true);   

        }
        hasIntercepted = true;
    } catch (Throwable e) {
        hasIntercepted = false;
    }

Then to use this method:

Object argument =(Object) ((String)"window.hitTest("+event.getX()+ ","+event.getY()+")");
    try {
        Object _tagName = stringByEvaluatingJavaScriptFromString.invoke(browserFrame, argument);
        String tagName =(String)_tagName;
    } catch (Exception e) {
        e.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文