简体   繁体   English

Chrome扩展相对于当前窗口的打开弹出窗口

[英]Chrome Extension open popup relative to current window

I'm trying to create a Chrome extension that will pop up a new window in a position relative to the recently focused window. 我正在尝试创建一个Chrome扩展程序,该扩展程序将在相对于最近聚焦的窗口的位置弹出一个新窗口。 I don't think I'm using the chrome.windows.getCurrent or chrome.windows.getLastFocused methods properly to do this. 我认为我没有正确使用chrome.windows.getCurrentchrome.windows.getLastFocused方法来执行此操作。 Every time I do, I get an undefined alert when I try to show a property of that window. 每当我尝试显示该窗口的属性时,都会收到undefined警报。

In my background.js file, I have: 在我的background.js文件中,我有:

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. 阅读chrome.windows API文档后,我对如何实际返回窗口属性感到困惑。 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. 我最初的尝试是错误的,因为我试图从get()回调函数返回一个值并将其放入变量中。 This, essentially evaluated to var left = chrome.windows.get(winId, 790) , which is an invalid function call. 这实际上是评估为var left = chrome.windows.get(winId, 790) ,这是一个无效的函数调用。 Rather than that, my call to chrome.windows.create() needed to be inside the callback function. 而不是说,我的电话给chrome.windows.create()是回调函数需要。

Also, a miss before was that the click listener calls a function with a tab parameter, not a window parameter. 此外,以前的遗漏是单击侦听器调用带有tab参数而不是window参数的函数。 So I needed the getCurrentInfo() function to pass in a tab and then chrome.windows.getCurrent() to pass in window info. 因此,我需要getCurrentInfo()函数传递一个tab ,然后chrome.windows.getCurrent()传递窗口信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM