package com.shsening.autouploadvideos.action
|
|
|
import android.app.Instrumentation
|
import android.content.Context
|
import android.os.SystemClock
|
import android.os.Handler
|
import com.shsening.autouploadvideos.UploadFragment
|
import com.shsening.autouploadvideos.UploadSessionActivity
|
import com.shsening.autouploadvideos.utils.Utils
|
import com.shsening.autouploadvideos.WebviewUtils
|
|
class ActionShipinhao (private var activity: UploadSessionActivity, private var threadHandler: Handler, private var videoInfo: Utils.VideoUploadTaskInfo) :PlatformActionInterface{
|
|
private var webView = activity.sessionProfileWebView
|
|
|
var jsCode_CheckContentLoaded = """
|
document.querySelector("#container-wrap > div.container-center > div > div > div.main-body-wrap.post-create > div.main-body > div > div.post-edit-wrap.material-edit-wrap > div.material > div > div > div > span > div > span > input[type=file]") !== null
|
""".trimIndent()
|
var jsCode_CheckVideoUploadFinished = """
|
document.querySelector("#container-wrap > div.container-center > div > div > div.main-body-wrap.post-create > div.main-body > div > div.post-edit-wrap.material-edit-wrap > div.form > div:nth-child(1) > div.form-item-body > div > div.video-cover > div.weui-desktop-popover__wrp.pure-text-popover > span > div") !==null
|
""".trimIndent()
|
var jsCode_PublishVideo_Finished ="document.querySelector('#footer > div.main-part > div.left-links > a:nth-child(3)') !==null"
|
|
var jsPath_PublishVideo ="#container-wrap > div.container-center > div > div > div.main-body-wrap.post-create > div.main-body > div > div.post-edit-wrap.material-edit-wrap > div.form > div.form-btns > div:nth-child(5) > span > div > button"
|
|
|
/**
|
* setting video description information .
|
* Title,description(including # and @ ) publish datetime etc
|
*/
|
private fun updatePostDescription(){
|
WebviewUtils.addValueToDiv(
|
webView,
|
"#container-wrap > div.container-center > div > div > div.main-body-wrap.post-create > div.main-body > div > div.post-edit-wrap.material-edit-wrap > div.form > div:nth-child(2) > div.form-item-body > div > div.input-editor",
|
videoInfo.description
|
)
|
|
|
// publish later radiobox
|
WebviewUtils.simpleElementClick(
|
webView,
|
"#container-wrap > div.container-center > div > div > div.main-body-wrap.post-create > div.main-body > div > div.post-edit-wrap.material-edit-wrap > div.form > div.post-time-wrap > div:nth-child(1) > div.form-item-body > div > label:nth-child(2)"
|
)
|
|
addValueToDatePickerInput(true,"#container-wrap > div.container-center > div > div > div.main-body-wrap.post-create > div.main-body > div > div.post-edit-wrap.material-edit-wrap > div.form > div.post-time-wrap > div:nth-child(2) > div.form-item-body > dl > dd",videoInfo.datetime)
|
|
|
// 在主线程中设置延迟执行
|
threadHandler.postDelayed(Runnable {
|
startToPublishVideo()
|
}, 2000)
|
}
|
|
|
override fun checkIfContentLoaded() {
|
webView.postDelayed({
|
webView.evaluateJavascript(jsCode_CheckContentLoaded) { result ->
|
if (result == "true") {
|
simulateSwipeAndUploadVideo()
|
} else {
|
webView.postDelayed({
|
checkIfContentLoaded()
|
}, 1000) // 1秒后再次检查
|
}
|
}
|
}, 1000) // 每秒检查一次
|
}
|
|
private fun startToPublishVideo(){
|
WebviewUtils.simpleElementClick(webView, jsPath_PublishVideo)
|
checkVideoPublishFinished()
|
}
|
private fun checkVideoUploadFinished(){
|
webView.postDelayed({
|
webView.evaluateJavascript(jsCode_CheckVideoUploadFinished) { result ->
|
if (result == "true") {
|
// 等待视频检测和生成封面
|
threadHandler.postDelayed({updatePostDescription()},3000)
|
|
} else {
|
// 元素不存在,1秒后再次检查
|
webView.postDelayed({
|
checkVideoUploadFinished()
|
}, 1000) // 1秒后再次检查
|
}
|
}
|
}, 1000) // 每秒检查一次
|
}
|
private fun checkVideoPublishFinished(){
|
webView.postDelayed({
|
webView.evaluateJavascript(jsCode_PublishVideo_Finished) { result ->
|
if (result == "true") {
|
// video published successfully!!!
|
activity.checkAndStartNextTask()
|
} else {
|
// 元素不存在,1秒后再次检查
|
webView.postDelayed({
|
checkVideoPublishFinished()
|
}, 1000) // 1秒后再次检查
|
}
|
}
|
}, 1000) // 每秒检查一次
|
}
|
|
|
private fun addValueToDatePickerInput(first:Boolean, jsPath: String, targetValue: String) {
|
|
var datetimes = Utils.validateAndReturnDateTime(targetValue) ?: return
|
|
var dpPanelText = datetimes[0].toString()+"年 "+datetimes[1].toString()+"月";
|
|
var dateRowColum = Utils.getDatePosition(datetimes[0], datetimes[1], datetimes[2]) ?:return;
|
|
val jsCode = """
|
try {
|
var datePanel = document.querySelector('${jsPath}');
|
datePanel.style.display='block';
|
|
// need time to popup date-picker
|
setTimeout(() => {
|
var datePanelTitle = datePanel.querySelector(' div > div');
|
if (datePanelTitle && datePanelTitle.innerText != '${dpPanelText}'){
|
datePanel.querySelector('div div >button:nth-child(4)').click()
|
}
|
datePanel.querySelector('div > div:nth-child(2) > table > tbody > tr:nth-child(${dateRowColum[0]+1}) > td:nth-child(${dateRowColum[1]}) > a').click()
|
|
//switch to time selection
|
var timePanel= datePanel.querySelector('div > div:nth-child(3) > dl > dd ');
|
timePanel.style.display='block';
|
setTimeout(() => {
|
timePanel.querySelector('ol:nth-child(2) > li:nth-child(${(datetimes[3]+1)})').click()
|
timePanel.querySelector('ol:nth-child(3) > li:nth-child(${(datetimes[4]+1)})').click();
|
}, 500);
|
|
}, 500);
|
} catch (error) {
|
console.error('发生错误:', error);
|
}
|
"""
|
webView.evaluateJavascript(jsCode,null)
|
|
|
}
|
|
private fun simulateSwipeAndUploadVideo() {
|
val instrumentation = Instrumentation()
|
val displayMetrics = activity.resources.displayMetrics
|
val width = displayMetrics.widthPixels
|
val height = displayMetrics.heightPixels
|
|
val startX = width.toFloat() * 0.9f // 从右侧滑动
|
val endX = width.toFloat() * 0.7f // 滑动到左侧
|
val y = height / 3f // Y 坐标在屏幕中间
|
|
Thread {
|
Utils.instrumentationSwipe(instrumentation, startX, y, endX, y)
|
SystemClock.sleep(500)
|
Utils.instrumentationClick(instrumentation, width * 4 / 5f, height / 2f)
|
// back to main thread
|
threadHandler.post {
|
checkVideoUploadFinished()
|
}
|
}.start()
|
}
|
|
}
|