简体   繁体   English

如何在Android中使用Duktape访问Javascript模块

[英]How to access Javascript module with Duktape in Android

I am successfully parsing and evaluating a javascript file with Duktape in my Android application using Kotlin. 我正在使用Kotlin在我的Android应用程序中使用Duktape成功解析和评估javascript文件。

val file_name = "lib.js"

val js_string = application.assets.open(file_name).bufferedReader().use {
  it.readText()
}


val duktape = Duktape.create()

try {
  Log.d("Greeting", duktape.evaluate("'hello world'.toUpperCase();").toString())
  duktape.evaluate(js_string)

} finally {
  duktape.close()
}

The javascript file was created with Browserify, so it is one single file with everything and it is working fine. javascript文件是使用Browserify创建的,因此它是一个包含所有内容的单个文件,并且工作正常。 But I need to request a module and a method from the module, example: 但是我需要从模块中请求一个模块和一个方法,例如:

var test = require('testjs-lib');
test.EVPair.makeRandom().toWTF();

I have no idea of how to do it and have not found any example, besides this link: http://wiki.duktape.org/HowtoModules.html 除了这个链接,我不知道怎么做,也没有找到任何例子: http//wiki.duktape.org/HowtoModules.html

It tells me to use a modsearch, but I don't have a clue how to do it or where it should be placed, not even if it is applicable for the Duktape Android ( https://github.com/square/duktape-android ). 它告诉我使用modsearch,但我不知道如何做到或应该放在哪里,即使它适用于Duktape Android( https://github.com/square/duktape-) android )。

Has anybody done it successfully that could shed some light on this matter? 有没有人成功地做过这件事可以解释一下这个问题?

in the testjs-lib.js, add the JS code that makes use of the module testjs-lib.js itself exports. 在testjs-lib.js中,添加使用模块testjs-lib.js本身导出的JS代码。 For example: 例如:

function myModule() {
  this.hello = function() {
    return 'hello!';
  }

  this.goodbye = function() {
    return 'goodbye!';
  }
}

module.exports = myModule;

//You code goes here
console.log(myModule.hello());
console.log(myModule.goodbye());

Then ask Duktape to evaluate the entire file. 然后让Duktape评估整个文件。

Say you want to include Underscore in duktape. 说你要包括下划线在duktape。

  1. Put your module/library code in a separate js file. 将模块/库代码放在单独的js文件中。 In an android project, you can put this js file in Assets folder. 在android项目中,您可以将此js文件放在Assets文件夹中。 In our example, it'd be sth like: underscore.js 在我们的例子中,它就像: underscore.js

  2. Create a java interface that'd be used by duktape to get inputstream to this js file. 创建一个duktape用来获取此js文件的输入流的java接口。 Sth like: 就像:

``` ```

public interface DuktapeHelper {

    @JavascriptInterface
    String getUnderScore();
}

```` ````

  1. Bind this java interface to a js interface in your duktape instance. 将此java接口绑定到duktape实例中的js接口。

``` ```

duktape.bind("helper", DuktapeHelper.class, <instance of your DuktapeHelperImplementation>);

``` ```

  1. Implment modSearch function in duktape using helper interface that you injected before. 使用之前注入的helper接口在duktape中使用modSearch函数。

``` ```

 duktape.evaluate("Duktape.modSearch = function (id) {\n" +
                    "  if (id == \"underscore\") {" +
                    "  return helper.getUnderScore();" +
                    "  } " +
                    "   throw new Error('cannot find module: ' + id);" +
                    "  };" +
                "var _ = require('underscore')._; ");

``` ```

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

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