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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With