Javascript 将外部java脚本函数应用于android

Javascript 将外部java脚本函数应用于android,javascript,android,Javascript,Android,我正在开发一个android应用程序,它使用webview控件呈现我的HTML文本。如何在我的android代码中应用此java脚本函数从webview控件中选择和复制文本,但是这是一个外部函数不在我的HTML页面中,并且具有返回值: function getSelectedText() { var txt; if (window.getSelection) { txt = window.getSelection(); } else if (window.document.getSele

我正在开发一个android应用程序,它使用webview控件呈现我的HTML文本。如何在我的android代码中应用此java脚本函数从webview控件中选择和复制文本,但是这是一个外部函数不在我的HTML页面中,并且具有返回值

function getSelectedText() {
var txt;
if (window.getSelection) {
    txt = window.getSelection();
} else if (window.document.getSelection) {
    txt =window.document.getSelection();
} else if (window.document.selection) {
    txt = window.document.selection.createRange().text;
}
 return txt;
}
非常感谢。

如前所述,您需要实现一个JavaScript接口,将其连接到您的webview,然后您可以从您的网页HTML调用它的函数

例如

在你的android代码中

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    @JavascriptInterface
    public void getText(String text) {
        Log.d("JsInterface", text)
        Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
    }
}
在你的网络视图中,像这样附加它

WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "MyJSInterface");
然后在HTML页面中添加JavaScript,如下所示:

<script type="text/javascript">
       function getSelectedText() {
          var txt;
          if (window.getSelection) {
              txt = window.getSelection();
          } else if (window.document.getSelection) {
              txt =window.document.getSelection();
          } else if (window.document.selection) {
              txt = window.document.selection.createRange().text;
          }
          MyJSInterface.getText(txt); // <-- this will be your function in WebAppInterface
        }

        // And add some code to invoke getSelectedText()
        // on the event you are interested in.
</script>

函数getSelectedText(){
var-txt;
if(window.getSelection){
txt=window.getSelection();
}else if(window.document.getSelection){
txt=window.document.getSelection();
}else if(窗口、文档、选择){
txt=window.document.selection.createRange().text;
}

MyJSInterface.getText(txt);//为javascript列表添加类

public class WebAppInterface {
    Context mContext;

/* Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void getText(String text) {
    Log.d("JsInterface", text)
    Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
}
}
将此附加到webview

WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "MyJSInterface");
String js= "(function getSelectedText() {"+
            "var txt;"+
            "if (window.getSelection) {"+
                "txt = window.getSelection().toString();"+
            "} else if (window.document.getSelection) {"+
                "txt = window.document.getSelection().toString();"+
            "} else if (window.document.selection) {"+
                "txt = window.document.selection.createRange().text;"+
            "}"+
            "MyJSInterface.getText(txt);"+
          "})()";
    // calling the js function
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        myWebView.evaluateJavascript("javascript:"+js, null);
    }else{
        myWebView.loadUrl("javascript:"+js);
    }
现在,包装javascript并将其加载到webview

WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this), "MyJSInterface");
String js= "(function getSelectedText() {"+
            "var txt;"+
            "if (window.getSelection) {"+
                "txt = window.getSelection().toString();"+
            "} else if (window.document.getSelection) {"+
                "txt = window.document.getSelection().toString();"+
            "} else if (window.document.selection) {"+
                "txt = window.document.selection.createRange().text;"+
            "}"+
            "MyJSInterface.getText(txt);"+
          "})()";
    // calling the js function
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        myWebView.evaluateJavascript("javascript:"+js, null);
    }else{
        myWebView.loadUrl("javascript:"+js);
    }
运行脚本(上面的代码)后,您将在javascript接口类的getText()方法中以Toast的形式获取文本


感谢source.rar,从source.rar开始了前两个步骤。最后一部分是执行javascript

您是想在网页中获取文本,还是想从网页中获取Java代码?我正在尝试从webview复制所选文本。您是否尝试将javascript接口绑定到代码?如sho所示wn,但这个调用HTML页面内部的内部java脚本不是外部java脚本,请注意,这是我第一次在android应用程序中工作。