0

I'm trying to find the first audio playing tab in Chrome (or other browsers).

I've already managed to get the titles of all opened chrome tabs (mainly using the information from Getting the current tab's URL from Google Chrome using C# and Get chrome browser title using c#) .

But just looking for the a tab including the word "Youtube" seems a bit weird (since it would also find the linked questions or other "non music stuff" as long as it finds the word Youtube).

Is there a way to find the first audio playing tab?

Edit
Fixed the initial problem:

Process[] chromeProcesses = Process.GetProcessesByName("chrome");

        if(chromeProcesses.Length > 0)
        {
            foreach(Process proc in chromeProcesses)
            {
                if (proc.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                AutomationElement chrome = AutomationElement.FromHandle(proc.MainWindowHandle);

                var tabs = chrome.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
                if (tabs != null)
                {
                    foreach(AutomationElement tab in tabs)
                    {
                        string tabTitle = tab.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
//...

I'm currently checking for "- YouTube" (at the end of the string) in the tabtitle. But there's a way to get even more precise; by getting the next element to the tabs text.

With the inspect.exe tool I've found out that a "currently playing" tab has an empty button (actually the speakers icon) next to the text - otherwise it's a close button. Hence by checking for the next element it should be possible to find out if the tab is also playing music.

But I'm currently struggling to find the next element. How to do that with the AutomationElement class?

Community
  • 1
  • 1
nehalist
  • 1,434
  • 18
  • 45
  • You can query the audible tabs from a Chrome extension, using chrome.tabs.query (https://developer.chrome.com/extensions/tabs#method-query). With C#, while there might be a way using all kind of Pinvokes, it would be a brittle solution since Google might change their design at any time. – Siderite Zackwehdex Mar 31 '16 at 10:10
  • Shouldn't it be possible to find the tabs with the `AutomationElement` class? – nehalist Mar 31 '16 at 19:55

1 Answers1

1

Fixed everything:

Process[] chromeProcesses = Process.GetProcessesByName("chrome");

        if(chromeProcesses.Length > 0)
        {
            foreach(Process proc in chromeProcesses)
            {
                if (proc.MainWindowHandle == IntPtr.Zero)
                {
                    continue;
                }

                AutomationElement chrome = AutomationElement.FromHandle(proc.MainWindowHandle);

                var tabs = chrome.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
                if (tabs != null)
                {
                    foreach(AutomationElement tab in tabs)
                    {
                        string tabTitle = tab.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();

                        AutomationElement next = tab.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
                        if (next != null)
                        {
                            string nextContent = next.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
                            if(nextContent != "")
                            {
                                break;
                            }
                        }

                        if (tabTitle.LastIndexOf(YoutubeRecogPattern, StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            if (tabTitle != this.LastTitle)
                            {
                                string videoTitle = tabTitle.Substring(0, tabTitle.Length - YoutubeRecogPattern.Length);
// ...

Where the YoutubeRecogPattern is - YouTube.

nehalist
  • 1,434
  • 18
  • 45