(Chromium для Android Studio) Как использовать addPossblyUnsafeJavascriptInterface? - PullRequest
0 голосов
/ 03 мая 2018

В ChromeActivity.Java я настроил некоторый код, но он не работает как addJavascriptInterface webview, который я сделал:

import org.chromium.content.browser.JavascriptInterface;
@SuppressLint("JavascriptInterface")

@Override
public void createContextualSearchTab(String searchUrl) {
    Tab currentTab = getActivityTab();
    if (currentTab == null) return;

    Class<? extends Annotation> requiredAnnotation = JavascriptInterface.class;
    currentTab.getWebContents().addPossiblyUnsafeJavascriptInterface(new MyJavaScriptInterface(this), "MyJS", requiredAnnotation);

    TabCreator tabCreator = getTabCreator(currentTab.isIncognito());
    if (tabCreator == null) return;


    tabCreator.createNewTab(
            new LoadUrlParams(searchUrl, PageTransition.LINK),
            TabModel.TabLaunchType.FROM_LINK, currentTab);
}

public class MyJavaScriptInterface {

    ChromeActivity context;

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

    @JavascriptInterface
    @SuppressWarnings("unused")
    public void closeApp(){
        this.context._closeApp();
    }
}

Chromium для Android Studio Исходный код: https://github.com/kuoruan/Chromium-Android

1 Ответ

0 голосов
/ 04 мая 2018

Начиная с Chromium 65, addPossblyUnsafeJavascriptInterface был изменен на addPossblyUnsafeInterface

mTabModelSelectorTabObserver = new TabModelSelectorTabObserver(mTabModelSelector) {
        @Override
        public void onShown(Tab tab) {
            setStatusBarColor(tab, tab.getThemeColor());
            JavascriptInjector ji = JavascriptInjector.fromWebContents(tab.getWebContents());
            ji.setAllowInspection(true);
            ji.addPossiblyUnsafeInterface(new MyJavaScriptInterface(ChromeActivity.this), "BEEB", JavascriptInterface.class);

        }
}


public class MyJavaScriptInterface {

    ChromeActivity context;

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

    @JavascriptInterface
    public void closeApp(){
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, "ccccccccccccccccccccccccccccccc", duration);
        toast.show();
    }
}
...