Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check whether the browser is active or not in crossrider?

I need to calculate the browser using time in an extension. If i can check whether the browser is active or not, then it is easy to calculate the time. How is this possible in crossrider?

var focused = true;
window.onfocus = window.onblur = function(e) {
    focused = (e || event).type === "focus";
}
alert(focused);

i tried this code in background.js but it always display "true" even if i minimize the browser window.

like image 620
Sumesh P Avatar asked Mar 15 '23 20:03

Sumesh P


1 Answers

The focus and blur events are not available in the background scope of any extension, even ones not using Crossrider.

The common solution is to use the extension scope (extension.js) to detect the events and then pass the information to the background page using messaging.

Using your code as a base, the following example should help you achieve your goal:

extension.js

appAPI.ready(function($) {
  window.onfocus = window.onblur = function(e) {
    appAPI.message.toBackground({
      focus: (e || event).type === "focus"
    });
  }
});

background.js

appAPI.ready(function($) {
  appAPI.message.addListener(function(msg) {
    console.log('focus: '+msg.focus);
  });
});

[Diclosure: I am a Crossrider employee]

like image 58
Shlomo Avatar answered May 28 '23 20:05

Shlomo