如何从 Edge 获取 URL 和选项卡标题

发布于 2025-01-09 15:13:05 字数 192 浏览 0 评论 0原文

我一直在做一些研究,但没有发现任何当前有用的东西。我正在尝试创建一个程序,可以在边缘当前打开的选项卡中获取所有 URL 及其标题。我猜这必须使用 C# 来完成,我没有这方面的经验。有人可以创建一个能够执行此操作的程序吗?我不介意想要语言,我只想将它们全部存储起来 - 最好是 JSON 键值对 - 或者甚至只是纯文本也可以。任何人都可以帮忙,因为我没有使用边缘的经验:)

I have been doing some research and have found nothing current and of use. I am trying to create a program that can get all URLs and their Titles in the currently open tabs on edge. I am guessing this would have to be done using C#, in which I have no experience in. Could someone please create a program that would be able to do this please? I don't mind want language I would just want to have them all stored - preferable in JSON key-value pairs - or even just plain text is fine. can anyone help as I have no experience working with edge :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2025-01-16 15:13:05

如果需要获取当前 Edge 中所有选项卡的 url 和标题,可以使用 System.Windows.Automation C# 中的 API。

这是一个简单的演示:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using System.Windows.Forms;

namespace ConsoleDemo
{
    class Program
    {
        [DllImport("User32.dll")]
        static extern int SetForegroundWindow(IntPtr point);

        [DllImport("User32")]
        private static extern int ShowWindow(int hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref Windowplacement lpwndpl);

        private struct Windowplacement
        {
            public int length;
            public int flags;
            public int showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        public static List<string> GetAndChangeTabUrl()
        {
            Process[] procsEdge = System.Diagnostics.Process.GetProcessesByName("msedge");
            List<string> tabs = new List<string>();
            foreach (Process proc in procsEdge)
            {
                //string name = proc.ProcessName;
                Windowplacement placement = new Windowplacement();
                GetWindowPlacement(proc.MainWindowHandle, ref placement);

                // Check if window is minimized
                if (placement.showCmd == 2)
                {
                    //the window is hidden so we restore it
                    ShowWindow(proc.MainWindowHandle.ToInt32(), 9);
                }

                //Switch Edge tab to the first one
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.SendWait("^1");

                if (proc.MainWindowHandle == IntPtr.Zero)
                    continue;

                string TabUrl = string.Empty;
                string TabTitle = string.Empty;
                AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
               
                Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
                foreach (AutomationElement tabitem in root.FindAll(TreeScope.Subtree, condTabItem))
                {
                    var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
                    TabUrl = (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
                    TabTitle = tabitem.Current.Name;
                    tabs.Add("URL: " + TabUrl + " ----Title: " + TabTitle);
                    SetForegroundWindow(proc.MainWindowHandle);
                    SendKeys.SendWait("^{TAB}"); // change focus to next tab
                }
            }
            return tabs;
        }

        static void Main(string[] args)
        {
            List<string> result = GetAndChangeTabUrl();
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

这就是结果:

在此处输入图像描述

If you need to get the url and title of all tabs in the current Edge, you can use the System.Windows.Automation api in C#.

Here is a simple demo:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using System.Windows.Forms;

namespace ConsoleDemo
{
    class Program
    {
        [DllImport("User32.dll")]
        static extern int SetForegroundWindow(IntPtr point);

        [DllImport("User32")]
        private static extern int ShowWindow(int hwnd, int nCmdShow);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowPlacement(IntPtr hWnd, ref Windowplacement lpwndpl);

        private struct Windowplacement
        {
            public int length;
            public int flags;
            public int showCmd;
            public System.Drawing.Point ptMinPosition;
            public System.Drawing.Point ptMaxPosition;
            public System.Drawing.Rectangle rcNormalPosition;
        }

        public static List<string> GetAndChangeTabUrl()
        {
            Process[] procsEdge = System.Diagnostics.Process.GetProcessesByName("msedge");
            List<string> tabs = new List<string>();
            foreach (Process proc in procsEdge)
            {
                //string name = proc.ProcessName;
                Windowplacement placement = new Windowplacement();
                GetWindowPlacement(proc.MainWindowHandle, ref placement);

                // Check if window is minimized
                if (placement.showCmd == 2)
                {
                    //the window is hidden so we restore it
                    ShowWindow(proc.MainWindowHandle.ToInt32(), 9);
                }

                //Switch Edge tab to the first one
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.SendWait("^1");

                if (proc.MainWindowHandle == IntPtr.Zero)
                    continue;

                string TabUrl = string.Empty;
                string TabTitle = string.Empty;
                AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
               
                Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
                foreach (AutomationElement tabitem in root.FindAll(TreeScope.Subtree, condTabItem))
                {
                    var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
                    TabUrl = (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
                    TabTitle = tabitem.Current.Name;
                    tabs.Add("URL: " + TabUrl + " ----Title: " + TabTitle);
                    SetForegroundWindow(proc.MainWindowHandle);
                    SendKeys.SendWait("^{TAB}"); // change focus to next tab
                }
            }
            return tabs;
        }

        static void Main(string[] args)
        {
            List<string> result = GetAndChangeTabUrl();
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

And this is the result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文