Add JavaScriptEngine abstraction to extensions-lib (#8155)
This allows us to swap out the implementation in the future and on different platforms without major changes to the extensions themselves.
This commit is contained in:
parent
caf9219d99
commit
7be6863910
@ -223,9 +223,6 @@ dependencies {
|
|||||||
// Data serialization (JSON, protobuf)
|
// Data serialization (JSON, protobuf)
|
||||||
implementation(kotlinx.bundles.serialization)
|
implementation(kotlinx.bundles.serialization)
|
||||||
|
|
||||||
// JavaScript engine
|
|
||||||
implementation(libs.bundles.js.engine)
|
|
||||||
|
|
||||||
// HTML parser
|
// HTML parser
|
||||||
implementation(libs.jsoup)
|
implementation(libs.jsoup)
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ import eu.kanade.tachiyomi.data.saver.ImageSaver
|
|||||||
import eu.kanade.tachiyomi.data.track.TrackManager
|
import eu.kanade.tachiyomi.data.track.TrackManager
|
||||||
import eu.kanade.tachiyomi.data.track.job.DelayedTrackingStore
|
import eu.kanade.tachiyomi.data.track.job.DelayedTrackingStore
|
||||||
import eu.kanade.tachiyomi.extension.ExtensionManager
|
import eu.kanade.tachiyomi.extension.ExtensionManager
|
||||||
|
import eu.kanade.tachiyomi.network.JavaScriptEngine
|
||||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||||
import eu.kanade.tachiyomi.network.NetworkPreferences
|
import eu.kanade.tachiyomi.network.NetworkPreferences
|
||||||
import eu.kanade.tachiyomi.source.SourceManager
|
import eu.kanade.tachiyomi.source.SourceManager
|
||||||
@ -79,7 +80,6 @@ class AppModule(val app: Application) : InjektModule {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
addSingletonFactory {
|
addSingletonFactory {
|
||||||
Database(
|
Database(
|
||||||
driver = get(),
|
driver = get(),
|
||||||
@ -92,7 +92,6 @@ class AppModule(val app: Application) : InjektModule {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
addSingletonFactory<DatabaseHandler> { AndroidDatabaseHandler(get(), get()) }
|
addSingletonFactory<DatabaseHandler> { AndroidDatabaseHandler(get(), get()) }
|
||||||
|
|
||||||
addSingletonFactory {
|
addSingletonFactory {
|
||||||
@ -101,7 +100,6 @@ class AppModule(val app: Application) : InjektModule {
|
|||||||
explicitNulls = false
|
explicitNulls = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addSingletonFactory {
|
addSingletonFactory {
|
||||||
XML {
|
XML {
|
||||||
unknownChildHandler = UnknownChildHandler { _, _, _, _, _ -> emptyList() }
|
unknownChildHandler = UnknownChildHandler { _, _, _, _, _ -> emptyList() }
|
||||||
@ -110,19 +108,17 @@ class AppModule(val app: Application) : InjektModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addSingletonFactory { ChapterCache(app) }
|
addSingletonFactory { ChapterCache(app) }
|
||||||
|
|
||||||
addSingletonFactory { CoverCache(app) }
|
addSingletonFactory { CoverCache(app) }
|
||||||
|
|
||||||
addSingletonFactory { NetworkHelper(app) }
|
addSingletonFactory { NetworkHelper(app) }
|
||||||
|
addSingletonFactory { JavaScriptEngine(app) }
|
||||||
addSingletonFactory { ExtensionManager(app) }
|
|
||||||
|
|
||||||
addSingletonFactory { SourceManager(app, get(), get()) }
|
addSingletonFactory { SourceManager(app, get(), get()) }
|
||||||
|
addSingletonFactory { ExtensionManager(app) }
|
||||||
|
|
||||||
addSingletonFactory { DownloadManager(app) }
|
addSingletonFactory { DownloadManager(app) }
|
||||||
|
|
||||||
addSingletonFactory { TrackManager(app) }
|
addSingletonFactory { TrackManager(app) }
|
||||||
|
|
||||||
addSingletonFactory { DelayedTrackingStore(app) }
|
addSingletonFactory { DelayedTrackingStore(app) }
|
||||||
|
|
||||||
addSingletonFactory { ImageSaver(app) }
|
addSingletonFactory { ImageSaver(app) }
|
||||||
|
@ -43,4 +43,7 @@ dependencies {
|
|||||||
api(libs.preferencektx)
|
api(libs.preferencektx)
|
||||||
|
|
||||||
implementation(androidx.corektx)
|
implementation(androidx.corektx)
|
||||||
|
|
||||||
|
// JavaScript engine
|
||||||
|
implementation(libs.bundles.js.engine)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
package eu.kanade.tachiyomi.network
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import app.cash.quickjs.QuickJs
|
||||||
|
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Util for evaluating JavaScript in sources.
|
||||||
|
*/
|
||||||
|
class JavaScriptEngine(context: Context) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Evaluate arbitrary JavaScript code and get the result as a primtive type
|
||||||
|
* (e.g., String, Int).
|
||||||
|
*
|
||||||
|
* @since extensions-lib 1.4
|
||||||
|
* @param script JavaScript to execute.
|
||||||
|
* @return Result of JavaScript code as a primitive type.
|
||||||
|
*/
|
||||||
|
@Suppress("UNUSED")
|
||||||
|
suspend fun <T> evaluate(script: String): T = withIOContext {
|
||||||
|
QuickJs.create().use {
|
||||||
|
it.evaluate(script) as T
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user