1

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

kidders
  • 13
  • 6
  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 24 '22 at 12:37

1 Answers1

2

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

Xudong Peng
  • 1,463
  • 1
  • 4
  • 9
  • Wow thanks so much this is perfect. Excuse my lack of knowledge but how would I run this?? Preferably something as an exe file so it can be run by a .bat file. – kidders Feb 25 '22 at 11:29
  • This simple demo running in console app using C#. You could manually create a bat file to run it, simply refer to [this answer](https://stackoverflow.com/questions/3406313/how-to-create-bat-file-to-run-c-sharp-code#answer-3406340). I tested this and it works well. – Xudong Peng Feb 28 '22 at 02:11
  • Can we do this without switching tabs? – Muhammad Khuzaima Umair Aug 21 '22 at 02:52