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

前端 未结 9 459
太阳男子
太阳男子 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"});
    Wait for ready...

提交回复
热议问题