zoukankan      html  css  js  c++  java
  • webview页面和壳通信的库(精简版)

    // PG精简版
    (function() {
        var PG = {
                iosBridge: null,
                callbackId: 0,
                callbacks: [],
                commandQueue: [],
                commandQueueFlushing: false
            },
            ua = navigator.userAgent,
            isIOS = (ua.indexOf("iPhone") > -1 || ua.indexOf("iPad") > -1 || ua.indexOf("iPod") > -1) ? true : false;
        PG.getAndClearQueuedCommands = function () {
            var commandQueue_json = JSON.stringify(PG.commandQueue);
            PG.commandQueue = [];
            return commandQueue_json;
        };
    
        PG.exec = function(method, callback, args) {
            var callbackId = '';
            if (typeof(callback) == "undefined") {
                callback = null;
            }
            if (typeof(args) == "undefined") {
                args = {};
            }
    
    
            if (callback && typeof(callback) == 'function') {
                callbackId = method + PG.callbackId++;
                PG.callbacks[callbackId] = callback;
            }
    
            var obj = {
                Method: method,
                CallbackId: callbackId,
                Args: args
            };
    
            if (isIOS) {
                if (PG.iosBridge == null) {
                    PG.iosBridge = document.createElement("iframe");
                    PG.iosBridge.setAttribute("style", "display:none;");
                    PG.iosBridge.setAttribute("height", "0px");
                    PG.iosBridge.setAttribute("width", "0px");
                    PG.iosBridge.setAttribute("frameborder", "0");
                    document.documentElement.appendChild(PG.iosBridge);
                }
    
                PG.commandQueue.push(JSON.stringify(obj));
                if (!PG.commandQueueFlushing) {
                    PG.iosBridge.src = 'pg://ready';
                }
                
                
            } else if (window.comjs) {
                // android
                window.comjs.notify('pg://' + encodeURIComponent(JSON.stringify(obj)));
            } else {
                console.log("非ios或android平台,不合适吧");
            }
    
    
        };
    
        PG.callback = function(callbackId, args) {
            if (PG.callbacks[callbackId]) {
                try {
                    var temp = decodeURIComponent(args),
                        obj = JSON.parse(temp);
    
                    PG.callbacks[callbackId](obj);
    
                } catch (e) {
                    console.log("Error in success callback: " + callbackId + " = " + e);
                }
    
                delete PG.callbacks[callbackId];
    
            }
        };
    
        
            
        if (typeof(window) === "object" && typeof(window.document) === "object") {
            window.PG = PG;
        }
            
    
        
    
    })();
  • 相关阅读:
    oracle数据库导出与导入
    Mysql导入表信息[Err] 1067
    Golang--不定参数类型
    (转)Docker容器的重启策略及docker run的--restart选项详解
    (转)Golang--使用iota(常量计数器)
    Golang--匿名变量
    Golang--Hello World
    Ubuntu Server16.04 配置网卡
    U盘安装ubuntu 16.04 遇到 gfxboot.c32:not a COM32R image boot 的解决方法
    ipfs私链服务
  • 原文地址:https://www.cnblogs.com/samwu/p/4600068.html
Copyright © 2011-2022 走看看