David 天宇 Wong
David 天宇 Wong

Reputation: 4197

firefox sdk: track if firefox is being run in the background

I'm trying to detect if Firefox is being run in the foreground (to track time being spent on websites).

Is there an easier way than tracking new windows being activated/deactivated?

Upvotes: 1

Views: 35

Answers (2)

David 天宇 Wong
David 天宇 Wong

Reputation: 4197

I ended up using windows.browserWindows.on close and deactivate / open and activate, coupled with tabs.on activate, deactive, close, ready and also tabs.activeTab.id/url/title

var active = true

/////////////////////////////////////////////
// Active Firefox?
////////////////////////////////////////////

// testing purpose

//tmr.setInterval(function(){console.log(active)}, 500)

//
// detect if firefox is running in foreground
//

windows.browserWindows.on('open', function(window) {
    active = true
})


windows.browserWindows.on('activate', function(window) {
    active = true
})

//
// detect is firefox is running in background
//

windows.browserWindows.on('deactivate', function(window) {
    active = false
})

windows.browserWindows.on('close', function(window) {
    active = false
})

Upvotes: 1

nmaier
nmaier

Reputation: 33162

  • The tabs module offers activate and deactivate events.
  • Another way is the Page Visibility API, which you could use e.g. in a PageMod. Will also trigger when the browser window is minimized.

Both of these will not trigger if (AFAIK) when the browser window itself becomes unfocused.

Upvotes: 1

Related Questions