javascript Phonegap 3 包含一个警告框,其中“OK”会使浏览器崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18623351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:38:13  来源:igfitidea点击:

Phonegap 3 contains an alert box in which "OK" crashes browser

javascriptandroidcordova

提问by Jamie Hutber

I can't come up with a good explanation for this, but when I include

我无法对此做出很好的解释,但是当我包括在内时

<script type="text/javascript" src="phonegap.js"></script>

In my Phonegap app, which I have not modified, 2 pops will appear on screen.

在我没有修改的 Phonegap 应用程序中,屏幕上会出现 2 个弹出窗口。

//The first popup
gap:["PluginManager","startup","PluginManager224542697"]

//the second
gap:["App","show","App224542698"]

I have to cancel both popups and would really love to understand the reasoning.

我必须取消两个弹出窗口,并且真的很想了解其中的原因。

The two lines in question are on line 21117 and 21118 of phonegap.js

有问题的两条线在 phonegap.js 的 21117 和 21118 行

 // Tell the native code that a page change has occurred.
 require('cordova/exec')(null, null, 'PluginManager', 'startup', []);
 require('cordova/channel').onNativeReady.fire();

This of course does not break when its in the app, but it does mean that development is next to impossible.

这在应用程序中当然不会中断,但这确实意味着开发几乎是不可能的。

采纳答案by Jamie Hutber

I had initially resisted posting an answer on this question. As was mentioned by one of the developers that the phonegap.jsshould not be included unless it is production code.

我最初拒绝发布关于这个问题的答案。正如其中一位开发人员所提到的,phonegap.js除非是生产代码,否则不应包含该代码。

This however means you can't tests phonegap features.

但是,这意味着您无法测试 phonegap 功能。

To briefly explain my thought process, in all my years developing (php) I have often set global variables that I can easily write code for dev, stage or production.

简单解释一下我的思考过程,在我开发 (php) 的这些年里,我经常设置全局变量,我可以轻松地为dev, stage or production.

Please consider this might not be the best way to go, but for me it works and I'm enjoying the rest of my backbone app far more than revisiting this currently ;)

请考虑这可能不是最好的方法,但对我来说它是有效的,而且我正在享受我的主干应用程序的其余部分,而不仅仅是目前重新审视这个 ;)

So, I did this:

所以,我这样做了:

//Define SD
var SD = {}; //define SD so we can use it globally

/*==================================================
Is Mobile - If true then we are a mobile
================================================== */
SD.isMobile = true;

//This is with the assumtion that your domain contains (for exmaple) http://yourdomain.local
if (document.URL.indexOf("local") > 0 || document.URL.indexOf("sex") > 0) {
    SD.isMobile = false;
}

SD = {
    isMobile: SD.isMobile,
    ENVIROMENT: 'liveApp',
    CDN: 'yoururl.com/',
    HTTP: 'http://yoururl.com/',
 }

// #define the globals depending on where we are -----------------------------------------------------
SD.globals = function () {
    switch (window.location.hostname) {
        case "sd.local":
            SD.ENVIROMENT = 'localApp',
                SD.CDN = 'sd.local/',
                SD.HTTP = 'http://yoururl.com/',
                SD.AJAX = SD.HTTP+'app/';
            break;
        case "192.168.0.25": //Set to your machines IP address
            SD.ENVIROMENT = 'mobilePhone',
                SD.AJAX = SD.HTTP+ 'app/';
            break;
        default:
            SD.AJAX = SD.HTTP+'app/';
            break;
    }
};

And now finally, after all the initwork I add the phonegap.jsif we need it.

现在终于,在完成所有init工作后,phonegap.js如果我们需要,我会添加它。

 if(SD.isMobile){
    $.getScript('phonegap.js', function( data, textStatus, jqxhr){
    c( "cordova was loaded." );
});

回答by antscode

For me the issue was occurring because phonegap.js was falling back to PROMPT based mode when running outside of phonegap. I believe this mode is intended for phonegap below Android 3.2, where all communication is via alert prompts (which is what you're seeing).

对我来说,发生这个问题是因为在 phonegap 之外运行时,phonegap.js 回退到基于 PROMPT 的模式。我相信此模式适用于 Android 3.2 以下的 phonegap,其中所有通信都通过警报提示(这就是您所看到的)。

I fixed the problem by setting the following variable before loading phonegap.js:

我通过在加载 phonegap.js 之前设置以下变量来解决这个问题:

window._cordovaNative = true;

window._cordovaNative = true;

This tricks phonegap into thinking you're running above phonegap 3.2 hence never enters Prompt mode.

这会让 phonegap 认为你在 phonegap 3.2 以上运行,因此永远不会进入提示模式。

Of course, if you're targeting devices below 3.2 then doing this will probably break all communication with Phonegap on those devices...

当然,如果你的目标是低于 3.2 的设备,那么这样做可能会破坏这些设备上与 Phonegap 的所有通信......

回答by Yauhen Yakimovich

I have stumbled upon this error and question while trying the following:

我在尝试以下操作时偶然发现了这个错误和问题:

  • Completed the hello world tutorial with cordova create, etc.
  • Deployed both on emulator and the connected device w/o a problem.
  • I got the same error as OP when doing cordova serveand just navigating to //localhost:8000 as instructed.
  • 完成了 hello world 教程cordova create,等等。
  • 部署在模拟器和连接的设备上,没有 oa 问题。
  • 我在执行时遇到了与 OP 相同的错误cordova serve,只是按照说明导航到 //localhost:8000。

I agree - seeing those alerts is super annoying. It kills the whole point of speedy non-native js-based development.

我同意 - 看到这些警报非常烦人。它扼杀了基于非原生 js 的快速开发的全部意义。

What is going wrong?

出了什么问题?

Communication API with the "device" is falling back to this handler promptbasednativeapi.js (also see cordova-js/src/android/exec.js). On version 3.x.x just saying

与“设备”的通信 API 回退到这个处理程序 promptbasednativeapi.js(另见cordova-js/src/android/exec.js)。在版本 3.xx 上只是说

window._cordovaNative = true;

was not enough for me. One should implement the whole protocol of communication.

对我来说还不够。应该实现整个通信协议。

Quick solution

快速解决方案

You can use some browser side "emulator", e.g. Ripple emulator extensionfor chrome ([UPDATE]Consider using https://github.com/apache/incubator-ripplefor phonegap 3.x.x as pointed by DuKes0mE). Such an "emulator" can understand and fire respective events, e.g. "device ready". So far it looks promising :-)

您可以使用一些浏览器端“模拟器”,例如chrome 的Ripple 模拟器扩展[更新]考虑使用https://github.com/apache/incubator-ripplefor phonegap 3.xx,如 DuKes0mE 所指出的)。这种“仿真器”可以理解并触发相应的事件,例如“设备就绪”。到目前为止,它看起来很有希望:-)

What is going on?

到底是怎么回事?

An answer by @antscode was of great help. After some digging and reading of cordova/cordova-jscode I figured out that

@antscode 的回答很有帮助。在对cordova/cordova-js代码进行了一些挖掘和阅读之后,我发现

  • Error comes from the mechanism which is a part of cordova plugin architecture. Plugins are developed as so called cordova commandswhich can be executed from js - this is exactly how cordova architecture is organized.
  • Cordova is a JS framework. One is suggested to primary write (non-native) JS code. To talk to all those different native plugins one has to come up with the protocol to communicate with them, sort of RPC with JSON serialization. This is exactly what is going own.
  • Plugins can be also in pure JS. To quote the manual
  • 错误来自作为cordova插件架构一部分的机制。插件是作为所谓的cordova命令开发的,可以从js执行——这正是cordova架构的组织方式。
  • Cordova 是一个 JS 框架。一种是建议主要编写(非原生)JS 代码。要与所有这些不同的本机插件交谈,必须提出与它们通信的协议,类似于带有 JSON 序列化的 RPC。这正是自己要发生的事情。
  • 插件也可以是纯 JS。引用手册

Create a new echomeplugin with:

创建一个新的echome插件:

window.echo = function(str, callback) {
    cordova.exec(callback, function(err) {
        callback('Nothing to echo.');
    }, "Echo", "echo", [str]);
};

Access it as a cordova command via JS:

通过 JS 作为cordova 命令访问它:

 window.echo("echome", function(echoValue) {
        alert(echoValue == "echome"); // should alert true.
    });

No emulator solution

无模拟器解决方案

I could imagine a situation when an rather complicated app will break just inside such an web-browser client side "emulator" (and only in it). A better solution would be to figure out a way to convince the app not to fallback to PROMPT method of communication (the one that makes annoying alerts). Well, I do not have such a solution right now :-( Will be happy to learn how to do it though.

我可以想象一个相当复杂的应用程序会在这样的网络浏览器客户端“模拟器”(并且仅在其中)中崩溃的情况。更好的解决方案是找出一种方法来说服应用程序不要回退到 PROMPT 通信方法(发出烦人警报的方法)。好吧,我现在没有这样的解决方案 :-( 不过会很高兴学习如何去做。

Solution is here: https://gist.github.com/ewiger/7d5e0cc8fccf311e9ce2

解决方案在这里:https: //gist.github.com/ewiger/7d5e0cc8fccf311e9ce2

回答by r3wt

there is probably no return event for the cancel button. if no instructions are passed to the kernel, this results in an exception(your browser crashing.) try defining an action for the cancel button and see if that helps.

取消按钮可能没有返回事件。如果没有指令传递给内核,这会导致异常(您的浏览器崩溃)。尝试为取消按钮定义一个操作,看看是否有帮助。

回答by Marhalpert

I went into hello/platforms/ios/CordovaLib and copied the cordova.js file into my www root and changed

我进入 hello/platforms/ios/CordovaLib 并将 cordova.js 文件复制到我的 www 根目录并更改

Or download from here:https://github.com/apache/cordova-ios/blob/master/CordovaLib/cordova.js

或从这里下载:https: //github.com/apache/cordova-ios/blob/master/CordovaLib/cordova.js

<script type="text/javascript" src="phonegap.js"></script>

to

<script type="text/javascript" src="cordova.js"></script>

and no more popups

没有更多的弹出窗口

回答by 13rac1

I was having the same issue with a project created by another developer. The cause in my case was the remains of the Cordova/PhoneGap 2.x configuration. This popup is related to the plugin system. Removing plugin from my config.xml made it better:

我在另一个开发人员创建的项目中遇到了同样的问题。在我的案例中,原因是 Cordova/PhoneGap 2.x 配置的剩余部分。此弹出窗口与插件系统有关。从我的 config.xml 中删除插件使它变得更好:

<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" />

The end, I decided it would be best to make a new 3.x project and move all of my HTML/CSS/JS to the newly created project. The new project fixed all issues.

最后,我决定最好创建一个新的 3.x 项目并将我所有的 HTML/CSS/JS 移动到新创建的项目中。新项目解决了所有问题。

回答by FlyTigert

I was experiencing the exact same two popups you describe but only experienced them when i added iOS to my phonegap project. the first time i did this i copied the www folder from my working Android app. this was the problem because it had artifacts from the Android plugins. After blasting and recreating the iOS app in the phonegap project using the command line tools i more carefully brought over only the needed html, js and css files for my app which resolved the issue.

我遇到了与您描述的完全相同的两个弹出窗口,但仅在我将 iOS 添加到我的 phonegap 项目时才遇到它们。我第一次这样做时,我从我正在运行的 Android 应用程序中复制了 www 文件夹。这是问题所在,因为它有来自 Android 插件的工件。使用命令行工具在 phonegap 项目中爆破并重新创建 iOS 应用程序后,我更小心地只为我的应用程序带来了所需的 html、js 和 css 文件,从而解决了问题。