How to fire deviceready event in Chrome browser (trying to debug phonegap project)

前端 未结 9 392
太阳男子
太阳男子 2020-12-23 16:09

I\'m developing a PhoneGap application and I\'d like to be able to debug it in Chrome rather than on the phone. However, I do the initialization of my code in an onDeviceRe

相关标签:
9条回答
  • 2020-12-23 16:51

    You simulate events like this:

    const simulateEvent = (eventName, attrs = {customData:"data"}, time = 1000, target = document) => {
        let event = new CustomEvent(eventName, { detail:  attrs });    
        setTimeout(() => {
            target.dispatchEvent(event);
        }, time);
    };
    
    var divReady = document.querySelector('div#ready');
    
    document.addEventListener('deviceready', (e) => {
     console.log("triggered with:", e.detail);
     divReady.innerHTML = `Ready! ${JSON.stringify(e.detail)}`;
    });
    
    simulateEvent('deviceready', {customData:"My custom data goes in event.detail"});
    <div id="ready"> Wait for ready... </div>

    0 讨论(0)
  • 2020-12-23 17:00

    I use Safari for debugging and do this:

    //my standard PG device ready
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
    //debug("onDeviceReady")
    getDefaultPageSetup();
    
    }
    
    //then add this (for safari
    window.onload = function () {
    if(! window.device)
        onDeviceReady()
    }
    
    0 讨论(0)
  • 2020-12-23 17:00

    Enhancing Chemik suggestion. The following code uses navigator.userAgent string to generically determine if the client browser is on a mobile platform.

    The purpose of the separation from desktop browsers is to allow code verifying prior to compiling/installing android apk, etc. It is much faster to make a quick code change, refresh desktop browser vs. compiling in eclipse and loading on android. Another added bonus is the ability to use weinre in one tab & the index.html from android assets in another tab (and use firebug).

    PS: weinre code is excluded since it has my private VPS info & UUID.

    thx!

    <!-- Framework:jQuery -->
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.2, minimum-scale=1.0, maximum-scale=3.0, user-scalable=yes" >
    <link  href="./framework/jquery/mobile/1.2.0/jquery.mobile-1.2.0.min.css" type="text/css" media="screen" rel="stylesheet" title="JQuery Mobile">
    <script src="./framework/jquery/jquery-1.8.2.min.js"></script>
    <script src="./framework/jquery/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
    
    <!-- Framework:Weinre -->
    
    <!-- Framework:PhoneGap -->
    <script src="./framework/phonegap/cordova-2.0.0.js" type="text/javascript" charset="utf-8"></script>
    
    <script type="text/javascript" charset="utf-8">
        var mobile = false;
        if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/)) {
            document.addEventListener("deviceready", function(){mobile = true; onDeviceReady();}, false);
        } else {
            $(document).ready(onDeviceReady);   
        }
        function onDeviceReady() {
            setEvents();
            test();
            if (mobile) {
                navigator.notification.alert("Debugging-ready for\n" + navigator.userAgent);
            } else {
                alert("Debugging-ready for\n" + navigator.userAgent);
            }
        };
    </script>
    
    0 讨论(0)
提交回复
热议问题