-1

How can social media web apps determine if your page is on screen vs minimized?

Here's an example: Whatsapp Web tells people that you're online if your page is open and on screen. It also does not load chats if it is minimized. How do I make it think that it is on screen. I would like some Javascript code that I can inject into my UIWebView (iOS) so that it keeps loading chats even when the UIWebView is on the background. Or some other solution to the problem.

What happens right now is that when I open a chat, it loads the last message and has a loading icon on top until I actually open the app. Only once I open the app, it loads the rest of the messages.

EDIT: From one of the answers, I think I should be running a code similar to this:

document.focus();

Still does not load the rest of the chat unless on screen.

  • Does this answer your question? [Detect If Browser Tab Has Focus](https://stackoverflow.com/questions/7389328/detect-if-browser-tab-has-focus) – r3wt Mar 18 '20 at 00:04
  • It helps! But I would like to make it focus programmatically. Inject some Javascript code to make it think the page is on screen. Would you know how to do that? Something like this: self.webView.evaluateJavaScript("document.focus();") – Malek Amiri Mar 18 '20 at 00:17
  • Welcome to SO. Please read [ask] and provide a [example] to illustrate your question. – koen Mar 18 '20 at 00:18

1 Answers1

1
setInterval( checkFocus, 200 );

function checkFocus() {
  document.hasFocus() ?  console.log('focus'): console.log('blur')
}

or you can do

window.addEventListener('focus', () => console.log('focus'));
 window.addEventListener('blur', () => console.log('blur'));
Colin
  • 1,195
  • 7
  • 10