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();
}
}
}
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.
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.
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.
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.
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/
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With