Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close tab in external web browser?

I am writing a desktop application and in one part I go through OAuth 2.0 flow.

The steps are:

  1. Start process - open web browser with login page.
  2. User logs in and authorize my app.
  3. In a background I search for process with a specific name, save it (name) in app.
  4. At the end I close the web browser.

The problem is that if user had previously opened some tabs in web browser - in point 1. new tab is added and in point 4. everything is closed (web browser with all tabs).

Can you tell me if there is a way to close a single tab in web browser? Or maybe there is other/easier way to go through OAuth?

As a manual solution I will simply show info to the user “Now you can close this tab”, however I would like to do it automatically.

This is C# .Net 4.0 WPF project.

string strAuthUrl = "http://accounts.example.com/login",
       strAuthCode = string.Empty;

// Request authorization from the user (by opening a browser window):
ProcessStartInfo startInfo = new ProcessStartInfo(strAuthUrl);
startInfo.CreateNoWindow = false;

//1.        
Process.Start(startInfo);   
//2. - is happening in web browser
//3.        
do
{
    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.MainWindowTitle.StartsWith("Success code="))
        {
            strAuthCode = proc.MainWindowTitle.ToString().Substring(13, 30);
//4.
            try
            {

                // Close process by sending a close message to its main window.
                proc.CloseMainWindow();
                // Free resources associated with process.
                proc.Close();
                // Wait 500 milisecs for exit
                proc.WaitForExit(500);

                //if proc has not exited so far - kill it
                if (proc.HasExited == false)
                {
                    proc.Kill();
                    proc.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                //Do something with exception
            }

            break;
        }
    }
}
while (string.IsNullOrEmpty(strAuthCode));

Thanks for your suggestions.

like image 272
Tom K Avatar asked Mar 14 '12 13:03

Tom K


People also ask

How do I close a tab in my browser?

Tap the app icon of the browser that you want to open. You can close tabs on Chrome and Firefox for both iPhone and Android, as well as Safari for iPhone. Tap the "Tabs" icon.

How do I remove open tabs?

To clear all open browser tabs in the mobile versions of either browser: Tap the tab icon (the square with a number in it) in the top-right corner. Tap the three vertical dots in the top-right corner. Tap Close all tabs.


1 Answers

As Stephen Lee Parker suggested, it is indeed possible to send a keystroke to a certain window. That is, given that you have the handle for your browser window.

Also, your browser of choise should support a hotkey for closing seperate tabs, but Ctrl+W works most of the time.

With:

using System.Runtime.InteropServices;

and

[DllImportAttribute("user32.dll", EntryPoint = "SetForegroundWindow")]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow([InAttribute()] IntPtr hWnd);

[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

byte W = 0x57; //the keycode for the W key

public static void Send(byte KeyCode, bool Ctrl, bool Alt, bool Shift, bool Win)
{
    byte Keycode = (byte)KeyCode;

    uint KEYEVENTF_KEYUP = 2;
    byte VK_CONTROL = 0x11;
    byte VK_MENU = 0x12;
    byte VK_LSHIFT = 0xA0;
    byte VK_LWIN = 0x5B;

    if (Ctrl)
        keybd_event(VK_CONTROL, 0, 0, 0);
    if (Alt)
        keybd_event(VK_MENU, 0, 0, 0);
    if (Shift)
        keybd_event(VK_LSHIFT, 0, 0, 0);
    if (Win)
        keybd_event(VK_LWIN, 0, 0, 0);

    //true keycode
    keybd_event(Keycode, 0, 0, 0); //down
    keybd_event(Keycode, 0, KEYEVENTF_KEYUP, 0); //up

    if (Ctrl)
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
    if (Alt)
        keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
    if (Shift)
        keybd_event(VK_LSHIFT, 0, KEYEVENTF_KEYUP, 0);
    if (Win)
        keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);

}

You can virtually send any Keycode on your keyboard to the currently active window. Since you can really easily set the foreground window using Win32, this should work on most applications.

You also can see that this code first 'presses down' any chosen modifier keys, then presses the key with your given keycode down before releasing everything, making it able to send CTRL+W for example.

    void CloseTab()
    {
       SetForegroundWindow(_browserWindow.Handle);
       Send(W, true, false, false, false); //Ctrl+W
    }

Good luck!,

(Also this is my first answer so I really hope i'm able to help some of you)

like image 196
iKevenaar Avatar answered Oct 13 '22 19:10

iKevenaar