Skip to content

Commit

Permalink
fix: echarts
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Aug 2, 2022
1 parent 6bda2c7 commit b5db6a3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {
compileOnly("com.google.auto.service:auto-service-annotations:1.0.1")
compileOnly("net.mamoe:mirai-core-utils:2.12.1")
// test
testImplementation(kotlin("test", "1.6.21"))
testImplementation(kotlin("test"))
testImplementation("org.icepear.echarts:echarts-java:1.0.3")
testCompileOnly("org.projectlombok:lombok:1.18.24")
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/kotlin/xyz/cssxsh/selenium/ECharts.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package xyz.cssxsh.selenium

import com.github.jknack.handlebars.*
import com.github.jknack.handlebars.io.URLTemplateLoader
import com.github.jknack.handlebars.io.*
import org.openqa.selenium.remote.*
import java.net.URL
import java.net.URLDecoder
Expand All @@ -14,7 +14,7 @@ public data class EChartsMeta(
var option: String,
var cdn: String = System.getProperty("xyz.cssxsh.selenium.echarts.cdn", DEFAULT_CDN),
var renderer: EChartsRenderer = EChartsRenderer.canvas,
var delay: Long = 3_000
var duration: Long = 1_000
)

@Suppress("EnumEntryName")
Expand All @@ -33,12 +33,16 @@ public fun RemoteWebDriver.echarts(meta: EChartsMeta): String {
html.writeText(template.apply(meta))
get(html.toURI().toASCIIString())

return executeScript(
"""
await new Promise(r => setTimeout(r, ${meta.delay}));
return chart.getDataURL()
""".trimIndent()
) as String
return executeAsyncScript("""
const duration = arguments[0] || 1000;
const callback = [...arguments].at(-1);
const id = setInterval(() => {
if (window.chart != null) {
clearInterval(id)
callback(chart.getDataURL());
}
}, duration);
""".trimIndent(), meta.duration) as String
}

public fun data(url: String): Pair<String, ByteArray> {
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/echarts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
<div id="display-container">
</div>
<script type="text/javascript">
const sleep = ms => new Promise(r => setTimeout(r, ms));
const chart = echarts.init(document.getElementById("display-container"), null, { renderer: "{{{ renderer }}}" });
window.chart = echarts.init(document.getElementById("display-container"), null, { renderer: "{{{ renderer }}}" });
let option = {{{ option }}}
chart.setOption(option);
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/xyz/cssxsh/selenium/SeleniumTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal abstract class SeleniumTest {
val driver = setupWebDriver(browser = browser).invoke(config)
try {
block(browser, driver)
} catch (cause: Throwable) {
} catch (cause: WebDriverException) {
cause.printStackTrace()
} finally {
driver.quit()
Expand Down

0 comments on commit b5db6a3

Please sign in to comment.