Skip to content

Instantly share code, notes, and snippets.

@ewiger
Last active August 29, 2015 14:04
Show Gist options
  • Save ewiger/7d5e0cc8fccf311e9ce2 to your computer and use it in GitHub Desktop.
Save ewiger/7d5e0cc8fccf311e9ce2 to your computer and use it in GitHub Desktop.
Avoiding alerts in cordova with browser as a client

ConsoleBasedNativeApi

Is a tiny work around for the problem described here: http://stackoverflow.com/questions/18623351/phonegap-3-alert-that-if-i-click-ok-crashes-broswer/24974286#24974286

Put this into your js/consolebasednativeapi.js

// The MIT License (MIT)

// Copyright (c) 2014 [email protected]

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/**
 * A remake of promptbasednativeapi.js => use console.log instead of alerts.
 */
(function () {
    var ConsoleBasedNativeApi = {
        exec: function(bridgeSecret, service, action, callbackId, argsJson) {
            return console.log(argsJson, 'gap:'+JSON.stringify([bridgeSecret, service, action, callbackId]));
        },
        setNativeToJsBridgeMode: function(bridgeSecret, value) {
            console.log(value, 'gap_bridge_mode:' + bridgeSecret);
        },
        retrieveJsMessages: function(bridgeSecret, fromOnlineEvent) {
            return console.log(+fromOnlineEvent, 'gap_poll:' + bridgeSecret);
        }
    };
    window._cordovaNative = ConsoleBasedNativeApi;
})();

Now link it somewhere in your index.html, before cordova.js, e.g.

        <script type="text/javascript" src="js/consolebasednativeapi.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment