美文网首页
通过注入js代码实现

通过注入js代码实现

作者: 952625a28d0d | 来源:发表于2016-06-23 22:23 被阅读1330次
    • JS代码
    
    // 创建JSBridge对象
    var JSBridge = {};
    var callbackPoll = {};
    (function initialize() { 
        // call方法
        JSBridge.call = function(func, param, callback) {
            if ('string' !== typeof func) {
                return; }
            if ('function' === typeof param) { callback = param;
                param = null; } else if (typeof param !== 'object') { param = null; }
            var clientId = '' + new Date().getTime() + (Math.random());
            if ('function' === typeof callback) { callbackPoll[clientId] = callback; }
            // 组装json数据 发送给native
            var invokeMsg = JSON.stringify({ func: func, param: param, msgType: 'call', clientId: clientId });
            var url = "http://xiaoying/clickevent?param=" + invokeMsg;
            // 通过改变document.location来实现数据传递 iOS端在startLoad截取解析
            document.location = url;
            return url; };
            // 回调方法 传入clientId param
        JSBridge.callback = function(clientId, param) {
            var invokeMsg = JSON.stringify({ clientId: clientId, param: param });
            var url = "http://xiaoying/clickevent?param=" + invokeMsg;
            document.location = url;
            return url; };
        JSBridge.trigger = function(name, param, clientId) { console.log('bridge.trigger ' + name);
            if (name) {
                var evt = document.createEvent('Events');
                evt.initEvent(name, false, true);
                if (typeof param === 'object') {
                    for (var k in param) { evt[k] = param[k]; } }
                evt.clientId = clientId;
                var prevent = !document.dispatchEvent(evt);
                if (clientId && name === 'back') { JSBridge.callback(clientId, { prevent: prevent }); } } };
            // native端调用此方法发送数据即可
        JSBridge._invokeJS = function(resp) { console.log(resp);
            resp = JSON.parse(resp);
            console.log("invokeJS msgType " + resp.msgType + " func " + resp.func);
            console.log('parse end.');
            if (resp.msgType === 'callback') {
                var func = callbackPoll[resp.clientId];
                if (!(typeof resp.keepCallback == 'boolean' && resp.keepCallback)) { delete callbackPoll[resp.clientId]; }
                if ('function' === typeof func) { setTimeout(function() { func(resp.param); }, 1); } } else if (resp.msgType === 'call') { resp.func && this.trigger(resp.func, resp.param, resp.clientId); } };
        JSBridge.startupParams = '{startupParams}';
        var readyEvent = document.createEvent('Events');
        readyEvent.initEvent('JSBridgeReady', false, false);
        var docAddEventListener = document.addEventListener;
    
        // js添加监听方法  name func即可  native传递数据即可
        document.addEventListener = function(name, func) {
            if (name === readyEvent.type) { setTimeout(function() { func(readyEvent); }, 1); } else { docAddEventListener.apply(document, arguments); } };
        document.dispatchEvent(readyEvent); 
    })();```
    
    - 注入
    
    
    • (void)webViewDidFinishLoad:(UIWebView *)webView{
      // 注入JS代码
      [self addJSClickEvent];
      }```

    • 移动端截取

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
        
        NSLog(@"shouldStartLoadWithRequest in:%@",request.URL.absoluteString);
        NSString *url = request.URL.absoluteString;
        
        NSString *dataGBK = [url stringByRemovingPercentEncoding];
        
        if(url != nil && [url hasPrefix:JS_STRING_URL_PRFIX])
        {
            if(dataGBK != nil && dataGBK.length > JS_STRING_URL_PRFIX.length)
            {
                // 获取json数据
                NSString *param = [dataGBK substringFromIndex:JS_STRING_URL_PRFIX.length];
                NSLog(@"param:%@",param);
                
                // 转为NSDictionary H5Container模型
                NSDictionary *dic = [XYH5ContainerTool objectFromJSONStringWithStr:param];
                if (!self.h5Container) {
                    self.h5Container = [[H5Container alloc] init];
                    self.h5Container.keepCallback = NO;    // default is YES;
                }
                
                [self.h5Container setValuesForKeysWithDictionary:dic];
                NSLog(@"url传递过来数据信息为%@",dic);
                
                // 解析
                [self parseWithH5Container:self.h5Container];
                
            }
            return NO;
        }
        return YES;
    }```
    
    - 处理
    
    
    • (void)parseWithH5Container:(H5Container *)h5Container{
      if ([h5Container.func isEqualToString:XYJS_CALL_POP_WINDOW]) {

        [self popWindowWithParam:h5Container.param];
      

      }
      }```

    #pragma mark - pop window
    - (void)popWindowWithParam:(NSDictionary *)param{
        
        [self.navigationController popViewControllerAnimated:YES];
    }```

    相关文章

      网友评论

          本文标题:通过注入js代码实现

          本文链接:https://www.haomeiwen.com/subject/qylodttx.html