Skip to content

Commit

Permalink
fix: no title in some html
Browse files Browse the repository at this point in the history
  • Loading branch information
sgpublic committed Jul 15, 2024
1 parent 326453d commit 76d43cf
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions server/src/main/kotlin/io/github/sgpublic/agentgate/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ object Config: CliktCommand(
}
var logo = "/favicon.ico"
HOME_HTML?.let { html ->
val links = html.head().getElementsByTag("link")
val links = html.head()
.getElementsByTag("link")
var size = -1
for (link in links) {
try {
Expand Down Expand Up @@ -114,9 +115,34 @@ object Config: CliktCommand(
return@lazy _AGENT_GATE_TARGET_NAME!!
}
var name = BuildConfig.APPLICATION_ID
HOME_HTML?.let { html ->
HOME_HTML?.let html@{ html ->
try {
name = html.title()
html.title()
.takeIf { it.isNotBlank() }
?.let {
name = it
return@html
}

val metaProperty = setOf("og:title")
val metaName = setOf("twitter:title")
for (element in html.getElementsByTag("meta")) {
if (metaProperty.contains(element.attribute("property").value)) {
element.attribute("content").value
.takeIf { it.isNotBlank() }
?.let {
name = it
return@html
}
} else if (metaName.contains(element.attribute("name").value)) {
element.attribute("content").value
.takeIf { it.isNotBlank() }
?.let {
name = it
return@html
}
}
}
} catch (e: Exception) {
log.debug("failed reading title from home page of target service", e)
}
Expand Down

0 comments on commit 76d43cf

Please sign in to comment.