Results 1 to 4 of 4

Thread: How can I close a tab programmatically for Google Chrome?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Question How can I close a tab programmatically for Google Chrome?

    I have a program that:


    Reads the history of Chrome File then, compares that history to a text file, and if any websites from the history match the websites in the text file, a
    Code:
    MessageBox
    shows up which says the URL detected.



    This is the code used in that program



    Code:
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim google As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\Google\Chrome\User Data\Default\History")
        Dim fileName As String = DateTime.Now.Ticks.ToString
        File.Copy(google, (Application.StartupPath + ("\" + fileName)))
        Dim dt As New DataTable()
        Using con As SQLiteConnection = New SQLiteConnection($"DataSource = {Application.StartupPath }\{fileName};Versio=3;New=False;Compress=True;")
            Using cmd As New SQLiteCommand("select * from urls order by last_visit_time desc ", con)
                con.Open()
                Using dr As SQLiteDataReader = cmd.ExecuteReader()
                    dt.Load(dr)
                End Using
            End Using
        End Using
        Dim lstGoogleHistory As New List(Of String)
        For Each row As DataRow In dt.Rows
            lstGoogleHistory.Add(row("url").ToString)
        Next
        Dim WhiteListText As String = My.Computer.FileSystem.ReadAllText("websitelist.txt").ToString
        Dim InWhiteList = From uri In lstGoogleHistory
                          Where uri.All(Function(x) WhiteListText.Contains(uri))
                          Select uri
        For Each URL In InWhiteList
            MessageBox.Show(URL)
        Next
    End Sub

    It works great and does what I want it to do, however, I'm having trouble finding a solution for another little feature I want added, and that is, if a website is detected, the tab which the URL is located in will close.


    I've had lots of trouble trying to find a solution for this, but I've found one which I don't think is a good way of doing it, and plus, it gives me the 'Access Denied' exception when used (yes my program is ran as administrator.)


    Code:
    SendKeys.Send("^w")

    Is there a way to use Chrome's Task Manager to close the tab? And if so, how can this be done?

    Or, could I send Control W to Chrome only?
    Last edited by Modulus; Jul 18th, 2018 at 10:11 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,689

    Re: How can I close a tab programmatically for Google Chrome?

    There are ways of manipulating IE tabs, but I'm not aware of any with Chrome.

    Name:  Untitled.jpg
Views: 2595
Size:  7.9 KB

    Try getting the tab window handle, and calling...
    (find the structure with Spy++)

    Dim WM_CLOSE As UInteger = &H10
    PostMessage(handle, WM_CLOSE, 0, 0)

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Re: How can I close a tab programmatically for Google Chrome?

    Ok, thanks.


    Maybe there is a way to send Control W to Chrome only?

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Re: How can I close a tab programmatically for Google Chrome?

    Quote Originally Posted by .paul. View Post
    There are ways of manipulating IE tabs, but I'm not aware of any with Chrome.

    Name:  Untitled.jpg
Views: 2595
Size:  7.9 KB

    Try getting the tab window handle, and calling...
    (find the structure with Spy++)

    Dim WM_CLOSE As UInteger = &H10
    PostMessage(handle, WM_CLOSE, 0, 0)

    Ok, thanks.


    Maybe there is a way to send Control W to Chrome only?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width