0

嘿,我正在为 Android 构建 Chromium,我试图弄清楚为什么 Javascript 接口不允许我在 Chrome 活动类中的网页中注入 javascript。即使我单独调用 tab.getWebContents().evaluateJavaScript(),浏览器冻结。有什么方法可以成功实现这一目标吗?下面是我的代码片段

  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();
  }
}
4

0 回答 0