简体   繁体   English

如何从外部网页访问本地数据?

[英]how to access local data from external webpage?

I'm trying to access the data inside the assets/css from an external HTML file. 我正在尝试从外部HTML文件访问assets / css中的数据。

The process goes like this: 过程如下:

<html> 
<head>
<meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, height=device-height, user-scalable=yes" />  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>ola</title> 

 <link rel="stylesheet" type="text/css" href="file:///android_asset/css/main.css" />
 <link rel="stylesheet" type="text/css" href="file:///android_asset/css/sch.css" />
 <script type="text/javascript" src="file:///android_asset/css/ethan.js" /> 
 <script type="text/javascript" src="scripts/allinone.js" /> 

</head> 
<body> 
  <input id="btnTest1" name="button" type="button" style="height:0px;width:0px;" /> 
</body> 
</html> 

So here the thing is that, I'm actually calling the HTML file using a link (since the HTML file is not locally present). 所以事情是,我实际上是使用链接调用HTML文件(因为HTML文件不是本地存在的)。 But main.css , sch.css and ethan.js are locally present in the assets/css folder. 但是main.csssch.cssethan.js本地存在于assets / css文件夹中。

What I'm trying to do is to load the allinone.js which is obviously external and the other three files into the which are internal and run the script. 我正在尝试做的是加载allinone.js ,这显然是外部的,其他三个文件加载到内部并运行脚本。

I found " file:///android_asset/css/main.css " but it looks like it doesn't work. 我找到了“ file:///android_asset/css/main.css ”,但看起来它不起作用。

Please help.... 请帮忙....

I would be curious to know more about the use case here. 我很想知道这里的用例。 My understanding is this: 我的理解是这样的:

  1. You're loading an externally hosted HTML file into an Android Webview 您正在将外部托管的HTML文件加载到Android Webview中
  2. You need to overlay some local styles/scripts, which can't be hosted on the external site along with the HTML (presumably because you're generating them dynamically). 您需要覆盖一些本地样式/脚本,这些样式/脚本不能与HTML一起托管在外部站点上(可能是因为您动态生成它们)。

If that's so -- and given that the logical approach you conceived of using the file:// URI does not work -- there would seem to be two options, each making use of the webView API: 如果是这样 - 并且考虑到您构想使用file:// URI的逻辑方法不起作用 - 似乎有两个选项,每个选项都使用webView API:

  1. Load the HTML file from the remote source, modify it, then set it as the webView's source. 从远程源加载HTML文件,修改它,然后将其设置为webView的源。 Locate the tag of the remote HTML and inject your local JS / CSS inline there. 找到远程HTML的标记并在那里注入本地JS / CSS。
  2. Make use of the 'loadUrl' WebView method to inject your CSS/Javascript dynamically (this seems unnecessarily complicated if #1 is an option). 利用'loadUrl'WebView方法动态注入你的CSS / Javascript(如果#1是一个选项,这似乎不必要地复杂)。 For example: 例如:

mWebView.loadUrl("javascript:injectJavascript(js)");

where the parameter 'js' is some inline Javascript that you load within your Android code, and injectJavascript is a method in the remote HTML file that actually inserts it into your DOM. 其中参数'js'是您在Android代码中加载的内联Javascript,而injectJavascript是远程HTML文件中实际将其插入DOM的方法。 Take an analogous approach to insert your CSS ... 采用类似的方法插入CSS ...

Admittedly these approaches are a bit hackish. 不可否认,这些方法有些过时。 Ideally you would use a custom method of the WebView class like 'addCssToDom' or something, but as far as I can see, no such methods are available. 理想情况下,您将使用WebView类的自定义方法,如“addCssToDom”或其他东西,但据我所知,没有这样的方法可用。

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

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