Advertisement
omegastripes

test_synchronous_top_50_ping

Aug 10th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Main
  3.  
  4. Sub Main()
  5.    
  6.     Dim arrUrls, objWnd, lWidth, lHeight, i, objNode, j, sIp, sRt
  7.    
  8.     ' get top 50 websites urls array from similarweb.com
  9.     arrUrls = GetTopSites()
  10.     ' create window for output
  11.     Set objWnd = CreateWindow()
  12.     ' set up window
  13.     With objWnd
  14.         With .Document
  15.             .GetElementsByTagName("head")(0).AppendChild .CreateElement("style")
  16.             .stylesheets(0).cssText = "body, #output {font-family: consolas, courier new; font-size: 9pt;} body {background-color: buttonface;} #output {height: 100%; width: 100%; overflow: scroll; background: #FFF;}"
  17.             .Title = "similarweb.com Top 50 Pings"
  18.             .Body.InnerHtml = "<div id='output'></div>"
  19.         End With
  20.         lWidth = CInt(.Screen.AvailWidth * 0.75)
  21.         lHeight = CInt(.Screen.AvailHeight * 0.75)
  22.         .ResizeTo .Screen.AvailWidth, .Screen.AvailHeight
  23.         .ResizeTo lWidth + .Screen.AvailWidth - .Document.Body.OffsetWidth, lHeight + .Screen.AvailHeight - .Document.Body.OffsetHeight
  24.         .MoveTo CInt((.Screen.AvailWidth - lWidth) / 2), CInt((.Screen.AvailHeight - lHeight) / 2)
  25.     End With
  26.     ' ping each url, create divs in window document, output results
  27.     On Error Resume Next
  28.     For i = 1 To UBound(arrUrls)
  29.         ' create div
  30.         Set objNode = objWnd.Document.CreateElement("div")
  31.         objWnd.output.AppendChild objNode
  32.         objNode.innerHTML = "<span style='width: 200px;'>" & arrUrls(i) & "</span>"
  33.         ' make ping
  34.         Ping arrUrls(i), sIp, sRt
  35.         ' output result
  36.         If sRt = "" Then
  37.             objNode.innerHTML = objNode.innerHTML & "<span style='color: red;'>n/a</span>"
  38.         Else
  39.             objNode.innerHTML = objNode.innerHTML & sIp & " " & sRt & " msec"
  40.         End If
  41.         If TypeName(objWnd) = "Object" Then Exit For ' window closed
  42.     Next
  43. End sub
  44.  
  45. Function GetTopSites()
  46.     Dim arrUrls, i
  47.     ' request rating from similarweb
  48.     With CreateObject("Msxml2.ServerXMLHTTP.3.0")
  49.         .SetOption 2, 13056 ' SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
  50.         .Open "GET", "https://www.similarweb.com/global", False
  51.         .Send
  52.         ' parse response to array
  53.         arrUrls = Split(.ResponseText, "<tr id=""")
  54.     End With
  55.     For i = 1 To UBound(arrUrls)
  56.         arrUrls(i) = Split(arrUrls(i), """", 2)(0)
  57.     Next
  58.     GetTopSites = arrUrls
  59. End Function
  60.  
  61. Function CreateWindow()
  62.     ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
  63.     Dim sSignature, oShellWnd, oProc
  64.     On Error Resume Next
  65.     sSignature = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
  66.     Do
  67.         Set oProc = CreateObject("WScript.Shell").Exec("mshta about:""about:<head><script>moveTo(-32000,-32000);document.title=' '</script><hta:application id=app border=thick minimizebutton=no maximizebutton=no scroll=no showintaskbar=yes contextmenu=no selection=yes innerborder=no icon=""""/><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('" & sSignature & "',document.parentWindow);</script></head>""")
  68.         Do
  69.             If oProc.Status > 0 Then Exit Do
  70.             For Each oShellWnd In CreateObject("Shell.Application").Windows
  71.                 Set CreateWindow = oShellWnd.GetProperty(sSignature)
  72.                 If Err.Number = 0 Then Exit Function
  73.                 Err.Clear
  74.             Next
  75.         Loop
  76.     Loop
  77. End Function
  78.  
  79. Sub Ping(strUrl, strProtocolAddress, lngResponseTime)
  80.     Dim objStatus
  81.     For Each objStatus In GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * From Win32_PingStatus Where Address = '" & strUrl & "'")
  82.         Select Case True
  83.             Case IsNull(objStatus.StatusCode)
  84.             Case objStatus.StatusCode <> 0
  85.             Case Else
  86.                 strProtocolAddress = objStatus.ProtocolAddress
  87.                 lngResponseTime = objStatus.ResponseTime
  88.         End Select
  89.     Next
  90. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement