javascript 为什么 Cordova 2.7.0 JS 似乎不再适用于远程页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16312222/
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 04:10:00  来源:igfitidea点击:

Why does Cordova 2.7.0 JS seemingly no longer work on remote pages?

javascriptioscordova

提问by Charlie Gorichanaz

Background

背景

I'm attempting to upgrade an iOS app built on Cordova2.0 to version 2.7.

我正在尝试将基于Cordova2.0的 iOS 应用升级到 2.7 版。

It's basically a welcome screen that points to a remote search engine (please withhold comments about app validity and likely approval, as we're past that), and we were using the ChildBrowserplugin to enable opening links in a sub browser so as not to trap the user in the Cordova webview.

它基本上是一个指向远程搜索引擎的欢迎屏幕(请不要对应用程序有效性和可能的​​批准发表评论,因为我们已经过去了),我们使用ChildBrowser插件来启用在子浏览器中打开链接,以免将用户困在 Cordova 网络视图中。

Cordova 2.7 has a feature called InAppBrowserI am hoping to use instead of ChildBrowser. InAppBrowser does essentially the same thing, aside from missing a button to open in Safari.

Cordova 2.7 有一个名为InAppBrowser的功能,我希望用它来代替 ChildBrowser。InAppBrowser 基本上做了同样的事情,除了缺少在 Safari 中打开的按钮。

Problem

问题

The existing app's remote webpages include the Cordova JS (as well as that for the ChildBrowser plugin) and it works fine for opening links in the sub browser.

现有应用程序的远程网页包括 Cordova JS(以及 ChildBrowser 插件的),它适用于在子浏览器中打开链接。

My test Cordova 2.7 app doesn't seem to load the Cordova JS correctly when it's being loaded from a remote web page.

我的测试 Cordova 2.7 应用程序在从远程网页加载时似乎没有正确加载 Cordova JS。

I tried using this exact same HTML on the embedded start page and a remote start page:

我尝试在嵌入式起始页和远程起始页上使用完全相同的 HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://mydomain.com/mobile/cordova-2.7.0.js"></script>
  </head>
  <body>
    <script>
      document.addEventListener("deviceready", onDeviceReady, false);
      function onDeviceReady() {
        alert("Ready!!");
      }
    </script>
  </body>
</html>

To test this as the embedded start page, I put this line in config.xml:

为了将其作为嵌入式起始页进行测试,我将此行放在config.xml

<content src="index.html" />

When I run the app, I promptly get the "Ready!" alert.

当我运行该应用程序时,我立即得到“准备就绪!” 警报。

To test this as the remote start page (I'm aiming to linkto the remote page in the final app, I am just using it as the start page for testing. The result is the same if I link from the embedded page.), I put this line in config.xml:

将此作为远程起始页面进行测试(我的目标是链接到最终应用程序中的远程页面,我只是将其用作测试的起始页面。如果我从嵌入页面链接,结果是相同的。) ,我把这一行放在config.xml

<content src="http://mydomain.com/mobile/index.php" />

When I run the app, I just get the blank screen and no alert.

当我运行该应用程序时,我只看到空白屏幕而没有警报。

Further, in cordova-2.7.0.jsL. 6255, I changed

此外,在cordova-2.7.0.jsL. 6255 中,我改变了

console.log('deviceready has not fired after 5 seconds.');

to

alert('deviceready has not fired after 5 seconds.');

With that change, running the app using the remote start page causes the blank page, and then after five seconds, I get the alert "deviceready has not fired after 5 seconds.". So this tells me Cordova JS is not starting correctly. Needless to say, I can't get InAppBrowser to launch links in the sub browser on the remote site, but I can get it working just fine on the embedded start page.

通过这一更改,使用远程启动页面运行应用程序会导致空白页面,然后在五秒后,我收到警报“5 秒后设备就绪尚未触发。”。所以这告诉我 Cordova JS 没有正确启动。不用说,我无法让 InAppBrowser 在远程站点的子浏览器中启动链接,但我可以让它在嵌入式起始页上正常工作。

Anyone have any ideas of where to go from here? This is a pretty simplistic example, so I'm assuming this is a Cordova settings problem or a change in the functionality. I appreciate any thoughts, thanks!

任何人都知道从这里去哪里?这是一个非常简单的示例,因此我假设这是 Cordova 设置问题或功能更改。我感谢任何想法,谢谢!

采纳答案by Shazron

Yes, something broke in 2.7 - related to our cordova-cli work. See: https://issues.apache.org/jira/browse/CB-3029

是的,在 2.7 中出现了一些问题 - 与我们的 cordova-cli 工作有关。请参阅:https: //issues.apache.org/jira/browse/CB-3029

The fix is to add an empty file called "cordova_plugins.json" in your root folder.

修复方法是在根文件夹中添加一个名为“cordova_plugins.json”的空文件。

回答by jjsquared

I had a similar problem relating to upgrading to Cordova 2.7. However my problem was all my console.logs stopped firing when running the app. I couldn't figure out why for the life of me this was happening. I thought it was because I upgraded jquery.mobile. That wasn't it. I then thought it was an .htaccess issue, that wasn't it either. It turns out, it was Cordova 2.7 that was causing this problem.

我在升级到 Cordova 2.7 时遇到了类似的问题。但是我的问题是我的所有 console.logs 在运行应用程序时都停止触发。我无法弄清楚为什么我的生活会发生这种情况。我以为是因为我升级了 jquery.mobile。不是这样。然后我认为这是一个 .htaccess 问题,也不是。事实证明,是 Cordova 2.7 导致了这个问题。

I did try adding the .json file on my server, that did not fix the issue.

我确实尝试在我的服务器上添加 .json 文件,但没有解决问题。

The fix was going into the 2.7 source and commenting out the following code:

该修复程序进入 2.7 源代码并注释掉以下代码:

/*comment out this as it is breaking console.logs
    var xhr = new context.XMLHttpRequest();
    xhr.onload = function() {
        // If the response is a JSON string which composes an array, call handlePluginsObject.
        // If the request fails, or the response is not a JSON array, just call finishPluginLoading.
        var obj = this.responseText && JSON.parse(this.responseText);
        if (obj && obj instanceof Array && obj.length > 0) {
            handlePluginsObject(obj);
        } else {
            finishPluginLoading();
        }
    };
    xhr.onerror = function() {
        finishPluginLoading();
    };
    xhr.open('GET', 'cordova_plugins.json', true); // Async
    xhr.send();
    */

Replace entire block with a call to the following function:

调用以下函数替换整个块:

finishPluginLoading();

My logs are now working again. Only took me 3 days scratching my head.

我的日志现在又可以工作了。只花了我 3 天时间挠头。

Hope this helps someone with a similar problem.

希望这可以帮助有类似问题的人。

回答by Nishikant

Like me if you are using Cordova 5.1.1 and want to access native functionality after redirect then copy cordova.js, cordova_plugins.js and plugins folder which is at \platforms\platform_name\assets\www\ and put them on server, finally reference cordova.js inside your html. After every plugin add make sure to update these files and folder.

像我一样,如果您使用 Cordova 5.1.1 并希望在重定向后访问本机功能,然后复制位于 \platforms\platform_name\assets\www\ 的 cordova.js、cordova_plugins.js 和 plugins 文件夹并将它们放在服务器上,最后参考html 中的cordova.js。添加每个插件后,请确保更新这些文件和文件夹。

回答by Felipe Plets

As Shazron mentioned the problem is the issue with the file"cordova_plugins.json".

正如 Shazron 提到的,问题是文件“cordova_plugins.json”的问题。

To solve the problem not changing the code you can create the "cordova_plugins.json" file in the root folder and insert a content between quotation marks inside this file. Mine for example has the following content:

要解决不更改代码的问题,您可以在根文件夹中创建“cordova_plugins.json”文件,并在该文件内的引号之间插入内容。我的例如有以下内容:

"Just a dummy file required since Cordova 2.6.0"

回答by Patrioticcow

create a file cordova_plugins.jsonthat contains {}. then go to cordova-2.7.0.jsand comment this line require('cordova/channel').onNativeReady.fire();then when development done, add it back

创建一个cordova_plugins.json包含{}. 然后去cordova-2.7.0.js评论这一行require('cordova/channel').onNativeReady.fire();然后当开发完成后,把它加回来

回答by MBillau

If you embed Cordova in the external web page, there will be no way to open the InAppBrowser from within your hybrid app, so Cordova will not be able to load. This is because the InAppBrowser requires Cordova to be fully loaded and initialized before it can be used to fetch a remote page. You need to use your HTML page that you have, with the <script type="text/javascript" src="http://mydomain.com/mobile/cordova-2.7.0.js"></script>as the main entry point for your app. Then you can use the InAppBrowser to open up your remote page. (You could probably do this in the onDeviceReady(), not sure if it would "flash" the page first though.) I don't think the remote page should have any Cordova code in it at all. I'm not sure if it would be possible to even interact with Cordova from the remote page due to the Same Origin Policy (probably you could use features of the InAppBrowser to inject "bridge" code though to get around this.)

如果您在外部网页中嵌入 Cordova,将无法从您的混合应用程序中打开 InAppBrowser,因此 Cordova 将无法加载。这是因为 InAppBrowser 需要 Cordova 完全加载和初始化,然后才能用于获取远程页面。您需要使用您拥有的 HTML 页面,以及<script type="text/javascript" src="http://mydomain.com/mobile/cordova-2.7.0.js"></script>作为您的应用程序的主要入口点。然后您可以使用 InAppBrowser 打开您的远程页面。(您可能可以在 onDeviceReady() 中执行此操作,但不确定它是否会先“刷新”页面。)我认为远程页面中根本不应该包含任何 Cordova 代码。由于同源策略,我不确定是否可以从远程页面与 Cordova 交互(可能您可以使用 InAppBrowser 的功能注入“桥接”代码来解决这个问题。)