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:
chrome.pageAction.onClicked.addListener(getCurrentInfo);
function getCurrentInfo() {
chrome.windows.getCurrent(showPopup);
}
function showPopup(win) {
alert(win.left); // correct pixel count from left
}
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 to var left = chrome.windows.get(winId, 790)
, which is an invalid function call. Rather than that, my call to chrome.windows.create()
needed to be inside the callback function.
Also, a miss before was that the click listener calls a function with a tab
parameter, not a window
parameter. So I needed the getCurrentInfo()
function to pass in a tab
and then chrome.windows.getCurrent()
to pass in window info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.