Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop through chrome tabs and close page depending on web address

Tags:

c#

.net

wpf

I would like to be able to loop through all the tabs on a chrome page and close any tabs which are youtube pages.

I have done some googling & found the code below. There are two (well probably more) issues. Firstly I have create a WPF application and added the System.Windows.Automation namespace (using visual studio 2015 .net 4.5) but AutomationElement is not recognised.

Also I am unsure of how to loop through the tabs and test if a page is a youtube page.

        Process[] procsChrome = Process.GetProcessesByName("chrome");

        if (procsChrome.Length <= 0)
            return null;

        foreach (Process proc in procsChrome)
        {
            // the chrome process must have a window 
            if (proc.MainWindowHandle == IntPtr.Zero)
                continue;

            // to find the tabs we first need to locate something reliable - the 'New Tab' button 
            AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
            var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
            if (SearchBar != null)
            {
                AutomationPattern[] patterns = SearchBar.GetSupportedPatterns();
                if(patterns.Length > 0)
                {
                    ValuePattern val = (ValuePattern)SearchBar.GetCachedPattern(patterns[0]);
                    if (val.Current.Value.Contains("youtube.com") || val.Current.Value.Contains("youtube.co.uk"))
                        proc.Close();
                }
            }
         }
like image 460
mHelpMe Avatar asked Jan 25 '17 16:01

mHelpMe


People also ask

Can Chrome cycle through tabs automatically?

Auto Refresh & Switch Tabs. Auto-refresh is an extension for Google Chrome which monitors the HTTP traffic and if a page fails to load, it auto-refreshes that page. This description specially develop to Switch to next tab & refresh next (+1) tab. The duration of Switch to next tab & refresh next (+1) tab is 20 seconds.

How do I make Chrome automatically close tabs?

Tap the three-dot menu icon in the toolbar. Select “Settings” from the menu. Next, go to “Tabs.” Under the “Close Tabs” section you can choose how long to wait before inactive tabs are automatically closed.

How do I toggle between tabs automatically?

On Windows, Linux, and macOS devices, press Control + Tab to move forward to the next tab on the right. On the flip side, Shift + Control + Tab will cycle backward through open tabs.

How do I cluster tabs in Chrome?

Organize your tabs with Chrome tab groups To create a tab group, just right-click any tab and select Add tab to new group. Right-click a tab. Click Add Tab to new group. Click New Group or click the name of an existing tab group.


2 Answers

It can be done easily with AutoHotkey

Here is a script that will open Chrome, loop all tabs and close the YouTube ones, then minimize Chrome again

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

SetTitleMatchMode, 2


IfWinExist, ahk_class Chrome_WidgetWin_1
{
    WinActivate, Chrome
    WinGetActiveTitle, ActiveWindowOld
    Loop {
        Send, ^{Tab}
        Sleep, 500
        WinGetActiveTitle, ActiveWindow
        if (ActiveWindow = ActiveWindowOld)
        {
            break
        }
        IfInString, ActiveWindow, YouTube - Google Chrome
        {
            Send, ^{w}
        }
    }
    WinMinimize, Chrome
}

Credit: https://autohotkey.com/board/topic/148742-cycling-through-all-chrome-tabs-and-closing-a-specific-tab/

like image 134
Hannott Avatar answered Nov 04 '22 09:11

Hannott


if (SearchBar != null)
{
    bool valuePatternExist = (bool)SearchBar.GetCurrentPropertyValue(AutomationElement.IsValuePatternAvailableProperty);
    if (valuePatternExist)
    {
        ValuePattern val = SearchBar.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
        if (val.Current.Value.Contains("youtube.com") || val.Current.Value.Contains("youtube.co.uk"))
            proc.Close();
    }
}
like image 34
Maxwell77 Avatar answered Nov 04 '22 08:11

Maxwell77