2022-05-04 06:22:49 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
|
|
|
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.tgbotapi.webapps.*
|
|
|
|
import io.ktor.client.HttpClient
|
2022-05-04 07:27:39 +00:00
|
|
|
import io.ktor.client.call.receive
|
2022-05-04 06:22:49 +00:00
|
|
|
import io.ktor.client.request.*
|
2022-04-29 14:57:16 +00:00
|
|
|
import io.ktor.client.statement.HttpResponse
|
|
|
|
import io.ktor.client.statement.readText
|
2022-05-04 06:22:49 +00:00
|
|
|
import io.ktor.http.*
|
|
|
|
import io.ktor.http.content.TextContent
|
2022-04-29 14:57:16 +00:00
|
|
|
import kotlinx.browser.document
|
|
|
|
import kotlinx.browser.window
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.dom.appendElement
|
|
|
|
import kotlinx.dom.appendText
|
2022-05-04 06:22:49 +00:00
|
|
|
import org.w3c.dom.HTMLElement
|
2022-04-29 14:57:16 +00:00
|
|
|
|
2022-05-04 07:27:39 +00:00
|
|
|
fun HTMLElement.log(text: String) {
|
|
|
|
appendElement("p", {})
|
|
|
|
appendText(text)
|
|
|
|
}
|
|
|
|
|
2022-04-29 14:57:16 +00:00
|
|
|
fun main() {
|
|
|
|
console.log("Web app started")
|
|
|
|
window.onload = {
|
|
|
|
val scope = CoroutineScope(Dispatchers.Default)
|
|
|
|
runCatching {
|
|
|
|
document.body ?.appendElement("button") {
|
2022-05-04 07:27:39 +00:00
|
|
|
addEventListener("click", {
|
2022-05-04 06:22:49 +00:00
|
|
|
scope.launchSafelyWithoutExceptions {
|
2022-05-04 07:27:39 +00:00
|
|
|
handleResult({ "Clicked" }) {
|
2022-05-10 18:23:14 +00:00
|
|
|
HttpClient().post("${window.location.origin.removeSuffix("/")}/inline") {
|
2022-05-04 06:22:49 +00:00
|
|
|
parameter(webAppQueryIdField, it)
|
2022-05-10 18:23:14 +00:00
|
|
|
setBody(TextContent("Clicked", ContentType.Text.Plain))
|
2022-05-04 07:27:39 +00:00
|
|
|
document.body ?.log(url.build().toString())
|
|
|
|
}.coroutineContext.job.join()
|
2022-05-04 06:22:49 +00:00
|
|
|
}
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|
2022-05-04 07:27:39 +00:00
|
|
|
})
|
2022-04-29 14:57:16 +00:00
|
|
|
appendText("Example button")
|
|
|
|
} ?: window.alert("Unable to load body")
|
|
|
|
webApp.apply {
|
|
|
|
onThemeChanged {
|
2022-05-04 07:27:39 +00:00
|
|
|
document.body ?.log("Theme changed: ${webApp.themeParams}")
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|
|
|
|
onViewportChanged {
|
2022-05-04 07:27:39 +00:00
|
|
|
document.body ?.log("Viewport changed: ${it.isStateStable}")
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
webApp.ready()
|
|
|
|
}.onFailure {
|
|
|
|
window.alert(it.stackTraceToString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|