Guest User

Untitled

a guest
Nov 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. public List<string> ChromeTabs()
  2. {
  3. List<string> ret = new List<string>();
  4.  
  5. Process[] procsChrome = Process.GetProcessesByName("chrome");
  6.  
  7. if (procsChrome.Length <= 0)
  8. {
  9. Console.WriteLine("Chrome is not running");
  10. }
  11. else
  12. {
  13. foreach (Process proc in procsChrome)
  14. {
  15. // the chrome process must have a window
  16.  
  17. if (proc.MainWindowHandle == IntPtr.Zero)
  18. {
  19. continue;
  20. }
  21.  
  22. // to find the tabs we first need to locate something reliable - the 'New Tab' button
  23. AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
  24. Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
  25. AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
  26.  
  27. // get the tabstrip by getting the parent of the 'new tab' button
  28. TreeWalker treewalker = TreeWalker.ControlViewWalker;
  29. AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); // <- Error on this line
  30.  
  31. // loop through all the tabs and get the names which is the page title
  32. Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
  33. foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
  34. {
  35. ret.Add(tabitem.Current.Name);
  36. //Console.WriteLine(tabitem.Current.Name);
  37. }
  38. continue;
  39. }
  40. }
  41. return ret;
  42. }`
  43.  
  44. delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
  45.  
  46. [DllImport("user32.dll")]
  47. private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc ewp, int lParam);
  48.  
  49. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  50. static extern bool GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  51.  
  52. [DllImport("user32.dll")]
  53. private static extern uint GetWindowText(IntPtr hWnd, StringBuilder lpString, uint nMaxCount);
  54.  
  55. [DllImport("user32.dll")]
  56. private static extern uint GetWindowTextLength(IntPtr hWnd);
  57.  
  58. [DllImport("user32.dll")]
  59. static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  60.  
  61. // get all the process id's of the running chrome processes
  62. Process[] chromeProcesses = Process.GetProcessesByName("chrome");
  63. List<uint> chromeProcessIds = chromeProcesses.Select(x => (unit)x.Id).ToList();
  64.  
  65. // a list to store the handles of the Google Chrome windows that we'll find
  66. List<IntPtr> windowHandles = new List<IntPtr>();
  67.  
  68. EnumWindowsProc enumerateHandle = delegate (IntPtr hWnd, int lParam)
  69. {
  70. // get the id of the process of the window we are enumerating over
  71. uint id;
  72. GetWindowThreadProcessId(hWnd, out id);
  73.  
  74. // if the process we're enumerating over has an id in our chrome process ids, we need to inspect it to see if it is a window or other process
  75. if (chromeProcessIds.Contains(id))
  76. {
  77. // get the name of the class of the window we are inspecting
  78. var clsName = new StringBuilder(256);
  79. var hasClass = GetClassName(hWnd, clsName, 256);
  80. if (hasClass)
  81. {
  82. // get the text of the window we are inspecting
  83. var maxLength = (int)GetWindowTextLength(hWnd);
  84. var builder = new StringBuilder(maxLength + 1);
  85. GetWindowText(hWnd, builder, (uint)builder.Capacity);
  86.  
  87. var text = builder.ToString();
  88. var className = clsName.ToString();
  89.  
  90. // actual Google Chrome windows have text set to the title of the active tab
  91. // in my testing, this needs to be coupled with the class name equaling "Chrome_WidgetWin_1".
  92. // i haven't tested this with other versions of Google Chrome
  93. if (!string.IsNullOrWhiteSpace(text) && className.Equals("Chrome_WidgetWin_1", StringComparison.OrdinalIgnoreCase))
  94. {
  95. // if we satisfy the conditions, this is a Google Chrome window. Add the handle to the list of handles to use later.
  96. windowHandles.Add(hWnd);
  97. }
  98. }
  99. }
  100. return true;
  101. };
  102.  
  103. EnumDesktopWindows(IntPtr.Zero, enumerateHandle, 0);
  104.  
  105. foreach (IntPtr ptr in windowHandles)
  106. {
  107. AutomationElement root = AutomationElement.FromHandle(ptr);
  108.  
  109. // continue grabbing Chrome tab information using the AutomationElement method
  110. ...
  111. }
Add Comment
Please, Sign In to add comment