' Получение координат элемента
Public Async Function GetCoord(sXPath As String) As Task(Of CResult)
Dim oTask As Task(Of JavascriptResponse), sJSRes As String, sJSErr As String, sFrameUrl As String, sArr() As String
Dim iX As Double, iY As Double, iR As Double, iB As Double
sXPath = Replace(sXPath, """", "'")
If sXPath.Contains(vbTab) Then sFrameUrl = Split(sXPath, vbTab)(0) : sXPath = Split(sXPath, vbTab)(1) ' Есть Frame
Call AddLog("Get Coordinates to '" & sXPath & "'")
oTask = Me.GetFrame(sFrameUrl).EvaluateScriptAsync("(function ()
{
var element = document.evaluate(""" & sXPath & """, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var position = element.getBoundingClientRect();
var x = position.left;
var y = position.top;
var r = position.right;
var b = position.bottom;
return x + ' ' + y + ' ' + r + ' ' + b;
})();
", Nothing)
Await oTask.ContinueWith(Sub(t) ' Выполнение JS
If Not t.IsFaulted Then ' Таск ещё работает
Dim response As JavascriptResponse = t.Result
If response.Success Then ' Выполнено успешно
If IsNothing(response.Result) Then ' Нулевой ответ
sJSRes = "False"
Else ' Есть ответ
sJSRes = response.Result.ToString()
End If
Else ' Завершено с ошибкой
sJSRes = "False"
sJSErr = response.Message
End If
End If
End Sub, TaskScheduler.FromCurrentSynchronizationContext())
If sJSErr <> "" Then Me.Busy = False : Return New CResult(False, 0, "Error JS: '" & sJSErr & "'") ' Выпонено с ошибкой
sJSRes = sJSRes.Replace(".", ",")
sArr = Split(sJSRes, " ")
If UBound(sArr) <> 3 Then Return New CResult(False, 0, "Invalid number of elements") ' Выпонено с ошибкой
If Not Double.TryParse(sArr(0), iX) Then Return New CResult(False, 0, "Invalid X coord of element") ' Выпонено с ошибкой
If Not Double.TryParse(sArr(1), iY) Then Return New CResult(False, 0, "Invalid Y coord of element") ' Выпонено с ошибкой
If Not Double.TryParse(sArr(2), iR) Then Return New CResult(False, 0, "Invalid Width of element") ' Выпонено с ошибкой
If Not Double.TryParse(sArr(3), iB) Then Return New CResult(False, 0, "Invalid Height of element") ' Выпонено с ошибкой
Return New CResult(True, 0, Math.Round(iX, 0) & " " & Math.Round(iY, 0) & " " & Math.Round(iR, 0) & " " & Math.Round(iB, 0))
End Function
private void button1_Click(object sender, EventArgs e)
{
string s = "";
var lines = textBox1.Lines.ToList();
if (lines.Count % 2 == 0)
{
for (int i = 0; i < lines.Count; i+=4)
s = s + string.Format(str, lines[i], lines[i + 1], lines[i + 2], lines[i+ 3]);
}
else
{
MessageBox.Show("Нечетное количество строк", "Ошибка");
}
textBox2.Text = s;
}