Last Updated: August 01, 2022
·
3.291K
· extofer

Text Based Web Browser for Programmer’s Forum

The Internet is full of distractions, and when you are heads down coding, the last thing you want is a diversion. On the other hand, the Internet is a useful resource when you are in a bind, as when searching online for assistance on a specific exception in your code.

So how can you avoid having the “squirrel” effect when online searching to resolve your problem? To this, I scripted a Text Based Web Browser in PowerShell to search stackoverflow.com for me. For developers on a Windows environment, PowerShell can be seen as a useful productivity tool. Using the Invoke-WebRequest cmdlet, you can send HTTP request to your site of choice and parse the response to your console.

$search = Invoke-WebRequest "http://stackoverflow.com/search?q=$arg"

The cmdlet returns a response that can be filtered and iterated to write out on the console.

$results = $search.links | where href -like "*/questions/*"  
foreach ($result in $results)
{
  $count += 1 
    if ($result.href -notlike "*/questions/tagged*")
    {
        Write-Host [$count]  -foregroundcolor DarkMagenta
        Write-Host $result.InnerText  
    }
}

Prefacing the results with a number will allow you to then select a result on your console and invoke another Invoke-WebRequest of the results to read the actual questions and answers to the post you selected on the forum.

$getlink = Read-Host "Select the linkt you want to view" 
[int]$i = $getlink-1
# gets content of the selected post
$var = $results[$i].href
Write-Host You selected $results[$i].InnerText uri $var  
$data = Invoke-WebRequest "http://stackoverflow.com$var"

Write-Host ************ Posted Question ************ 
# write the posted question
$data.ParsedHtml.getElementsByTagName("div") |  Where "id" -match "question-header" | Select -ExpandProperty InnerText  
$data.ParsedHtml.getElementsByTagName("div") |  Where "classname" -match "^question" | Select -ExpandProperty InnerText  

Write-Host ************ Posted Answer ************ 
# write the posted accepted-answer first
$data.ParsedHtml.getElementsByTagName("div") |  Where "classname" -match "answer accepted-answer" | Select -ExpandProperty InnerText  

# write the posted all other answers
$data.ParsedHtml.getElementsByTagName("div") |  Where "classname" -match "answer" | Select -ExpandProperty InnerText  

The pinnacle tip to this is, if you are developing on Visual Studio; why not include this utility on the Visual Studio environment?

Picture

Following the How To Add/Edit Environment Variables in Windows 7, you can call your PowerShell script from the Visual Studio Package Manager Console, after all, the PM is a PowerShell console.

Sources:
How To Add/Edit Environment Variables in Windows 7: http://www.nextofwindows.com/how-to-addedit-environment-variables-in-windows-7/
exto-posh-web source code: https://github.com/extofer/exto-posh-web