https://github.com/paulcoding810/js
Android JavaScript Library
https://github.com/paulcoding810/js
android js kotlin rhino
Last synced: 6 months ago
JSON representation
Android JavaScript Library
- Host: GitHub
- URL: https://github.com/paulcoding810/js
- Owner: paulcoding810
- Created: 2025-01-20T03:28:15.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-03-04T04:09:11.000Z (10 months ago)
- Last Synced: 2025-04-04T23:51:13.987Z (9 months ago)
- Topics: android, js, kotlin, rhino
- Language: Kotlin
- Homepage: https://central.sonatype.com/artifact/com.paulcoding/js
- Size: 131 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Android JavaScript Library
[](https://central.sonatype.com/artifact/com.paulcoding/js)
A Kotlin-based Android library that provides seamless JavaScript execution and integration capabilities using Mozilla's Rhino engine.
## Features
- Execute JavaScript code from strings or files
- Built-in network functions with fetch
- HTML parsing with JSoup integration
- Base64 decoding support
- Console logging capabilities
- JSON parsing and conversion
- Coroutine support for asynchronous operations
## Installation
Add the following dependency to your app's `build.gradle.kts`:
```kotlin
dependencies {
implementation("com.paulcoding:js:1.0.2")
}
```
## Initialization
Initialize the library in your Application class:
```kotlin
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
JS.initialize(applicationContext)
}
}
```
## Usage
### Basic JavaScript Execution
```kotlin
// Create JS instance
val js = JS()
// Execute JavaScript string
suspend fun executeJs() {
js.evaluateString("'Hello, World!'")
.onSuccess { result ->
println(result) // Prints: Hello, World!
}
.onFailure { error ->
error.printStackTrace()
}
}
// Call JavaScript function
suspend fun callJsFunction() {
js.callFunction("add", arrayOf(2, 3))
.onSuccess { result ->
println(result) // Prints: 5
}
}
```
### Loading JS Object
```kotlin
// Load from app's files directory
val js = JS("scripts", "main.js")
// Or with absolute path
val js = JS(File("/path/to/script.js"))
```
```kotlin
// Pass custom properties to the JavaScript environment:
val properties = mapOf(
"apiKey" to "your-api-key",
"baseUrl" to "https://api.example.com"
)
val js = JS(properties = properties)
```
### Built-in JavaScript Functions
#### Network Requests (fetch)
```javascript
// Basic GET request
const response = fetch('https://api.example.com/data')
const jsonData = response.json()
// or
const htmlDoc = response.html()
// or
const textContent = response.text()
// Advanced request with options
const response = fetch('https://api.example.com/data', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ key: 'value' })
})
```
#### Console Logging
```javascript
console.log('Debug message')
```
#### Base64 Decoding
```javascript
const decoded = atob('SGVsbG8gV29ybGQ=')
```
## Dependencies
- [Rhino](https://github.com/mozilla/rhino) - JavaScript engine
- [Ktor](https://ktor.io/) - HTTP client
- [Gson](https://github.com/google/gson) - JSON parsing
- [JSoup](https://jsoup.org/) - HTML parsing
## ProGuard Configuration
```proguard
### Rhino
-keepattributes Signature
-dontwarn org.mozilla.javascript.**
-keep class org.mozilla.javascript.** { *; }
-keep class org.jsoup.** { *; }
-dontwarn org.jspecify.annotations.NullMarked
### Ktor and OkHttp
-keep class okhttp3.** { *; }
-keep class com.squareup.okhttp3.** { *; }
-keep class io.ktor.** { *; }
-dontwarn java.lang.management.ManagementFactory
-dontwarn java.lang.management.RuntimeMXBean
### Gson
-keep class com.google.gson.** { *; }
### R8
-dontwarn kotlin.Cloneable$DefaultImpls
```
## Requirements
- Minimum SDK: 24
- Kotlin: 1.8+
- Java: 11
## Credits
[VBook Extensions](https://github.com/Darkrai9x/vbook-extensions)
## License
See [License](LICENSE)