Skip to content

Commit

Permalink
build: 2.0.0-M1
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Dec 15, 2021
1 parent 67625fe commit 2a2bf6f
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 330 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [Mirai Selenium Plugin](https://github.com/cssxsh/mirai-selenium-plugin)

> 基于 [MxLib](https://github.com/Karlatemp/MxLib)Mirai Selenium 前置插件
> Mirai Selenium 前置插件
Mirai-Console的前置插件,用于使用Selenium调用浏览器进行截图等

Expand All @@ -13,8 +13,10 @@ Mirai-Console的前置插件,用于使用Selenium调用浏览器进行截图
| Windows | Chrome | Yes |
| Windows | Firefox | Yes |
| Windows | Edge | Yes |
| Linux | Chrome | Yes |
| Linux | Firefox | Yes |
| MacOS | Chrome | Yes |
| MacOS | Chrome | Test |
| MacOS | Firefox | Test |

## 在插件项目中引用

Expand Down
8 changes: 1 addition & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "xyz.cssxsh.mirai"
version = "1.0.5"
version = "2.0.0-M1"

mavenCentralPublish {
useCentralS01()
Expand All @@ -27,12 +27,6 @@ repositories {
}

dependencies {
implementation("io.github.karlatemp.mxlib:mxlib-selenium:3.0-dev-20") {
exclude("org.seleniumhq.selenium")
exclude("junit")
exclude("classworlds")
exclude("io.netty")
}
api("org.seleniumhq.selenium:selenium-java:4.1.0") {
exclude("io.netty")
}
Expand Down
27 changes: 9 additions & 18 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/MiraiSeleniumPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package xyz.cssxsh.mirai.plugin

import io.github.karlatemp.mxlib.*
import io.github.karlatemp.mxlib.exception.*
import io.github.karlatemp.mxlib.logger.*
import kotlinx.coroutines.*
import net.mamoe.mirai.console.extension.*
import net.mamoe.mirai.console.plugin.jvm.*
Expand All @@ -11,12 +8,13 @@ import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext
import net.mamoe.mirai.utils.*
import xyz.cssxsh.mirai.plugin.data.*
import xyz.cssxsh.selenium.*
import java.util.logging.*

object MiraiSeleniumPlugin : KotlinPlugin(
JvmPluginDescription(
id = "xyz.cssxsh.mirai.plugin.mirai-selenium-plugin",
name = "mirai-selenium-plugin",
version = "1.0.5",
version = "2.0.0-M1",
) {
author("cssxsh")
}
Expand All @@ -27,24 +25,22 @@ object MiraiSeleniumPlugin : KotlinPlugin(
/**
* 初始化 Selenium
*
* 如果 是 [io.github.karlatemp.mxlib.selenium.MxSelenium] 不支持的环境,
* 请自行实现 初始化方法(判断浏览器类型,下载驱动,配置路径)
*
* @see [setupSelenium]
* @see [setupWebDriver]
*/
fun setup(flush: Boolean = false): Boolean = synchronized(this) {
if (!flush && installed) return true

MiraiSeleniumConfig.reload()
installed = false

val folder = dataFolder.resolve("selenium")
System.setProperty(SELENIUM_FOLDER, folder.absolutePath)
try {
setupSelenium(browser = MiraiSeleniumConfig.browser, factory = MiraiSeleniumConfig.factory)
setupWebDriver(browser = MiraiSeleniumConfig.browser)
installed = true
} catch (exception: UnsupportedOperationException) {
logger.warning({ "浏览器不受支持 $exception" }, exception)
logger.warning({ "浏览器 ${MiraiSeleniumConfig.browser} 不受支持" }, exception)
} catch (cause: Throwable) {
logger.warning({ "初始化浏览器驱动失败 $cause" }, cause)
logger.warning({ "初始化浏览器驱动失败" }, cause)
}

return installed
Expand All @@ -60,12 +56,7 @@ object MiraiSeleniumPlugin : KotlinPlugin(
@OptIn(ConsoleExperimentalApi::class)
override fun PluginComponentStorage.onLoad() {
KtorContext = childScopeContext(name = "SeleniumHttpClient", context = Dispatchers.IO)
try {
MxLib.setLoggerFactory { name -> NopLogger(name) }
MxLib.setDataStorage(dataFolder)
} catch (_: ValueInitializedException) {
//
}
SeleniumLogger.level = Level.OFF
}

override fun onEnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object MiraiSeleniumConfig : ReadOnlyPluginConfig("MiraiSeleniumConfig"), Remote
override val log: Boolean by value(false)

@ValueName("browser")
@ValueDescription("指定使用的浏览器,Chrome/Firefox")
@ValueDescription("指定使用的浏览器,Chrome/Firefox/Edge")
override val browser: String by value("")

@ValueName("factory")
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/xyz/cssxsh/selenium/KtorHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.net.URI
class KtorHttpClient(private val config: SeleniumHttpClientConfig) : SeleniumHttpClient {

@OptIn(KtorExperimentalAPI::class)
val client = HttpClient(OkHttp) {
private val client = HttpClient(OkHttp) {
engine {
proxy = config.proxy()
}
Expand Down Expand Up @@ -92,6 +92,10 @@ class KtorHttpClient(private val config: SeleniumHttpClientConfig) : SeleniumHtt

@SeleniumHttpClientName("ktor")
class Factory : SeleniumHttpClientFactory {

init {
System.setProperty("io.ktor.random.secure.random.provider", "DRBG")
}

private val clients = mutableListOf<HttpClient>()

Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/xyz/cssxsh/selenium/RegisterKeys.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package xyz.cssxsh.selenium

object RegisterKeys {
const val USER_CHOICE =
"""HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Shell\Associations\URLAssociations\https\UserChoice|ProgId"""
const val EDGE =
"""HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon|version"""
const val CHROME =
"""HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\BLBeacon|version"""
const val CHROMIUM =
"""HKEY_CURRENT_USER\SOFTWARE\Chromium\BLBeacon|version"""
const val FIREFOX =
"""HKEY_CURRENT_USER\SOFTWARE\Mozilla\Mozilla Firefox|CurrentVersion"""
}
Loading

0 comments on commit 2a2bf6f

Please sign in to comment.