Sunday 15 September 2013

javascript - How to determine if Chrome Window is miniminzed from a Chrome Extension -



javascript - How to determine if Chrome Window is miniminzed from a Chrome Extension -

i'm writing chrome extension , want determine if chrome window minimized. on searching stackoverflow.com found next solution, not working me , error: error in response windows.get: typeerror: undefined not function @ chrome-extension:// ......

i have next code in background.js

chrome.windows.onfocuschanged.addlistener(function(windowid) { if (windowid === -1) { console.window("minimized"); } else { chrome.windows.get(windowid, function(chromewindow) { if (chromewindow.state === "minimized") { console.window("minimized"); } else { console.window("not minimized"); } }); } });

are there special permissions should add together manifest create above code work? help highly appreciated.

your problem obvious: doing there on line #3, #7 , #9? there no such console.window() function... that's why getting error: because property window of object console undefined. looks trying log information, utilize wrong function. what want console.log().

here right code:

chrome.windows.onfocuschanged.addlistener(function(windowid) { if (windowid === -1) { console.log("minimized"); } else { chrome.windows.get(windowid, function(chromewindow) { if (chromewindow.state === "minimized") { console.log("minimized"); } else { console.log("not minimized"); } }); } });

plus, quoting documentation:

when requested, windows.window contain array of tabs.tab objects. must declare "tabs" permission in manifest if require access url, title, or faviconurl properties of tabs.tab.

so, if want access windows' tabs, you'll have add together permission in manifest:

... "permissions": [ "tabs", ... ], ...

note: you'll never log "minimized", because onfocuschanged event fired when window gets focused, not when loses focus. if want see if window minimized you'll have utilize chrome.windows.getall() method , iterate on various windows check whether minimized or not.

addendum: check windows minimized:

function dosomethingwithminimizedwindows(windows) { (var = 0; < windows.length; i++) { // windows[i] // example: console.log('window #'+windows[i].id+' minimized!'); } } chrome.windows.getall(function(windows) { var minimized = []; (var = 0; < windows.length; i++) { if (windows[i].state === "minimized") { minimized.push(windows[i]); } } dosomethingwithminimizedwindows(minimized); });

the above code produce following:

window #378 minimized! window #457 minimized! window #460 minimized! window #463 minimized!

javascript google-chrome dom google-chrome-extension

No comments:

Post a Comment