I'd like to execute a console mand on a specific tab and receive a boolean answer whether the tab is playing audio. (Usually stated as a little speaker icon near the tab's title)
Can this be done?
I'd like to execute a console mand on a specific tab and receive a boolean answer whether the tab is playing audio. (Usually stated as a little speaker icon near the tab's title)
Can this be done?
Share Improve this question edited Jun 21, 2017 at 15:21 Flip 6,7618 gold badges50 silver badges83 bronze badges asked Jan 14, 2017 at 12:14 Omer HOmer H 4355 silver badges14 bronze badges2 Answers
Reset to default 9You can detect if a Chrome tab is playing audio with a Chrome Extension, using the chrome.tabs API. The API provides a boolean audible
property, as described below:
Whether the tab has produced sound over the past couple of seconds (but it might not be heard if also muted). Equivalent to whether the speaker audio indicator is showing.
Source: https://developer.chrome./extensions/tabs
There's no way to access the tabs from the console itself, as it is run from the context of the current page. In the background page of a Chrome Extension, you have access to all the tabs, and you can query them using the API outlined above. Your extension can simply provide a panel that displays a list of all the open tabs and a flag to say whether the audio is playing.
Aside: If you just want to quickly mute HTML5 Audio/Video in the current tab using the Chrome console, you can run the following (the $$
is equivalent to document.querySelectorAll()
):
$$('video', 'audio').forEach((element) => element.muted = true);
Perhaps something like this will solve your problem:
!!Array.prototype.find.call(document.querySelectorAll('audio,video'),function(elem){return elem.duration > 0 && !elem.paused})
Detects both audio and video elements currently playing. Works cross browsers.