如何从 chrome 获取打开的标签列表?|C# [英] How to get a list of open tabs from chrome? | C#

查看:23
本文介绍了如何从 chrome 获取打开的标签列表?|C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想从 google chrome 中提取打开的标签(标题、URL)并列出主题,就像在 chrome 任务管理器中一样.到目前为止,我已尝试过滤所有 chrome 进程并获取窗口标题,但这不起作用:

So I want to extract the open tabs from google chrome (title, URL) and list theme out kind of like in the chrome task manager. So far I have tried to filter all the chrome processes and get the window titles but that doesn't work:

var procs = Process.GetProcesses();

...

foreach (var proc in procs)
{
   if (Convert.ToString(proc.ProcessName) == "chrome")
   {
      Console.WriteLine("{0}: {1} | {2} | {3} ||| {4}
", i, proc.ProcessName, runtime, proc.MainWindowTitle, proc.Handle);
   }
}

这并没有给我标签的地址或标题,还有其他方法吗?

This doesn't give me the address or the title of the tab, is there another way to do it?

推荐答案

先引用两个dll

UIAutomationClient.dll
UIAutomationTypes.dll

位于:C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0(或 3.5)

然后

using System.Windows.Automation;

和代码

Process[] procsChrome = Process.GetProcessesByName("chrome");
if (procsChrome.Length <= 0)
{
   Console.WriteLine("Chrome is not running");
}
else
{
   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);
      Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
      AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
      // get the tabstrip by getting the parent of the 'new tab' button 
      TreeWalker treewalker = TreeWalker.ControlViewWalker;
      AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
      // loop through all the tabs and get the names which is the page title 
      Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
      foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
      {
          Console.WriteLine(tabitem.Current.Name);
      }
   }
}

这篇关于如何从 chrome 获取打开的标签列表?|C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆