diff --git a/.gitignore b/.gitignore index 69a99ab..d92ad33 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /CSharpLibraryTools/CoreActivities/bin/Debug/netcoreapp3.1 /CSharpLibraryTools/CoreActivities/obj /CSharpLibraryTools/.vs/CSharpLibraryTools/config +/CSharpLibraryTools/CoreActivities/bin/Debug diff --git a/CSharpLibraryTools/CSharpLibraryTools/Implementations/BrowseActivityImp.cs b/CSharpLibraryTools/CSharpLibraryTools/Implementations/BrowseActivityImp.cs index 3de39a9..9a21d3a 100644 --- a/CSharpLibraryTools/CSharpLibraryTools/Implementations/BrowseActivityImp.cs +++ b/CSharpLibraryTools/CSharpLibraryTools/Implementations/BrowseActivityImp.cs @@ -7,22 +7,61 @@ namespace CSharpLibraryTools public class BrowseActivityImp { private readonly IBrowserActivity _browserActivity; + private readonly BrowserActivityEnumAdaptee _browserActivityEnumAdaptee; - public BrowseActivityImp(IBrowserActivity browserActivity) + public BrowseActivityImp(IBrowserActivity browserActivity, + BrowserActivityEnumAdaptee browserActivityEnumAdaptee) { _browserActivity = browserActivity; + _browserActivityEnumAdaptee = browserActivityEnumAdaptee; } public async Task Run() { await Task.Run(() => { - var activeUrl = _browserActivity.EnlistActiveTabUrl(BrowserType.Chrome); - Console.WriteLine($"Active URL: {activeUrl}\n"); + //Check browser open or not + Console.WriteLine($"The browser {_browserActivityEnumAdaptee.ToDescriptionString(BrowserType.Chrome)} " + + $"{(_browserActivity.IsBrowserOpen(BrowserType.Chrome)?"is open": "is not open")}"); - var tabs = _browserActivity.EnlistAllOpenTabs(BrowserType.Chrome); - foreach (var item in tabs) - Console.WriteLine($"Tab: {item}"); + Console.WriteLine($"The browser {_browserActivityEnumAdaptee.ToDescriptionString(BrowserType.Edge)} " + + $"{(_browserActivity.IsBrowserOpen(BrowserType.Edge) ? "is open" : "is not open")}"); + + Console.WriteLine($"The browser {_browserActivityEnumAdaptee.ToDescriptionString(BrowserType.FireFox)} " + + $"{(_browserActivity.IsBrowserOpen(BrowserType.FireFox) ? "is open" : "is not open")}"); + + Console.WriteLine($"The browser {_browserActivityEnumAdaptee.ToDescriptionString(BrowserType.Opera)} " + + $"{(_browserActivity.IsBrowserOpen(BrowserType.Opera) ? "is open" : "is not open")}"); + + Console.WriteLine($"The browser {_browserActivityEnumAdaptee.ToDescriptionString(BrowserType.Safari)} " + + $"{(_browserActivity.IsBrowserOpen(BrowserType.Safari) ? "is open" : "is not open")}"); + + //Check open browser active url and tabs + if (_browserActivity.IsBrowserOpen(BrowserType.Chrome)) + { + var activeUrl = _browserActivity.EnlistActiveTabUrl(BrowserType.Chrome); + Console.WriteLine($"Active URL: {activeUrl}\n"); + + var activeTitle = _browserActivity.EnlistActiveTabTitle(BrowserType.Chrome); + Console.WriteLine($"Active Tab Title: {activeTitle}\n"); + + var tabs = _browserActivity.EnlistAllOpenTabs(BrowserType.Chrome); + foreach (var item in tabs) + Console.WriteLine($"Tab: {item}"); + } + + if (_browserActivity.IsBrowserOpen(BrowserType.Edge)) + { + var activeUrl = _browserActivity.EnlistActiveTabUrl(BrowserType.Edge); + Console.WriteLine($"Active URL: {activeUrl}\n"); + + var activeTitle = _browserActivity.EnlistActiveTabTitle(BrowserType.Chrome); + Console.WriteLine($"Active Tab Title: {activeTitle}\n"); + + var tabs = _browserActivity.EnlistAllOpenTabs(BrowserType.Edge); + foreach (var item in tabs) + Console.WriteLine($"Tab: {item}"); + } }); } } diff --git a/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdaptee.cs b/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdaptee.cs index 4625cc5..82e38ab 100644 --- a/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdaptee.cs +++ b/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdaptee.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Windows.Automation; namespace CoreActivities.BrowserActivity @@ -12,46 +13,47 @@ public class BrowserActivityAdaptee public BrowserActivityAdaptee(BrowserActivityEnumAdaptee browserActivityEnumAdaptee) => _browserActivityEnumAdaptee = browserActivityEnumAdaptee; - public string GetActiveTabUrl(BrowserType browserType) + public string GetActiveTabTitle(BrowserType browserType) { try { - // There are always multiple chrome processes, so we have to loop through all of them to find the - // process with a Window Handle and an automation element of name "Address and search bar" + // Find the automation element + var elm = GetAutomationElement(browserType); + if (elm != null) + return elm.Current.Name; - var browserName = _browserActivityEnumAdaptee.ToDescriptionString(browserType); - var browserProcess = Process.GetProcessesByName(browserName); + return string.Empty; + } + catch (Exception ex) + { + throw new Exception(ex.Message); + } + } - if (browserProcess.Length > 0) + public string GetActiveTabUrl(BrowserType browserType) + { + try + { + // Find the automation element + var elm = GetAutomationElement(browserType); + if(elm != null) { - var url = string.Empty; - foreach (var chrome in browserProcess) - { - // The browser process must have a window - if (chrome.MainWindowHandle == IntPtr.Zero) - continue; - - // Find the automation element - var elm = AutomationElement.FromHandle(chrome.MainWindowHandle); - var elmUrlBar = elm.FindFirst(TreeScope.Descendants, - new PropertyCondition(AutomationElement.NameProperty, "Address and search bar")); + var elmUrlBar = elm.FindFirst(TreeScope.Descendants, + new PropertyCondition(AutomationElement.NameProperty, "Address and search bar")); - // If it can be found, get the value from the URL bar - if (elmUrlBar != null) + // If it can be found, get the value from the URL bar + if (elmUrlBar != null) + { + var patterns = elmUrlBar.GetSupportedPatterns(); + if (patterns.Length > 0) { - var patterns = elmUrlBar.GetSupportedPatterns(); - if (patterns.Length > 0) - { - var val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]); - url = val.Current.Value; - } + var val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]); + return val.Current.Value; } } - - return url; } - else - return string.Empty; + + return string.Empty; } catch (Exception ex) { @@ -66,17 +68,17 @@ public IList GetOpenTabsInfos(BrowserType browserType) var tabInfos = new List(); var browserName = _browserActivityEnumAdaptee.ToDescriptionString(browserType); - var processes = Process.GetProcessesByName(browserName); + var processes = GetBrowserProcessByBrowserType(browserType); - if (processes.Length <= 0) - throw new Exception($"No process found with this {browserName} name"); + if (processes.Count <= 0) + return new List(); else { foreach (var proc in processes) { - if (proc.MainWindowHandle != IntPtr.Zero) + if (proc.Process.MainWindowHandle != IntPtr.Zero) { - var root = AutomationElement.FromHandle(proc.MainWindowHandle); + var root = AutomationElement.FromHandle(proc.Process.MainWindowHandle); var condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem); var tabs = root.FindAll(TreeScope.Descendants, condition); var enumerator = tabs.GetEnumerator(); @@ -97,5 +99,58 @@ public IList GetOpenTabsInfos(BrowserType browserType) throw new Exception(ex.Message); } } + + public IList GetBrowserProcessByBrowserType(BrowserType browserType) + { + var browser = _browserActivityEnumAdaptee.ToDescriptionString(browserType).ToLower(); + var processes = Process.GetProcesses(); + + return processes.Select(x => new ProcessAndTitle + { + MainWindowTitle = x.MainWindowTitle, + Process = x + }).Where(x => x.MainWindowTitle.ToLower().Contains(browser)) + .GroupBy(x => x.MainWindowTitle) + .Select(x => x.First()) + .ToList(); + } + + private AutomationElement GetAutomationElement(BrowserType browserType) + { + try + { + // There are always multiple chrome processes, so we have to loop through all of them to find the + // process with a Window Handle and an automation element of name "Address and search bar" + var browserProcess = GetBrowserProcessByBrowserType(browserType); + + if (browserProcess.Count > 0) + { + AutomationElement url = null; + foreach (var chrome in browserProcess) + { + // The browser process must have a window + if (chrome.Process.MainWindowHandle == IntPtr.Zero) + continue; + + // Find the automation element + return AutomationElement.FromHandle(chrome.Process.MainWindowHandle); + } + + return url; + } + else + return null; + } + catch (Exception ex) + { + throw new Exception(ex.Message); + } + } + } + + public class ProcessAndTitle + { + public string MainWindowTitle { get; set; } + public Process Process { get; set; } } } diff --git a/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdapter.cs b/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdapter.cs index 5c60cf8..b1c5851 100644 --- a/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdapter.cs +++ b/CSharpLibraryTools/CoreActivities/BrowserActivity/BrowserActivityAdapter.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Generic; +using System.Linq; namespace CoreActivities.BrowserActivity { public interface IBrowserActivity { + bool IsBrowserOpen(BrowserType browserType); IList EnlistAllOpenTabs(BrowserType browserType); string EnlistActiveTabUrl(BrowserType browserType); + string EnlistActiveTabTitle(BrowserType browserType); } public class BrowserActivityAdapter : IBrowserActivity @@ -21,6 +24,16 @@ public BrowserActivityAdapter(BrowserActivityAdaptee browserActivityAdaptee, _browserActivityEnumAdaptee = browserActivityEnumAdaptee; } + public string EnlistActiveTabTitle(BrowserType browserType) + { + var tabTitle = _browserActivityAdaptee.GetActiveTabTitle(browserType); + + if (string.IsNullOrWhiteSpace(tabTitle)) + throw new Exception($"No active tab title found in {_browserActivityEnumAdaptee.ToDescriptionString(browserType)} browser"); + + return tabTitle; + } + public string EnlistActiveTabUrl(BrowserType browserType) { var tabUrl = _browserActivityAdaptee.GetActiveTabUrl(browserType); @@ -39,5 +52,12 @@ public IList EnlistAllOpenTabs(BrowserType browserType) return tabs; } + + public bool IsBrowserOpen(BrowserType browserType) + { + var title = _browserActivityEnumAdaptee.ToDescriptionString(browserType).ToLower(); + var browserTitles = _browserActivityAdaptee.GetBrowserProcessByBrowserType(browserType); + return browserTitles.Count > 0 && browserTitles.Any(x => x.MainWindowTitle.ToLower().Contains(title.ToLower())); + } } } diff --git a/CSharpLibraryTools/CoreActivities/CoreActivities.csproj b/CSharpLibraryTools/CoreActivities/CoreActivities.csproj index e7d910c..60932e1 100644 --- a/CSharpLibraryTools/CoreActivities/CoreActivities.csproj +++ b/CSharpLibraryTools/CoreActivities/CoreActivities.csproj @@ -4,6 +4,17 @@ Library net5.0-windows netcoreapp3.1 + false + Md Shams Wadud + Reason Follower + ReasonFollower.CSharpLibraryTools.CoreActivities + Aim of this package is to separate daily code in a SOLID manner so that we could re-use the code again without reinventing the wheel. + MIT + https://github.com/abbirku/CSharpLibraryTools + Screen Capture, Running Programs, Google Drive, File Manager, Directory manager, Browser Activity, Active Program + false + 1.2.1 + Add EnlistActiveTabTitle in IBrowserActivity diff --git a/README.md b/README.md index be05969..733e61b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ _**Interface & Its members**_ | ------ | ------ | | EnlistAllOpenTabs(BrowserType browserType) | Returns list of open tabs title of a browser. | | EnlistActiveTabUrl(BrowserType browserType) | Returns url string of present visting website in a browser. | +| EnlistActiveTabTitle(BrowserType browserType) | Returns tab title of present visting website in a browser. | +| IsBrowserOpen(BrowserType browserType) | Returns bool. | _**Notes**_ - **BrowserType** is an Enum which contains **Chrome**, **FireFox**, **Edge**, **Opera**, **Safari**