Android的 - 加载并执行JavaScript的外部网页 [英] Android - load and execute javascript on external webpage

查看:1170
本文介绍了Android的 - 加载并执行JavaScript的外部网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让presume我想添加一个< D​​IV>你好< / DIV> 网​​页的 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).

下面是我的活动的code:

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 );

    }

}

含量的script.js 位于 /资产文件夹:

function job() {
    var div = document.createElement('div');
    div.innerHTML = 'hello';
    document.getElementsByTagName('body').item(0).appendChild(div);
}

和内容的 page.html即可位于 /资产文件夹也:

And the content of page.html located in the "/assets" folder too:

<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");

招不工作了...有一个安全的理由吗?

the trick doesn't work anymore... Is there a security reason?

推荐答案

在资源创建一个文件夹原始:RES /生。然后主机yourFile.html在文件夹生吃。然后用code:

Create a folder "raw" in the res : res/raw. then host yourFile.html in the folder raw. then with your code :

字符串link = this.getApplicationContext()getFilesDir()+原始/ yourFile.html;

String link = this.getApplicationContext().getFilesDir()+ "raw/yourFile.html";

mWebview.loadUrl(链接);

mWebview.loadUrl(link);

这篇关于Android的 - 加载并执行JavaScript的外部网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆