chrome.windows.onFocusChanged not working

1,605 views
Skip to first unread message

Rajesh Katalkar

unread,
Dec 21, 2013, 12:55:35 PM12/21/13
to chromium-...@chromium.org
i am using chrome.windows.onFocusChanged event to detect focus change event...It work well when focus is changed between windows..
but i don't get any event when i minimize and maximize the browser once it has some page loaded....
I need to do some clean on minimize event..

steps to repro
I have a extension which alerts on this event..

OS: win7 64bit (not tested other OS)

steps to repro
1)make sure no windows are on desktop only plain desktop..you can do show desktop for that
2)open browser...which will have empty new tab..
3)minimize the browser ...will get alert
4)maximize it ....will get alert
5)load some page...
3)minimize the browser ...no alert
4)maximize it ....no alert

Rajesh Katalkar

unread,
Dec 22, 2013, 4:30:13 AM12/22/13
to Chromium-extensions
more updates on the steps


steps to repro
1)make sure no windows are on desktop only plain desktop..you can do show desktop for that
2)open browser...which will have empty new tab..
3)minimize the browser ...will get alert
4)maximize it ....will get alert
5)load some page...
6)minimize the browser by click minimize button from top right corner...no alert
7)maximize it  from task bar....no alert

===>if on step 6 we minimize and maximize by clicking taskbar icon then alerts work.



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/8e3bbe9f-a552-4d2c-a360-4ec03ce276e9%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.

Rajesh Katalkar

unread,
Dec 28, 2013, 12:50:41 PM12/28/13
to chromium-...@chromium.org
No one seems to respond...This bug needs to be fixed...But for now i have made my own custom focuschange event using chromeapi's and blur and focus events of page and other can use it also....
The idea is we inject a js in every frame at document start which will send focus and blur events, so that we don't get focus event at first as we already are in focus....Because i inject my other working js in document end....Then we process it in background js and use chrome api's to decide focus or not focus....
Below is the code for this.

put following on background js file
===============================
function ChromeWindow(list)
{
  this.focusedwindow = null;
  this.Event = Event;
  this.FocusEvtListner = list;
 
  function Event(tab,request)
  {
     if(request == undefined || tab == undefined)
       return;
   
  
     if(request.command == "winevent")
     {         
       console.log(request.state+" domain="+request.domain);
       chrome.windows.get(tab.windowId ,winget.bind(this));
       function winget(win)
       {
           if(win == undefined || win == null)
             return;
             
           if(this.focusedwindow == null)
           {
            this.focusedwindow = win;
            if(tab.status == "complete")
              this.FocusEvtListner(win,tab);
           }
           else if(this.focusedwindow.id!=win.id || this.focusedwindow.focused!=win.focused)   
           {
             console.log("diff chrome win or state changed");      
             this.focusedwindow = win;
             if(tab.status == "complete")             
               this.FocusEvtListner(win,tab);
           }
           else
            console.log("same chrome win");      
           
           console.log("chrome  winid="+win.id +" state ="+win.state +" focused="+win.focused );
       }
     }
  }
}

_ChromeWindow = new ChromeWindow(OnFocusEvt);

function OnFocusEvt(win,tab)
{   
  console.log("\n\nOnFocusEvt start");
  if(win.focused)
    console.log("focused");
  else
    console.log("not focused");
   
  console.log("OnFocusEvt  winid="+win.id +" state ="+win.state +" focused="+win.focused +" tab.status="+tab.status);
  console.log("OnFocusEvt taburl ="+tab.url);
}

chrome.runtime.onMessage.addListener(OnRequest);

function OnRequest(request, sender, sendResponse)
{
   consolelog(sender.tab ?
   "from a content script:" + sender.tab.url :
   "from the extension");
     
     _ChromeWindow.Event(sender.tab,request);
    return;
}
=============================================================
another content script ...injected at document start

focusevt.js

{
      "matches": ["<all_urls>"],
      "js": ["focusevt.js"],
      "run_at" : "document_start",  
      "all_frames": true     
    }

window.onblur = function()
{
 chrome.runtime.sendMessage({command:"winevent",state:"blur",domain:document.domain});
}

window.onfocus = function()
{
  chrome.runtime.sendMessage({command:"winevent",state:"focus",domain:document.domain});
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.

Giedrius Drulia

unread,
Sep 17, 2014, 10:18:14 AM9/17/14
to chromium-...@chromium.org
There is a bug, but I found that window property focused actually tells truth about current state of the window, so you can query in intervals getCurrent window and check its property focused, if window is not in focus, it means that chrome is not in focus itself.

Hopefully this helps.
Reply all
Reply to author
Forward
0 new messages