28

After looking at this youtube video I was curious how some of the features shown, can be implemented with JS.

One of my major questions is how can one detect another system window (like the word window, shown in the video) on the iframe.

On another video there is a hint suggesting that the technic is based on the fact that browsers optimize rendering for elements that are out of the view.

I couldn't tap into what are the exact methods/properties that are used.

What are your thoughts?

Roni Gadot
  • 437
  • 2
  • 19
  • 30

2 Answers2

7

What I know it's possible to detect if page is in foreground or in background - or more precisely: if has focus or has not focus.

var focus = true;
window.onblur = function() { focus = false; action(); }
window.onfocus = function() { focus = true; action(); }

document.onblur = window.onblur;
document.focus = window.focus;
 
function action(){
 if(focus) {
  document.body.style.background = "green";
 } else {
  document.body.style.background = "lightgray";
 }
}
try click inside and then outside

The above code snippet uses event listeners onblur and onfocus for events focus and blur.  


 

Better can be to use Visibility API:

  • it works when switching tabs (page can detect that user has opened another tab)

Note: While onblur and onfocus will tell you if the user switches windows, it doesn't necessarily mean it's hidden. Pages only become hidden when the user switches tabs or minimizes the browser window containing the tab.

see Detect if browser tab is active or user has switched away

http://jsbin.com/lowocepazo/edit?js,output


For scrolling there is an Intersection Observer

provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport


//EDIT: Nowdays is not possible to detect such cases like for example in 1st video you posted (in 2:09) when another window is obscured some element:  enter image description here

If I'm wrong, please correct me.

related:

Lukas M.
  • 2,859
  • 23
  • 30
  • Thanks, you provided good info (nothing new to me) ... if it's not possible, can you try and guess what was the method they used? – Roni Gadot Jun 03 '18 at 10:49
  • Can confirm: no visible difference in requestAnimationFrame calls https://jsfiddle.net/Farbdose/u2tquxbx/ https://www.chromestatus.com/feature/4514713492783104 only slows execution based on viewport coordinates not on real overlap. – Fabian N. Jun 03 '18 at 17:13
  • The advertissement is maybe made with adobe flash. – user2226755 Jun 07 '18 at 12:01
4

You have to check document.hasFocus and position/size of windows and screen monitor.

Maybe like this :

demo

You can try my demo here : https://jsfiddle.net/p9ahuo3t/

let bool = document.hasFocus();

$("p.info").text("in");
console.log(outerWidth + screenX)
if (screen.width < outerWidth + screenX) {
    bool = false;
    $("p.info").text("right side: out");
} else if ((outerWidth - innerWidth) + screenX < 0) {
    bool = false;
    $("p.info").text("left side: out");
} else if (screen.height < outerHeight + screenY) {
    bool = false;
    $("p.info").text("bottom side: out");
} else if ((outerHeight - innerHeight) + screenY < 0) {
    bool = false;
    $("p.info").text("top side: out");
}

if (currentChild && !currentChild.closed) {  
    let rectPage = {
        left:   (outerWidth - innerWidth) + screenX,
        top:    (outerHeight - innerHeight) + screenY,
        right:  outerWidth + screenX,
        bottom: outerHeight + screenY
    };

    let rectPopup = {
        left:   currentChild.screenX,
        top:    currentChild.screenY,
        right:  currentChild.outerWidth + currentChild.screenX,
        bottom: currentChild.outerHeight + currentChild.screenY
    }; 
    if (intersectRect(rectPage, rectPopup)) {
        $("p.info").text("eclipse with popup");
        bool = false;
    }
}
$page.text(pin(bool));

Also :

user2226755
  • 12,494
  • 5
  • 50
  • 73
  • Very impressive demo, however this won't work with another app window (like word for example) – Roni Gadot Jun 07 '18 at 14:01
  • @RoniGadot I'm not sure that your video was pure javascript or real. Other developpers would find the same thing. – user2226755 Jun 07 '18 at 15:22
  • I hope my answer helps you but in javascript we can't do do more. – user2226755 Jun 08 '18 at 10:01
  • @Hors Sujet, this video was made by spider.io that were aquired by Google, so I'm guessing it's real. I'm curious what was the technic. – Shlomi Schwartz Jun 09 '18 at 14:39
  • It is strange that only one find that... http://spider.io/blog/2011/10/calling-out-to-researchersacademics It's maybe browser issue like that : https://www.youtube.com/watch?v=mngp2ACiZLM – user2226755 Jun 09 '18 at 15:14