package com.bytedance.sdk.component.YFl;

import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Base64;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
/* loaded from: classes2.dex */
public class UZM extends YFl {
    static final /* synthetic */ boolean nc = true;
    protected WebView NjR;
    protected String qsH;

    @Override // com.bytedance.sdk.component.YFl.YFl
    protected Context YFl(nc ncVar) {
        if (ncVar.wN != null) {
            return ncVar.wN;
        }
        if (ncVar.YFl != null) {
            return ncVar.YFl.getContext();
        }
        throw new IllegalStateException("WebView cannot be null!");
    }

    @Override // com.bytedance.sdk.component.YFl.YFl
    protected String YFl() {
        return this.NjR.getUrl();
    }

    @Override // com.bytedance.sdk.component.YFl.YFl
    protected void Sg(nc ncVar) {
        this.NjR = ncVar.YFl;
        this.qsH = ncVar.tN;
        if (ncVar.pDU) {
            return;
        }
        tN();
    }

    protected void tN() {
        if (!nc && this.NjR == null) {
            throw new AssertionError();
        }
        this.NjR.addJavascriptInterface(this, this.qsH);
    }

    @Override // com.bytedance.sdk.component.YFl.YFl
    @JavascriptInterface
    public void invokeMethod(String str) {
        super.invokeMethod(str);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.bytedance.sdk.component.YFl.YFl
    public void Sg() {
        super.Sg();
        AlY();
    }

    protected void AlY() {
        this.NjR.removeJavascriptInterface(this.qsH);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.bytedance.sdk.component.YFl.YFl
    public void YFl(String str, rkt rktVar) {
        if (rktVar != null && !TextUtils.isEmpty(rktVar.qsH)) {
            String str2 = rktVar.qsH;
            YFl(str, String.format("javascript:(function(){   const iframe = document.querySelector(atob('%s'));   if (iframe && iframe.contentWindow) {        iframe.contentWindow.postMessage(%s, atob('%s'));   }})()", Base64.encodeToString(String.format("iframe[src=\"%s\"", str2).getBytes(), 2), str, Base64.encodeToString(str2.getBytes(), 2)));
            return;
        }
        super.YFl(str, rktVar);
    }

    @Override // com.bytedance.sdk.component.YFl.YFl
    protected void YFl(String str) {
        YFl(str, "javascript:" + this.qsH + "._handleMessageFromToutiao(" + str + ")");
    }

    private void YFl(String str, final String str2) {
        if (this.vc || TextUtils.isEmpty(str2)) {
            return;
        }
        Runnable runnable = new Runnable() { // from class: com.bytedance.sdk.component.YFl.UZM.1
            @Override // java.lang.Runnable
            public void run() {
                if (UZM.this.vc) {
                    return;
                }
                try {
                    UZM.this.NjR.evaluateJavascript(str2, null);
                } catch (Throwable unused) {
                }
            }
        };
        if (Looper.myLooper() != Looper.getMainLooper()) {
            this.AlY.post(runnable);
        } else {
            runnable.run();
        }
    }
}
