简体   繁体   English

如何在Cordova / Phonegap中的远程html文件中包含本地脚本?

[英]How to include a local script in remote html file in Cordova / Phonegap?

I have an iOS Cordova/Phonegap project which loads html pages from the internet. 我有一个iOS Cordova / Phonegap项目,可从互联网加载html页面。 This works if I upload cordova.js and all the plugins then include cordova.js in the html page. 如果我上传cordova.js,然后所有插件都在html页面中包含cordova.js,则此方法有效。 However, since the files are already in the app, it seems a waste to make people download them all. 但是,由于文件已经在应用程序中,因此让人们全部下载它们似乎是一种浪费。 I'm trying to include the local files in the remote html, but it doesn't seem to load. 我正在尝试将本地文件包含在远程html中,但似乎没有加载。 How can I do this? 我怎样才能做到这一点?

I have a local script test.js in www 我在www中有一个本地脚本test.js

alert("test");

I'm getting the path to it with 我正在走向它

[[NSBundle mainBundle] pathForResource:@"test" ofType:@"js" inDirectory:@"www"];

It looks something like 看起来像

/var/containers/Bundle/Application/E31EA51E-7ED0-4D30-90FC-57ACBF3B3DA5/MyApp.app/www/test.js

I include the file in the remote html 我将文件包含在远程html中

<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval' gap://ready file: data:">
<script src="file:///var/containers/Bundle/Application/E31EA51E-7ED0-4D30-90FC-57ACBF3B3DA5/MyApp.app/www/test.js" type="text/javascript"></script>

However, the script never loads. 但是,脚本永远不会加载。

Edit: I added "Access-Control-Allow-Origin: *" header to the http page, but it still doesn't work. 编辑:我在HTTP页上添加了“ Access-Control-Allow-Origin:*”标题,但仍然无法正常工作。

面对相同的问题,我认为在iOS 11中使用“ WKURLSchemeHandler”拦截您的请求将解决该问题。

if you are using UIWebView, it's quite easy to go with a NSURLProtocol subclass which can intercept all of your network request and do your own job(in your case, accept the http://*/test.js and return the file content as the response). 如果您使用的是UIWebView,则很容易使用NSURLProtocol子类,该子类可以拦截您的所有网络请求并完成您自己的工作(在您的情况下,接受http://*/test.js并将文件内容返回为响应)。

However, if you are using the WKWebView, the NSURLProtocol would not work, and the latest WKURLSchemeHandler can't intercept the http/https requests either.probably set up an embedded http server is the way to go. 但是,如果您使用的是WKWebView,则NSURLProtocol将不起作用,并且最新的WKURLSchemeHandler也无法拦截http / https请求。可能的方法是设置嵌入式http服务器。

This can be done using a PR to the file plugin which solves the mixed content problem on ios: apache/cordova-plugin-file#296 The fixed version is available at: https://github.com/guylando/cordova-plugin-file 可以使用PR到文件插件来解决此问题,该插件可以解决ios上的混合内容问题:apache / cordova-plugin-file#296固定版本位于: https : //github.com/guylando/cordova-plugin-文件

If you load a remote site https://example.com on the webview then it allows to access local files using the url: https://example.com/cdvfile/bundle/www/cordova.js instead of cdvfile://localhost/bundle/www/cordova.js And by this solves the mixed content problems 如果在Web视图上加载远程站点https://example.com ,则它允许使用以下URL访问本地文件: https ://example.com/cdvfile/bundle/www/cordova.js而不是cdvfile:// localhost / bundle / www / cordova.js从而解决了混合内容的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM