Описание: Автоматизация Custom Webview с помощью jSoup
Название: jSoup для Basic4android - Часть 2 Автор: я (c1cl0n) Формат: AVI Разрешение: 1920x1080 Продолжительность: 31 мин. Качество (битрейт): 3492 Kbit/AVC/16:9/60fps/AVI(H264) Страна: Россия
Видео показывает как автоматизировать процессы ввода текста, нажатия на кнопочки (галочки итп) на любой HTML странице. По сути - это бот для Android, по факту - удобное средство парсинга и автоматизации рутинных процессов
Sub Class_Globals Private mEventName As String, mCallBack As Object Private mBase As Panel, mProperties As Map Private WebView As WebView, WebViewExtras As WebViewExtras, CookieManager As CookieManager End Sub
Private Sub RisePageFinished(Text As String) If SubExists(mCallBack, $"${mEventName}_PageFinished"$) Then CallSubDelayed2(mCallBack, $"${mEventName}_PageFinished"$, Text) End If End Sub
Public Sub Initialize (Callback As Object, EventName As String) mEventName = EventName mCallBack = Callback
WebView.Initialize($"WebView"$) CookieManager.SetAcceptCookies(True) WebView.javascriptEnabled = True WebViewExtras.addjavascriptInterface(WebView, $"JSInterface"$) End Sub
Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map) mProperties = Props mBase = Base mBase.AddView(WebView, 0, 0, mBase.Width, mBase.Height) End Sub
Public Sub getHtml() As String Return mProperties.Get($"Html"$) End Sub
Public Sub setHtml(Text As String) mProperties.Put($"Html"$, Text) End Sub
Public Sub getUrl() As String Return mProperties.Get($"Url"$) End Sub
Public Sub setUrl(Text As String) mProperties.Put($"Url"$, Text) End Sub
Public Sub getCookies() As String Return mProperties.Get($"Cookies"$) End Sub
Public Sub setCookies(Cookies As String) mProperties.put($"Cookies"$, Cookies) End Sub
Public Sub ClearCookies() CookieManager.RemoveAllCookies mProperties.put($"Cookies"$, "") End Sub
Public Sub Title() WebViewExtras.executejavascript(WebView, $"JSInterface.CallSub('DocumentHTML', false, document.title)"$) Wait For DocumentHTML (ResponseHtml As String) End Sub
Public Sub LoadHtml() WebView.LoadHtml(mProperties.Get($"Html"$)) Wait For WebView_PageFinished (ResponseUrl As String) Log("URL: " & ResponseUrl) setUrl(ResponseUrl) WebViewExtras.executejavascript(WebView, $"JSInterface.CallSub('DocumentHTML', false, document.documentElement.outerHTML)"$) Wait For DocumentHTML (ResponseHtml As String) setHtml(ResponseHtml) RisePageFinished(ResponseHtml) End Sub
Public Sub LoadUrl() WebView.LoadUrl(mProperties.Get($"Url"$)) Wait For WebView_PageFinished (ResponseUrl As String) If CookieManager.HasCookies Then setCookies(CookieManager.GetCookie(ResponseUrl)) setUrl(ResponseUrl) WebViewExtras.executejavascript(WebView, $"JSInterface.CallSub('DocumentHTML', false, document.documentElement.outerHTML)"$) Wait For DocumentHTML (ResponseHtml As String) setHtml(ResponseHtml) RisePageFinished(ResponseHtml) End Sub
Public Sub Inject(Tag As String, Selector As String, Attribute As String, Value As String) Dim JScript As StringBuilder JScript.Initialize JScript.Append($"var x = document.querySelectorAll("${Tag}");"$) JScript.Append($"var i;"$) JScript.Append($"for (i = 0; i < x.length; i++) {"$) JScript.Append($"if (x[i].matches("${Selector}") ) {"$) JScript.Append($"document.getElementsByTagName("${Tag}")[i].${Attribute} = "${Value}";"$) JScript.Append($"break;"$) JScript.Append($"}"$) JScript.Append($"}"$) WebViewExtras.executejavascript(WebView, JScript.ToString) End Sub
Public Sub InjectByIndex(Tag As String, Selector As String, Index As Int, Attribute As String, Value As String) Dim JScript As StringBuilder JScript.Initialize JScript.Append($"var x = document.querySelectorAll("${Tag}");"$) JScript.Append($"var i;"$) JScript.Append($"if (x[${Index}].matches("${Selector}") ) {"$) JScript.Append($"document.getElementsByTagName("${Tag}")[${Index}].${Attribute} = "${Value}";"$) JScript.Append($"}"$) WebViewExtras.executejavascript(WebView, JScript.ToString) End Sub
Public Sub Click(Tag As String, Selector As String, WaitFor As Boolean) Dim JScript As StringBuilder JScript.Initialize JScript.Append($"var x = document.querySelectorAll("${Tag}");"$) JScript.Append($"var i;"$) JScript.Append($"for (i = 0; i < x.length; i++) {"$) JScript.Append($"if (x[i].matches("${Selector}") ) {"$) JScript.Append($"document.getElementsByTagName("${Tag}")[i].click();"$) JScript.Append($"break;"$) JScript.Append($"}"$) JScript.Append($"}"$) WebViewExtras.executejavascript(WebView, JScript.ToString) If WaitFor Then Wait For WebView_PageFinished (ResponseUrl As String) setUrl(ResponseUrl) WebViewExtras.executejavascript(WebView, $"JSInterface.CallSub('DocumentHTML', false, document.documentElement.outerHTML)"$) Wait For DocumentHTML (ResponseHtml As String) setHtml(ResponseHtml) RisePageFinished(ResponseHtml) End If End Sub