How can I check if the browser is active or not in the cross-raid?

I need to calculate the browser using the time in the extension. If I can check if the browser is active or not, then it's easy to count the time. How is this possible in a cross-raid?

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

I tried this code in background.js but always displayed "true" even if I minimize the browser window.

+4
source share
1 answer

Focus and blur events are not available in the background area of ​​any extension, even those that do not use Crossrider.

A common solution is to use the extension area (extension.js) to detect events and then transfer information to the background page using messaging .

, :

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: Crossrider]

+6

All Articles