1.0.0 • Published 3 years ago

sdbridge-android v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

sdbridge-android

TypeScript or javascript mount WebViewJavascriptBridge to window by self.


npm install sdbridge-android

Usage:

import { addBridgeForAndroidWebView } from 'sdbridge-android';
//@ts-ignore
let bridge = window.WebViewJavascriptBridge;
if (!bridge) {
    console.log("window.WebViewJavascriptBridge 没有挂载成功!!,TS正在尝试挂载");
    console.log("TypeScript正在尝试挂载");
    addBridgeForAndroidWebView();
    //@ts-ignore
    bridge = window.WebViewJavascriptBridge;
    if (bridge){
        console.log("window.WebViewJavascriptBridge, 已经被TypeScript挂载成功!!");
    }
} else {
    console.log("window.WebViewJavascriptBridge 挂载成功!!");
}

Why android webView sometimes need TypeScript or javascript mount WebViewJavascriptBridge to window by self ?

Because when you use window.WebViewJavascriptBridge in android webView,but window.WebViewJavascriptBridge did not finish loaded!!!

On the other hand iOS WKWebView have webView?.configuration.userContentController.addUserScript(...) And WKUserScriptInjectionTimeAtDocumentStart this mean:

WKWebView can inject the script after the document element has been created, but before any other content has been loaded.

License

MIT License (c) 2020

See LICENSE file.