razonasistemas
razonasistemas

Reputation: 371

How to close the current tab of a browser from a desktop application?

I´m developing a system that consist in a program that runs in the desktop (Windows).

Each minute detects if the active program is a browser and in this case checks if the url of the active tab is in a "black list"

In this case I would like to close the current tab of the browser. What I´m doing now is killing the browser process, and that means that tabs with urls not in the black list were closed too.

For detecting the url of the active tab, I´m using the solution from How to get the url from Chrome using delphi

I´m using Delphi 7, but a solution in any language will be appreciate.

Upvotes: 1

Views: 1753

Answers (2)

I am using this methodology to close browser tabs. As you know, Chrome and Iexplorer uses different process for every tabs. I find process name and kill them. In the following code is killing all process in the memory for chrome or iexplore or what ever you want. Don't forget to add winapi.tlhelp32 unit in your uses line.

function KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  // find all process and kill them all one by one    
  while Integer(ContinueLoop) <> 0 do
  begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
      UpperCase(ExeFileName))) then
      Result := Integer(TerminateProcess(
                        OpenProcess(PROCESS_TERMINATE,
                                    BOOL(0),
                                    FProcessEntry32.th32ProcessID),
                                    0));
     ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

Upvotes: 1

Paul Sweatte
Paul Sweatte

Reputation: 24617

Use the following process:

  • Start recording a macro
  • Open a browser
  • Press the keyboard shortcut to close a tab
  • Stop recording the macro
  • Create a shortcut to the macro

Upvotes: 0

Related Questions