'How to get URLs and tab Titles from Edge

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 :)



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xudong Peng