0

我有一个 Webview,其中加载了用户定义的网站,然后按下 WebView 下方的按钮,将 Javascript(来自远程 url)加载到用户所在的页面中。

然后应该触发这个 JavaScript 中的一个函数,但是它似乎没有工作。

我的活动:

public class SearchViewer extends SherlockActivity {

    TextView grabit;
    WebView webView;
    ImageView right, left;
    ProgressBar progressBar;

    @SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchviewer);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setIcon(R.drawable.ic_arrowleft);

        Intent intent = getIntent();
        String url = intent.getStringExtra("URL");
        String name = intent.getStringExtra("Name");
        setTitle(name);

        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl(url);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);

                progressBar.setVisibility(View.GONE);

            }
        });
        grabit = (TextView) findViewById(R.id.grabit);
        grabit.setOnClickListener(OnClick_grabit);

        right = (ImageView) findViewById(R.id.right);
        left = (ImageView) findViewById(R.id.left);

        right.setOnClickListener(OnClick_right);
        left.setOnClickListener(OnClick_left);

    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

        case android.R.id.home:
            finish();
            break;

        default:
            return super.onOptionsItemSelected(item);
        }
        return false;
    }

    public void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this);
    }

    public void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this);
    }

    private OnClickListener OnClick_grabit = new OnClickListener() {
        public void onClick(View v) {
            webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
            webView.loadUrl("javascript: initialize("+1+")");
        }
    };
    private OnClickListener OnClick_right = new OnClickListener() {
        public void onClick(View v) {
            webView.goForward();
        }
    };
    private OnClickListener OnClick_left = new OnClickListener() {
        public void onClick(View v) {
            webView.goBack();
        }
    };

当我加载javascript时

webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");

并在 Javascript 文件中启动 Javascriptinitialize()函数,它工作正常。但是,当我尝试执行上述两行时

webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');");
                webView.loadUrl("javascript: initialize("+1+")");

它不起作用,我怎样才能加载 Javascript,然后从 android 触发函数,以便我可以传递一个值。

我努力了

private OnClickListener OnClick_grabit = new OnClickListener() {
        public void onClick(View v) {
            webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pmdev.co.uk/dadg-giftgrabber/android/gift-grabber.js');");
            webView.setWebViewClient(new WebViewClient(){

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    webView.loadUrl("javascript: initialize("+1+")");
                }

            });
        }
    };

但这不起作用

4

2 回答 2

0

Lajos 的答案应该可以工作,但是,如果您希望最后一个代码段正常工作,则需要setWebViewClient在实际调用加载脚本之前调用调用初始化的 onPageFinished,loadUrl即使这样做,您也应该小心你会调用你的javascriptinitialize()函数两次。问题是,loadUrl 将在加载完成时产生对 onPageFinished 的调用(即使 url 是javascript:url.

于 2013-09-27T03:31:16.437 回答
0

你试过这个:

webView.loadUrl("javascript: function loadScript(scriptURL) { var scriptElem = document.createElement('SCRIPT'); scriptElem.setAttribute('language', 'JavaScript'); scriptElem.setAttribute('src', scriptURL); document.body.appendChild(scriptElem);} loadScript('http://www.pathtofile.com/javascript.js');initialize("+1+")");
于 2013-09-26T09:01:30.410 回答