在Android中执行没有webview的javascript [英] Execute javascript without webview in Android

查看:20
本文介绍了在Android中执行没有webview的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Android 应用程序中执行一个 JS 函数.该函数位于网站上的 .js 文件中.

I'm trying to execute a JS fonction in my Android app. The function is in a .js file on a website.

我没有使用 webview,我想执行 JS 函数,因为它发送了我想要的请求.

I'm not using webview, I want to execute the JS function because it sends the request i want.

在浏览器的控制台中,我只需要执行 question.vote(0);,我该如何在我的应用程序中执行此操作?

In the Console in my browser i just have to do question.vote(0);, how can I do it in my app ?

推荐答案

UPDATE 2018: AndroidJSCore 已被 LiquidCore,基于 V8.它不仅包含 V8 引擎,而且还提供所有 Node.js.

UPDATE 2018: AndroidJSCore has been superseded by LiquidCore, which is based on V8. Not only does it include the V8 engine, but all of Node.js is available as well.

您可以在没有 WebView 的情况下执行 JavaScript.您可以使用 AndroidJSCore.下面是一个简单的例子,你可以如何做:

You can execute JavaScript without a WebView. You can use AndroidJSCore. Here is a quick example how you might do it:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://your_website_here/file.js");
HttpResponse response = client.execute(request);
String js = EntityUtils.toString(response.getEntity());

JSContext context = new JSContext();
context.evaluateScript(js);
context.evaluateScript("question.vote(0);");

然而,这很可能在 WebView 之外不起作用,因为我认为您不仅依赖 JavaScript,还依赖 AJAX,它不是纯 JavaScript 的一部分.它需要浏览器实现.

However, this most likely won't work outside of a WebView, because I presume you are not only relying on JavaScript, but AJAX, which is not part of pure JavaScript. It requires a browser implementation.

您是否有理由不使用隐藏的 WebView 而只是注入您的代码?

Is there a reason you don't use a hidden WebView and simply inject your code?

// Create a WebView and load a page that includes your JS file
webView.evaluateJavascript("question.vote(0);", null);     

这篇关于在Android中执行没有webview的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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