android WebView 注入js 几种方式

2020-08-16 16:09:30

参考地址 android WebView 注入js 几种方式

有时我们开发中需要将js 注入到我们本地,有可能你会说,放在Web不就可以了吗,的确,但是需求就是这样的

通过流的形式注入

 @SuppressLint("ObsoleteSdkInt")
    public void onJsLocal() {
        StringBuilder builder = new StringBuilder(getJS(this, "qqq.js"));
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            agentWeb.getWebCreator().getWebView() .loadUrl("javascript:" + builder.toString());
        } else {
            agentWeb.getWebCreator().getWebView().evaluateJavascript(builder.toString(), new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Log.i("onReceiveValue", value);
                }
            });
        } 
    }

第二种 通过文件加载

view.loadUrl("javascript:(function() { "
                                + " " //turns to red the background color
                                + " var script=document.createElement('script'); "
                                + " script.setAttribute('type','text/javascript'); "
                                + " script.setAttribute('src', 'http://192.168.12.2/assets/mathjax/jsBridge.js'); "
                                + " script.onload = function(){ "
                                + "  console.log('aaaaaaaa' ) ; "
                                + " }; "
                                + " document.getElementsByTagName('head')[0].appendChild(script); "
                                + "})()");

第三种,第二种有可能存在加载不成功的情况,我们可以拦截

 Map<String, String> map = new HashMap<>();
                                map.put("Access-Control-Allow-Origin", "*");
                                map.put("Access-Control-Allow-Headers", "Content-Type");
                                WebResourceResponse resourceResponse = new WebResourceResponse("application/javascript",
                                        "UTF-8", 200, "OK", map,
                                        getApplication().getAssets().open(JS_FILENAME));

注意由于注入是一个异步的操作,所以和js交互的过程有可能存在不顺畅的问题

可以采用

view.postDelay ...


  • 2021-01-22 21:59:34

    emcc编译与部分重要参数选取

    C/C++代码通过emcc编译为字节码,然后根据不同的目标编译为asm.js或wasm。emcc和gcc编译选项类似,例如-s OPTIONS=VALUE、-O等。另外为了适应Web环境,emcc增加了一些特有的选项,如–pre-js 、–post-js 等。

  • 2021-01-22 22:01:19

    Emscripten Compiler Frontend (emcc)

    The Emscripten Compiler Frontend (emcc) is used to call the Emscripten compiler from the command line. It is effectively a drop-in replacement for a standard compiler like gcc or clang.

  • 2021-01-22 22:21:41

    emcc编译命令介绍

    这个输入文件file,既可以是clang可以编译的C/C++语言,也可以是二进制形式的llvm bitcode或者人类可读形式的llvm assembly文件。

  • 2021-01-22 22:25:51

    How to protect your JS code by WebAssembly

    对于iOS或是Android来说,我们可以将相关的算法通过C/C++进行编写,然后编译为dylib或是so并进行混淆以此来增加破解的复杂度,但是对于前端来说,并没有类似的技术可以使用。当然,自从asm.js及WebAssembly的全面推进后,我们可以使用其进一步增强我们核心代码的安全性,但由于asm.js以及WebAssembly标准的开放,其安全强度也并非想象中的那么美好。

  • 2021-01-24 09:50:16

    UICollectionViewCell cell高度自适应

    本来想使用UICollectionView来作为整体的布局,并且不再使用UITableView,但是发现高度不固定的布局,UICollectionView没啥优势呀,至少我没找到好的方法,从网上看的是,要自定义cell,并且继承preferredLayoutAttributesFittingAttributes