Android - 在外部网页上加载并执行 javascript
假设我想在网页底部添加一个
我在 WebView 中加载的 http://www.google.com 。是否可以?hello
我可以为内部网页(位于设备内部存储器上的页面)执行此操作。
这是我的 Activity 的代码:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Main extends Activity {
private WebView mWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
private final String jsFileURL = "file:///android_asset/script.js";
@Override
public void onPageFinished(WebView view, String url){
final WebView v = view;
view.loadUrl("javascript:{" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.src = '" + jsFileURL + "';" +
"document.getElementsByTagName('head').item(0).appendChild(script); };");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
v.loadUrl("javascript:job();");
}
}, 100);
}
});
mWebview.loadUrl("file:///android_asset/page.html");
//mWebview.loadUrl("http://www.google.com");
setContentView(mWebview );
}
}
位于“/assets”文件夹中的 script.js 的内容:
function job() {
var div = document.createElement('div');
div.innerHTML = 'hello';
document.getElementsByTagName('body').item(0).appendChild(div);
}
以及 page.html 的内容> 也位于“/assets”文件夹中:
<html>
<head></head>
<body>
<div>content of my page</div>
</body>
</html>
但是
如果我
mWebview.loadUrl("file:///android_asset/page.html");
//mWebview.loadUrl("http://www.google.com");
通过
//mWebview.loadUrl("file:///android_asset/page.html");
mWebview.loadUrl("http://www.google.com");
技巧进行更改就不再起作用...是否有安全原因?
Lets presume I want to add a <div>hello</div>
a the bottom of the webpage http://www.google.com that I loaded in a WebView. Is it possible?
I can do it for an internal webpage (a page located on the internal memory of the device).
Here is the code of my Activity:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Main extends Activity {
private WebView mWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
private final String jsFileURL = "file:///android_asset/script.js";
@Override
public void onPageFinished(WebView view, String url){
final WebView v = view;
view.loadUrl("javascript:{" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.src = '" + jsFileURL + "';" +
"document.getElementsByTagName('head').item(0).appendChild(script); };");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
v.loadUrl("javascript:job();");
}
}, 100);
}
});
mWebview.loadUrl("file:///android_asset/page.html");
//mWebview.loadUrl("http://www.google.com");
setContentView(mWebview );
}
}
The content of script.js located in the "/assets" folder:
function job() {
var div = document.createElement('div');
div.innerHTML = 'hello';
document.getElementsByTagName('body').item(0).appendChild(div);
}
And the content of page.html located in the "/assets" folder too:
<html>
<head></head>
<body>
<div>content of my page</div>
</body>
</html>
BUT
If I change
mWebview.loadUrl("file:///android_asset/page.html");
//mWebview.loadUrl("http://www.google.com");
by
//mWebview.loadUrl("file:///android_asset/page.html");
mWebview.loadUrl("http://www.google.com");
the trick doesn't work anymore... Is there a security reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 res 中创建一个文件夹“raw”:res/raw。然后将 yourFile.html 托管在 raw 文件夹中。然后用你的代码:
Create a folder "raw" in the res : res/raw. then host yourFile.html in the folder raw. then with your code :