I’m trying to create a Chrome extension that will pop up a new window in a position relative to the recently focused window. I don’t think I’m using the chrome.windows.getCurrent or chrome.windows.getLastFocused methods properly to do this. Every time I do, I get an undefined alert when I try to show a property of that window.
In my background.js file, I have:
chrome.pageAction.onClicked.addListener(showPopup);
function showPopup() {
var left = chrome.windows.getCurrent(function (w) {
w.left - 200;
// also tried: return w.left - 200;
});
alert(left); // undefined
}
Reading the chrome.windows API docs left me confused on how to actually return an attribute of a window. Can anyone shed some light here?
I’m not sure if there’s another (or better) way to do this, but I got this to work:
My original attempt was wrong because I was trying to return a value from the
get()callback function and place that into a variable. This, essentially evaluated tovar left = chrome.windows.get(winId, 790), which is an invalid function call. Rather than that, my call tochrome.windows.create()needed to be inside the callback function.Also, a miss before was that the click listener calls a function with a
tabparameter, not awindowparameter. So I needed thegetCurrentInfo()function to pass in ataband thenchrome.windows.getCurrent()to pass in window info.