将外部javascript函数应用于android

时间:2014-06-07 12:30:20

标签: javascript android

我正在开发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;
}

非常感谢。

2 个答案:

答案 0 :(得分:1)

正如here所述,您需要实现一个JavaScript界面​​,将其附加到您的网页浏览,然后您可以从网页HTML中调用它的功能。

E.g。

在你的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>

答案 1 :(得分:0)

为javascript listner添加Class

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");

现在,创建javascript并将其加载到webview

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()方法中将你的文本作为Toa​​st。

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