1

CordovaWebViewに外部JavaScriptを注入できるCordovaプラグインを開発しようとしています。しかし、私は成功することができません。WebView JavascriptインジェクションがCordovaプラグインで動作しません

私はMainActivityでCordovaActivityを拡張して同じことを試しましたが、これはうまく動作します。

Heres私がやったこと。

WebAppInterfaceクラス

private class WebAppInterface { 

    private final Context context; 

    WebAppInterface(Context context) { 
     this.context = context; 
    } 

    @JavascriptInterface 
    public void showToast(String toastMsg) { 
     Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show(); 
    } 
} 

インジェクションコード

WebSettings webSettings = ((WebView)appView.getEngine.getView()).getSettings(); 
    webSettings.setJavaScriptEnabled(true); 

    myWebView.addJavascriptInterface(new WebAppInterface(webView.getContext()), "android"); 

    myWebView.loadUrl("javascript:android.showToast('Toast 1')"); 

はコルドバに来て、私はCordovaPlugin拡張MyPluginと呼ばれるプラグインを作って、メソッドを実行overided。

@Override 
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 

    if ("showLog".equals(action)) { 
     Log.d("showLog", "showLog"); 
     return true; 
    } 

    if ("showToast".equals(action)) { 

     cordova.getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

       WebView webView = (WebView) UnfoldPlugin.this.webView.getEngine().getView(); 
       webView.getSettings().setJavaScriptEnabled(true); 
       webView.addJavascriptInterface(new WebAppInterface(webView.getContext()), "android"); 

       webView.loadUrl("javascript:android.showToast('Hello World')"); 
      } 
     }); 

     return true; 
    } 

    return false; 
} 

私はcordova.execメソッドを呼び出すだけでなくUnfoldPlugin.jsを書かれています。アクション "showLog"と "showToast"の両方が呼び出されますが、javascriptインジェクションを介してトーストを取得することはできません。

これは、プラグインを介して外部のJavaScriptの注入を許可しないCordovaの問題ですか?

どうすればこの問題を解決できますか?助けて。

編集

しかしジャバスクリプト:のwindow.alert( 'Hello Worldのは')私はaddJavascriptInterfaceはコルドバプラグインで機能していないと思う

@Override 
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 

    if ("showToast".equals(action)) { 

     cordova.getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       webView.loadUrl("javascript:window.alert('Hello World')"); 
      } 
     }); 
     callbackContext.success(); 
     return true; 
    } 

に動作します。

+0

受け入れられた回答はあなたの助けになるでしょうか? http://stackoverflow.com/questions/40974950/custom-cordova-plugin-creation-for-ionic2-project – Stallion

+0

役に立たなかった –

答えて

0

数時間の研究の後、私は答えを見つけました。それが誰かを助けるかもしれないことを望みます。

addJavascriptInterfaceメソッドを初期化内で呼び出す必要があります。

public void initialize(CordovaInterface cordova, CordovaWebView webView) { 
     super.initialize(cordova, webView); 
     WebView webView = (WebView) UnfoldPlugin.this.webView.getEngine().getView(); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.addJavascriptInterface(new WebAppInterface(webView.getContext()), "android"); 
    } 

次に、任意のjavascriptタグを使用したwebView.loadUrl()を使用できます。

関連する問題