wendux / DSBridge-IOS

:earth_asia: A modern cross-platform JavaScript bridge, through which you can invoke each other's functions synchronously or asynchronously between JavaScript and native.
1.96k stars 305 forks source link

求INIT_SCRIPT源码 #11

Closed yanue closed 7 years ago

wendux commented 7 years ago
/**
 * Created by du on 17/1/1.
 */
function getJsBridge() {
    window._dsf=window._dsf||{};
    return {
        call: function (method, args, cb) {
            var ret = '';
            if (typeof args == 'function') {
                cb = args;
                args = {};
            }
            if (typeof cb == 'function') {
                window.dscb = window.dscb || 0;
                var cbName = 'dscb' + window.dscb++;
                window[cbName] = cb;
                args['_dscbstub'] = cbName;
            }
            args = JSON.stringify(args || {})

            if (window._dswk) {
                ret = prompt(window._dswk + method, args);
            } else {
                if (typeof _dsbridge == 'function') {
                    ret = _dsbridge(method, args);

                } else {
                    ret = _dsbridge.call(method, args);
                }
            }
            return ret;
        },
        register:function(name,fun){
            if(typeof name=="object"){
                Object.assign(window._dsf,name)
            }else {
                window._dsf[name] = fun;
            }
        }
    }
};
dsBridge=getJsBridge();