Skip to content

Commit

Permalink
prepare browser extension
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 26, 2023
1 parent 209c371 commit 1dab7a4
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
app/release/*
!app/release/.gitkeep

sekret.properties
app/shared/sekret.properties
app/desktop/mongodb-realm
app/shared/sekret/src
Expand Down
1 change: 1 addition & 0 deletions app/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ version = appVersion

sekret {
packageName = artifact
propertiesFile = rootDir.canonicalPath
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.datlag.burningseries.ui.screen.initial.series

import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import dev.datlag.burningseries.ui.KeyEventDispatcher
import dev.datlag.burningseries.ui.PIPActions
import dev.datlag.burningseries.ui.PIPEventDispatcher
import dev.datlag.burningseries.ui.PIPModeListener

@Composable
actual fun EnterSeriesScreen() {
SideEffect {
KeyEventDispatcher = { null }
PIPEventDispatcher = { null }
PIPModeListener = { }
PIPActions = { null }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ actual fun VideoScreen(component: VideoComponent) {
val castState by remember(castContext) { mutableStateOf(castContext?.castState) }
val casting by remember(castState) { mutableStateOf(castState == CastState.CONNECTED || castState == CastState.CONNECTING) }
var cast by remember(casting) { mutableStateOf(casting) }
val useCastPlayer by remember(headers, cast) { mutableStateOf(headers.isEmpty() && cast) }
val useCastPlayer = remember(headers, cast) { headers.isEmpty() && cast }

val sessionListener = remember { object : SessionAvailabilityListener {
override fun onCastSessionAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.datlag.burningseries.getPackageName
import dev.datlag.burningseries.network.Firestore
import dev.datlag.burningseries.network.JsonBase
import dev.datlag.burningseries.network.WrapAPI
import dev.datlag.burningseries.network.realm.RealmLoader
import dev.datlag.burningseries.network.state.EpisodeStateMachine
import dev.datlag.burningseries.network.state.HomeStateMachine
import dev.datlag.burningseries.network.state.SaveStateMachine
Expand Down Expand Up @@ -104,8 +105,11 @@ object NetworkModule {
firestoreKtor.create<Firestore>()
}
}
bindEagerSingleton {
RealmLoader(instanceOrNull())
}
bindProvider {
EpisodeStateMachine(instance(), instance(), instanceOrNull(), instanceOrNull(), instanceOrNull())
EpisodeStateMachine(instance(), instance(), instance(), instanceOrNull(), instanceOrNull())
}
bindEagerSingleton {
SaveStateMachine(instance(), instance(), instanceOrNull(), instanceOrNull(), instanceOrNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ fun SeriesScreen(component: SeriesComponent) {
}

dialogState.child?.instance?.render()
EnterSeriesScreen()
}

@Composable
expect fun EnterSeriesScreen()

@Composable
private fun CompactScreen(component: SeriesComponent) {
val seriesState by component.seriesState.collectAsStateWithLifecycle()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.datlag.burningseries.ui.screen.initial.series

import androidx.compose.runtime.Composable

@Composable
actual fun EnterSeriesScreen() { }
21 changes: 21 additions & 0 deletions extension/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
alias(libs.plugins.multiplatform)
alias(libs.plugins.serialization)
alias(libs.plugins.ktorfit)
}

val artifact = VersionCatalog.artifactName("extension")
group = artifact

kotlin {
js(IR) {
browser()
binaries.executable()
}

sourceSets {
jsMain.get().dependencies {
runtimeOnly(npm("webextension-polyfill", "0.10.0"))
}
}
}
55 changes: 55 additions & 0 deletions extension/src/jsMain/resources/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"manifest_version": 3,

"name": "Burning-Series Activate",
"description": "Burning-Series Activate is an extension to activate episodes for the app in your browser.",
"version": "0.0.0",

"action": {
"default_icon": "icons/launcher_128.png",
"default_popup": "popup.html"
},

"icons": {
"16": "icons/launcher_16.png",
"32": "icons/launcher_32.png",
"48": "icons/launcher_48.png",
"64": "icons/launcher_64.png",
"96": "icons/launcher_96.png",
"128": "icons/launcher_128.png"
},

"content_scripts": [
{
"matches": [
"*://*.bs.to/serie/*",
"*://*.burningseries.co/serie/*",
"*://*.burningseries.sx/serie/*",
"*://*.burningseries.ac/serie/*",
"*://*.burningseries.vc/serie/*",
"*://*.burningseries.cx/serie/*",
"*://*.burningseries.nz/serie/*",
"*://*.burningseries.se/serie/*",
"*://*.burningseries.tw/serie/*"
],
"js": [
"browser-polyfill.min.js",
"content_script.js"
]
}
],

"background": {
"service_worker": "background.js"
},

"permissions": [
"storage",
"background"
],

"host_permissions": [
"https://jsonbase.com/*",
"https://*.googleapis.com/*"
]
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ parcelable = "1.2.0"
protobuf = "0.9.4"
protoc = "3.24.4"
realm = "1.12.0"
sekret = "0.1.1"
sekret = "0.2.0"
serialization-json = "1.6.0"
splashscreen = "1.0.1"
sqldelight = "2.0.0"
Expand Down
9 changes: 9 additions & 0 deletions model/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ kotlin {
iosX64()
iosArm64()
iosSimulatorArm64()
js(IR) {
browser()
nodejs()
binaries.executable()
}

jvmToolchain(CompileOptions.jvmTargetVersion)

Expand Down Expand Up @@ -43,6 +48,10 @@ kotlin {
jvmMain.get().dependencies {
api(libs.lang)
}

jsMain.get().dependencies {
api(libs.coroutines.js)
}
}
}

Expand Down
20 changes: 18 additions & 2 deletions network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ kotlin {
iosX64()
iosArm64()
iosSimulatorArm64()
js(IR) {
browser()
nodejs()
binaries.executable()
}

jvmToolchain(CompileOptions.jvmTargetVersion)

Expand All @@ -34,13 +39,23 @@ kotlin {
api(libs.ktsoup)
api(libs.ktsoup.fs)
api(libs.ktsoup.ktor)
api(libs.realm)
api(libs.realm.sync)
implementation(libs.jsunpacker)
api(libs.firebase.auth)
api(libs.firebase.store)
}
}

val realmCompatible by creating {
dependsOn(commonMain)
dependencies {
api(libs.realm)
api(libs.realm.sync)
}
}

jvmMain.get().dependsOn(realmCompatible)
androidMain.get().dependsOn(realmCompatible)
nativeMain.get().dependsOn(realmCompatible)
}
}

Expand Down Expand Up @@ -75,4 +90,5 @@ dependencies {
add("kspIosX64", libs.ktorfit.ksp)
add("kspIosArm64", libs.ktorfit.ksp)
add("kspIosSimulatorArm64", libs.ktorfit.ksp)
add("kspJs", libs.ktorfit.ksp)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.datlag.burningseries.network.realm

expect class RealmLoader {
suspend fun login()

suspend fun loadEpisodes(hosterHref: List<String>): List<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@ import dev.datlag.burningseries.model.state.EpisodeState
import dev.datlag.burningseries.network.Firestore
import dev.datlag.burningseries.network.JsonBase
import dev.datlag.burningseries.network.firebase.FireStore
import dev.datlag.burningseries.network.realm.RealmLoader
import dev.datlag.burningseries.network.scraper.Video
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.auth.auth
import dev.gitlive.firebase.firestore.FirebaseFirestore
import io.ktor.client.*
import io.realm.kotlin.mongodb.App
import io.realm.kotlin.mongodb.Credentials
import io.realm.kotlin.mongodb.ext.call
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import org.mongodb.kbson.BsonDocument

@OptIn(ExperimentalCoroutinesApi::class)
class EpisodeStateMachine(
private val client: HttpClient,
private val jsonBase: JsonBase,
private val app: App?,
private val realmLoader: RealmLoader,
private val firestore: FirebaseFirestore?,
private val firestoreApi: Firestore?
) : FlowReduxStateMachine<EpisodeState, EpisodeAction>(initialState = EpisodeState.Waiting) {
Expand All @@ -36,11 +33,7 @@ class EpisodeStateMachine(
spec {
inState<EpisodeState.Waiting> {
onEnterEffect {
if (NetworkStateSaver.mongoUser == null) {
NetworkStateSaver.mongoUser = suspendCatching {
app?.login(Credentials.anonymous())
}.getOrNull()
}
realmLoader.login()
if (NetworkStateSaver.firebaseUser == null) {
NetworkStateSaver.firebaseUser = suspendCatching {
Firebase.auth.signInAnonymously().user
Expand Down Expand Up @@ -76,11 +69,7 @@ class EpisodeStateMachine(
val mongoHoster = NetworkStateSaver.mongoHosterMap[episodeHref] ?: emptyList()
val mongoDBResults = async {
mongoHoster.ifEmpty {
val newList = suspendCatching {
val doc = NetworkStateSaver.mongoUser!!.functions.call<BsonDocument>("query", hosterHref.toTypedArray())
doc.getArray("result").values.map { it.asDocument().getString("url").value }
}.getOrNull() ?: emptyList()

val newList = realmLoader.loadEpisodes(hosterHref)
NetworkStateSaver.mongoHosterMap[episodeHref] = newList
newList
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package dev.datlag.burningseries.network.state

import dev.gitlive.firebase.auth.FirebaseUser
import io.realm.kotlin.mongodb.User

data object NetworkStateSaver {

var mongoUser: User? = null
var firebaseUser: FirebaseUser? = null

val mongoHosterMap: MutableMap<String, List<String>> = mutableMapOf()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.datlag.burningseries.network.realm

actual object RealmLoader {
actual suspend fun login() { }

actual suspend fun loadEpisodes(hosterHref: List<String>): List<String> {
return emptyList()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.datlag.burningseries.network.realm

import dev.datlag.burningseries.model.common.suspendCatching
import dev.datlag.burningseries.network.state.NetworkStateSaver
import io.realm.kotlin.mongodb.App
import io.realm.kotlin.mongodb.Credentials
import io.realm.kotlin.mongodb.User
import org.mongodb.kbson.BsonDocument
import io.realm.kotlin.mongodb.ext.call

actual class RealmLoader(private val app: App?) {
actual suspend fun login() {
if (mongoUser == null) {
mongoUser = suspendCatching {
app?.login(Credentials.anonymous())
}.getOrNull()
}
}

actual suspend fun loadEpisodes(hosterHref: List<String>): List<String> {
return suspendCatching {
val doc = mongoUser?.functions?.call<BsonDocument>("query", hosterHref.toTypedArray())
doc?.getArray("result")?.values?.map { it.asDocument().getString("url").value }
}.getOrNull() ?: emptyList()
}

companion object {
private var mongoUser: User? = null
}
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ include(":network")
include(":color")
include(":database")

include(":extension")

pluginManagement {
repositories {
google()
Expand Down

0 comments on commit 1dab7a4

Please sign in to comment.