Skip to content

Commit

Permalink
Merge pull request #34 from cssxsh/fix_user_agent
Browse files Browse the repository at this point in the history
fix: RemoteWebDriver
  • Loading branch information
cssxsh authored Aug 22, 2021
2 parents a265028 + 04a53d0 commit ba34f6a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ val MEDIA_REGEX = """(?<=bilibili\.com/bangumi/media/md)(\d+)""".toRegex()

### SeleniumConfig.yml

* `ua` 截图设备UA
* `user_agent` 截图设备UA 默认 iPad `Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1`
* `width` 截图宽度
* `height` 截图高度
* `pixel_ratio` 截图像素比
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliHelperPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.cssxsh.mirai.plugin

import kotlinx.coroutines.*
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
import net.mamoe.mirai.console.data.*
Expand Down Expand Up @@ -54,10 +55,16 @@ object BiliHelperPlugin : KotlinPlugin(
BiliSearchCommand.register()

if (selenium) {
if ("iPad" in SeleniumToolConfig.userAgent) {
logger.error { "最近b站 网页版动态无法处理 IPad UserAgent,请更改为其他 UserAgent" }
}
driver = RemoteWebDriver(config = SeleniumToolConfig)
launch(SupervisorJob()) {
driver.runCatching {
home()
}.onSuccess { version ->
logger.info { "driver agent $version" }
}.onFailure {
logger.warning { "设置主页失败" }
}
}
}

BiliTasker.startAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import net.mamoe.mirai.console.data.*
import xyz.cssxsh.mirai.plugin.tools.*

object SeleniumToolConfig : ReadOnlyPluginConfig("SeleniumConfig"), RemoteWebDriverConfig {
private const val IPAD =
"Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"

@ValueName("user_agent")
@ValueDescription("截图UA")
override val userAgent: String by value(IPAD)
Expand Down
28 changes: 18 additions & 10 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/tools/SeleniumTool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private fun RemoteWebDriverConfig.toConsumer(): DriverConsumer = { capabilities
"height" to height,
"pixelRatio" to pixelRatio
),
"userAgent" to userAgent
"userAgent" to "$userAgent MicroMessenger"
)
)
}
Expand All @@ -89,9 +89,9 @@ private fun RemoteWebDriverConfig.toConsumer(): DriverConsumer = { capabilities
addPreference("devtools.responsive.viewport.width", width)
addPreference("devtools.responsive.viewport.height", height)
addPreference("devtools.responsive.viewport.pixelRatio", pixelRatio)
addPreference("devtools.responsive.userAgent", userAgent)
addPreference("devtools.responsive.userAgent", "$userAgent MicroMessenger")
// XXX responsive 无法调用
addPreference("general.useragent.override", userAgent)
addPreference("general.useragent.override", "$userAgent MicroMessenger")
addArguments("--width=${width}", "--height=${height}")
}
else -> throw IllegalArgumentException("未设置参数的浏览器")
Expand All @@ -102,15 +102,20 @@ private val IS_READY_SCRIPT by lazy { JavaScriptLoader.load("IsReady") }

private val HAS_CONTENT_SCRIPT by lazy { JavaScriptLoader.load("HasContent") }

private val BROWSER_VERSION by lazy { JavaScriptLoader.load("BrowserVersion") }

private val Init = Duration.ofSeconds(10)

private val Timeout = Duration.ofMinutes(3)

private val Interval = Duration.ofSeconds(10)

private const val HOME_PAGE = "https://m.bilibili.com/detail/508396365455813655"
internal const val HOME_PAGE = "https://t.bilibili.com/h5/dynamic/detail/508396365455813655"

internal const val IPAD =
"Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"

fun RemoteWebDriver(config: RemoteWebDriverConfig, home: String = HOME_PAGE): RemoteWebDriver {
fun RemoteWebDriver(config: RemoteWebDriverConfig): RemoteWebDriver {

val thread = Thread.currentThread()
val oc = thread.contextClassLoader
Expand All @@ -125,11 +130,6 @@ fun RemoteWebDriver(config: RemoteWebDriverConfig, home: String = HOME_PAGE): Re
pageLoadTimeout(Timeout)
scriptTimeout = Interval
}
runBlocking {
withTimeout(Timeout.toMillis()) {
get(home)
}
}
}
}

Expand All @@ -143,6 +143,14 @@ private fun WebDriver.responsive() {
actions.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys("m").keyUp(Keys.SHIFT).keyUp(Keys.CONTROL).perform()
}

suspend fun RemoteWebDriver.home(page: String = HOME_PAGE): Map<String, Boolean> {
withTimeout(Timeout.toMillis()) {
get(page)
}
@Suppress("UNCHECKED_CAST")
return executeScript(BROWSER_VERSION) as Map<String, Boolean>
}

suspend fun RemoteWebDriver.getScreenshot(url: String): ByteArray {
val home = windowHandle
val tab = switchTo().newWindow(WindowType.TAB) as RemoteWebDriver
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// no-unused-vars
const BrowserVersion = () => (window['selfBrowser'] || {})['version'] || {}

0 comments on commit ba34f6a

Please sign in to comment.