0

Hey im building Chromium for Android, and I'm trying to figure out why Javascript Interface isnt allowing me to inject javascript within a webpage within Chrome Activity Class.. Even when I call tab.getWebContents().evaluateJavaScript() seperately, the browser freezes. Is there any way to achieve this successfully? Below is a snippet of my code

  import org.chromium.content.browser.JavascriptInterface;

  // Chrome Activity
  public abstract class ChromeActivity<C extends ChromeActivityComponent>
  ....

   @Override
   public void onDidFinishNavigation(Tab tab, String url, boolean isInMainFrame,
                                          boolean isErrorPage, boolean hasCommitted, boolean isSameDocument,
                                          boolean isFragmentNavigation, @Nullable Integer pageTransition, int errorCode,
                                          int httpStatusCode)
              { 

                    JavascriptInjector ji = JavascriptInjector.fromWebContents(tab.getWebContents());
                    tab.getWebContents().evaluateJavaScript("alert("Hello World");",null);
                    ji.setAllowInspection(true);

                    ji.addPossiblyUnsafeInterface(new MyJavaScriptInterface(ChromeActivity.this), "JS", JavascriptInterface.class);
             }


 class MyJavaScriptInterface {

     ChromeActivity context;
     public MyJavaScriptInterface(ChromeActivity activity) {
     this.context = activity;
  }

  @JavascriptInterface
  public void app(){
  int duration = Toast.LENGTH_SHORT;
  Toast toast = Toast.makeText(this.context, "Test", duration);
  toast.show();
  }
}
T Shoats
  • 347
  • 2
  • 4
  • 19
  • What happens if instead of a Toast or alert you used a console.log? – Morrison Chang Mar 13 '21 at 05:34
  • @MorrisonChang whenever I add tab.getWebContents().evaluateJavaScript() within the onDidFinishNavigation() method the browser crashes ands shuts down. – T Shoats Mar 13 '21 at 06:01
  • @MorrisonChang even when I try to add tab.getWebContents.evaluateJavascript() within onPageLoadFinished,the browser starts but crashes. This happens even when I use console.log(), or any type of javascript – T Shoats Mar 13 '21 at 06:04

0 Answers0