0

Some websites stop working (for example, playing music) if one moves from one tab to another. Question: Is it possible for the client to stop this behaviour?

There are multiple ways to check if the current browser tab is focused. One can use document.hasFocus() or window.addEventListener('focus', function), for example. The visibilitychange or blur events aren't cancelable, so Event.stopPropagation wouldn't work. But is there any other way to stop their propagation? (For example, could an addon break the way the blur event works and not make it dispatch?)

Or is there a way to simulate a tab having focus via the JS console? For example, by toggling a checkbox in the Web Console or by constantly retriggering a focus event after the tab loses focus? (Or maybe could tampermonkey force set the read-only document.visibilityState always to visible?)


Regarding the question being closed: This question is not the same as Is there a way to detect if a browser window is not currently active?, it's asking from the opposite direction: that question asks how can one detect changes in focus, this one asks how can one avoid that detection

flen
  • 1,905
  • 1
  • 21
  • 44
  • 2
    Yes, it is possible, but first you'll need to find out which mechanism the page uses for detecting focus (there are a few). – Bergi Jun 24 '22 at 23:12
  • 2
    For a simple solution, you can try making a tampermonkey script that makes `document.hasFocus = () => true;` and override `window.AddEventListener` to if the first argument is `"focus"` don't actually register a listener. – Samathingamajig Jun 24 '22 at 23:14
  • 1
    @dippas No, that's the exact opposite of the question asked here – Samathingamajig Jun 24 '22 at 23:14
  • https://stackoverflow.com/questions/7389328/detect-if-browser-tab-has-focus – dippas Jun 24 '22 at 23:17
  • Yes, sites you wish to not perform this action you can write a simple script and inject through console or something like tampermonkey. – BGPHiJACK Jun 24 '22 at 23:31
  • I'd try to scope the function they run and cancel it by over-writing it; if that doesn't work create another event listener to re-enable the function when you go out of focus! :) – BGPHiJACK Jun 24 '22 at 23:33
  • @BGPHiJACK thanks! I think tampermonkey would be the best solution to inject the script as soon as the page starts loading, otherwise changing `addEventListener` and so on would be too late. Ideally, there'd be some `stopPropagation` solution, but I don't think there's a similar thing for this event – flen Jun 25 '22 at 00:00
  • @Samathingamajig that's a good idea! But it'd also have to check for `document.hidden`, `document.visibilityState`, `document.addEventListener("visibilitychange", onchange)` and possibly something else I'm missing. Ideally, there'd be some way to keep dispatching a "focus" event (or to stop propagation of any "blur"), but I don't think there's such a thing – flen Jun 25 '22 at 00:11
  • While it's not a perfect solution, one can [use the browser inspector and remove eventListeners](https://superuser.com/a/1462761/423890) – flen Jul 31 '22 at 02:43

0 Answers0