Redirecting Web browser tab on Windows

166 views Asked by At

I would like to redirect web browser Url with a different specific URL. I believe I can do it by changing the Host file in Windows, however I would like to try a way to do it Winforms/C# app.

One way of doing it would be getting current tab process Id, closing it and open a new process for chrome, IE Explorer or firefox. Below code is working and it can get the active URL from any web browser. However it cannot get the Web browswer tab process id. It get the Main Chrome Process Id. How can I get that Tab info? Or can I do it a different way?

public readonly CUIAutomation _automation  = new CUIAutomation();
public void HandleFocusChangedEvent(IUIAutomationElement element)
    {
        if (element != null)
        {
            try
            {
                using (Process process = Process.GetProcessById(element.CurrentProcessId))
                {
                    try
                    {
                        if ((!process.ProcessName.Contains("chrome") || process.ProcessName.Contains("browswermsedge") || process.ProcessName.Contains("firefox"))) {
                            Debug.WriteLine("Action is not from a web browswer, " + process.ProcessName);
                            return;
                        }

                        IUIAutomationElement elm = _automation.ElementFromHandle(process.MainWindowHandle);
                        IUIAutomationCondition Cond = _automation.CreatePropertyCondition(30003, 50004);
                        IUIAutomationElementArray elm2 = elm.FindAll(TreeScope.TreeScope_Descendants, Cond);
                        
                        for (int i = 0; i < elm2.Length; i++)
                        {
                            IUIAutomationElement elm3 = elm2.GetElement(i);
                            IUIAutomationValuePattern val = (IUIAutomationValuePattern)elm3.GetCurrentPattern(10002);
                            if (val == null)
                            {
                                continue;
                            }

                            if (val.CurrentValue != "" && val.CurrentValue.Contains(".com"))
                            {
                                Debug.WriteLine("An URL FOUND: " + val.CurrentValue);
                                // AN URL FOUND BUT CANNOT GET PROCESS ID WITH
                                // elm3.CurrentProcessId it gives main Chrome process Id
                            }
                        }
                    }
                    catch
                    {
                        Debug.WriteLine("Exception occured while getting active application URL");
                    }
                }
            }
            catch {
                Debug.WriteLine("Could not get process Information while getting the active URL");
            }
        }
    }

Above code is from this stack overflow answer Btw I am open to any suggestion on redirecting URL on web browswer. I even tried below code which changes the URL in active tab, but it doesn't go that web site.

val.SetValue("SomeUrl.com")
0

There are 0 answers