Close Mozilla Firefox popups in C#

[Firefox popups]

Some browser tools try to block advertising popups, but lately some have been sneaking through on my system so I wrote this program to close Firefox popups when they appear. Start Firefox and create the windows that you want. Then start the program. Later, when a web page tries to display a popup, the program closes it.

The example List desktop windows in C# lists the running desktop windows. This example uses the same technique to make a list of windows when it starts. Later if a new window appears that ends with the string “Mozilla Firefox,” the program stops it.

When the program starts, it uses the following code to make a list of desktop windows.

// Make a list of the current Firefox windows.
private List<IntPtr> Handles = new List<IntPtr>();
private List<string> Titles = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
    // Get the desktop windows.
    List<IntPtr> handles = new List<IntPtr>();
    List<string> titles = new List<string>();
    DesktopWindowsStuff.GetDesktopWindowHandlesAndTitles(
        out handles, out titles);

    // Save the Firefox windows.
    for (int i = 0; i < handles.Count; i++)
    {
        if (titles[i].EndsWith("Mozilla Firefox"))
        {
            Handles.Add(handles[i]);
            Titles.Add(titles[i]);
        }
    }

    // Display the results.
    lstAllowedWindows.DataSource = Titles;
}

The code first creates lists to hold the handles and titles of the current Firefox windows. Then the form’s Load event handler calls the GetDesktopWindowHandlesAndTitles method described in the previous post. The code loops through the returned windows and saves those with titles ending in “Mozilla Firefox.”

The program’s Timer uses the following code to check for new windows every second.

// Look for new Firefox windows.
private void tmrCheckForPopups_Tick(object sender, EventArgs e)
{
    // Get the desktop windows.
    List<IntPtr> handles = new List<IntPtr>();
    List<string> titles = new List<string>();
    DesktopWindowsStuff.GetDesktopWindowHandlesAndTitles(
        out handles, out titles);

    // Look for new Firefox windows.
    for (int i = 0; i < handles.Count; i++)
    {
        if (titles[i].EndsWith("Mozilla Firefox"))
        {
            // See if this is a new window.
            if (!Handles.Contains(handles[i]))
            {
                // It's new. Stop it.
                DesktopWindowsStuff.StopWindow(handles[i]);
                lstStoppedWindows.Items.Add(titles[i]);

                // Add it to the list of allowed handles
                // so we don't try to stop it again if it
                // takes a while to close.
                Handles.Add(handles[i]);
            }
        }
    }
}

This code calls GetDesktopWindowHandlesAndTitles again to get a new list of the current windows. It then loops through the windows looking for those with titles that end in “Mozilla Firefox.” When it finds such a window, the program checks whether the Handles list already contains that window’s handle. If the handle is not in the list, the program uses the StopWindow method (described next) to stop the window. It displays the window’s title in the stopped windows ListBox and adds the window’s handle to the Handles list so it won’t try to stop the window again. This prevents the program from trying to stop a window several times if it takes a little while to close.

The following code shows how the StopWindow method closes Firefox popups.

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg,
    IntPtr wParam, IntPtr lParam);
...
// Send a message to a process.
private static void PostMessageToWindow(IntPtr hWnd, uint msg,
    IntPtr wParam, IntPtr lParam)
{
    if (!PostMessage(hWnd, msg, wParam, lParam))
    {
        // An error occured.
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }
}

// Stop a window.
public static void StopWindow(IntPtr hWnd)
{
    PostMessageToWindow(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}

This code fragment starts by declaring the PostMessage API function. This function sends a message to the window with the indicated handle.

The PostMessageToWindow method simply calls the PostMessage API function.

Finally the StopWindow method calls PostMessageToWindow to send a window the WM_CLOSE message to a window tell it to close itself.

Note that this method only works if the popup window’s title ends with “Mozilla Firefox.” Fortunately that seems to be true of Firefox popups so the program can stop them.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in API, system and tagged , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *