0

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")
Meric Ozcan
  • 678
  • 5
  • 25
  • This is really weird code. No idea why the original author chose to add a COM reference, when you can use the standard UIAutomation class. Also note that enumerating all descendants of the main Window, you get all UI Elements currently in the view, which includes all HTML elements, in Chromium browsers. Not in FireFox, for now -- The address bar of EdgeChrome, with class name `OmniboxViewViews`, supports automation (e.g., `ValuePattern` and `TextPattern`) and you can set the value of that Edit Element to an address and make the Browser navigate to that address – Jimi Jan 05 '23 at 22:29
  • The Process Id of all Tabbed Documents is the same as the main Process. Tabs are graphic elements, just allow to switch to another Web View – Jimi Jan 05 '23 at 22:32
  • @Jimi I added the ".com" string comparison. Do you have a example code for Chromium and Firefox browsers? I have been searching for this for a long time but couldn't find anything. Lastly when you look with Task Manager you can see that there are process Ids for each chrome tab, if you enlarge the Chrome Process. – Meric Ozcan Jan 06 '23 at 07:52
  • 1
    I don't know how a *".com" string comparison* helps; do you suppose all addresses contain that string? -- Did you try adding `SendKeys.Send("{ENTER}")` after `val.SetValue("SomeUrl.com")` -- I think I've already written something like this for both EdgeChrome and FireFox. You can find the (only) Element, direct child of the main Window Element, that allows editing (Edit Element Type, focusable) -- The Processes you see in Task Manager are not related to Tab Pages, though some are linked to WebView Documents. The Process ID of the current View is only one process (always the same) – Jimi Jan 06 '23 at 12:49
  • I used that ".com" for a dummy entry, in future I might use a string list to blacklist some websites. I tried endKeys.Send("{ENTER}") after val.SetValue("SomeUrl.com") Yes, However somehow it doesn't have the effect. It changes the URL but enter button cannot take effect. – Meric Ozcan Jan 06 '23 at 13:03
  • https://stackoverflow.com/questions/11402643/sendkey-send-not-working Send Key didn't worked but this one worked, hurray!! @Jimi – Meric Ozcan Jan 06 '23 at 13:12

0 Answers0